繁体   English   中英

在React JS中将道具存储为状态

[英]Storing props as a state in React js

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存储为一个作为道具传递给我在React.js中的状态之一的ID号this.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>

将this.props.match.params.id存储到我的状态的方法是什么?

要在构造函数中访问props,可以执行以下操作:

constructor(props) {
  super(props)
  this.state = {
    idnum: props.match.params.id
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM