constructor() {
super();
this.state = {
idnum: this.props.match.params.id,
pokemon: [],
imgurl: '',
abilities: [],
types: []
};
this.clickHandler = this.clickHandler.bind(this);
};
clickHandler(e) {
this.setState({
idnum: this.props.match.params.id
})
console.log("passed" , this.props.match.params.id)
}
我正在尝试存储this.props.match.params.id,这是一个作为prop传递到我在React.js中的一个状态的id号.his.props.match.params.id在我的componentWillMount函数中工作使用此ID号从API调用特定数据,但是当我尝试将其存储在我的状态时,它给出了我未定义的匹配。
我需要这个才能工作,以便我可以通过执行
链接到具有先前Id编号的页面时重新呈现页面<Link to={{ pathname: '/details/' + (parseInt(this.props.match.params.id) - 1) }}><Button onClick = {this.clickHandler }>Prev</Button></Link>
将my.props.match.params.id存储到我的状态的方法是什么?
答案 0 :(得分:0)
要在构造函数中访问props,您可以执行以下操作:
constructor(props) {
super(props)
this.state = {
idnum: props.match.params.id
}
}