2017-10-12 85 views
0
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 this.props我的狀態之一的id號.match.params.id在我的componentWillMount函數中起作用,它使用這個ID號從一個API調用一個特定的數據,但是當我嘗試將它存儲在我的狀態時,它給了我那個未定義的匹配。存儲作爲道具的狀態作出反應JS

我需要這讓我可以做

<Link to={{ pathname: '/details/' + (parseInt(this.props.match.params.id) - 1) }}><Button onClick = {this.clickHandler }>Prev</Button></Link> 

什麼是存儲我this.props.match的方式鏈接到與以前的身份證號碼的頁面時,頁面重新渲染工作。 params.id到我的狀態?

+1

請提供[MCVE。 –

+0

當你的應用程序調用構造函數時,this.props不存在。你需要在你的組件裝入後設置狀態,就像在componentDidMount中 – Cruiser

+0

可能重複[在構造函數中訪問道具](https://stackoverflow.com/questions/30571875/whats-the-difference-between-super-and-superprops-在反應的-時-使用-e)中 –

回答

0

要訪問的道具在構造函數中,你可以做到以下幾點:

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