【发布时间】:2021-08-10 13:15:52
【问题描述】:
我有问题。我使用 React Router 来检查 URL 路径。
<Route path="/:brand" component={Container} />
我在Container组件中使用过
import React from 'react';
import CarPage from '../components/CarPage';
import '../styles/Container.css';
const Container = ({ match }) => {
return (
<CarPage brand={match.params.brand} />
);
}
export default Container;
在组件中,我想在“findMyIndex”函数中使用它来检查它在“carList”数组中的索引
import React, { Component } from 'react';
class CarPage extends Component {
state = {
index: 0,
}
carList = ["Ford", "Fiat", "Ferrari"];
findMyIndex = (props.brand) => {
console.log(props.brand);
}
render() {
return (
<p>{this.props.brand}</p>
);
}
}
export default CarPage;
然后他必须在this.state.index中输入找到的索引。问题是我不知道如何在类组件的函数中正确使用道具。我是初学者,想知道如何在“findMyIndex”中正确使用“brand”。请帮忙。
【问题讨论】:
-
我从来没有使用过类,但是由于箭头函数继承了上下文,
this.props.brand不工作吗? (根本没有将值传递给箭头函数)
标签: reactjs react-router react-props react-component