簡體   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