我正在寻找这么多的方式来解决这个内部状态,但没有一个人的作品,setState
仍然不是这里的componentWillReciveProps
方法里面工作是我的代码:反应母语:未更新componentWillReciveProps
export class Detail extends React.Component
{
constructor(props)
{
super(props);
this.state = {
ids: 'ger'
}
}
componentWillReceiveProps(nextProps) {
this.setState({ ids: nextProps.data },() => {
console.log(nextProps.data+"=this id")
});
}
render()
{
return (
<View>
<Text>{this.state.ids}</Text>
</View>
);
}
}
如果我做console.log(nextProps.data+"=this id")
它可以将我想更新的ID返回到this.state.ids
。但在<Text>{this.state.ids}</Text>
中,渲染仍显示默认值this.state.ids
('ger'),表示this.state.ids
未在componentWillReceiveProps
中更新。
我认为这与你的[上一个问题](https://stackoverflow.com/q/46389012/2315280)有关,并且可能是由于你的渲染项目上没有'key' prop 'ListView'。请看看[这个答案](https://stackoverflow.com/a/35229429/2315280) – bennygenel
我试过这种方式,仍然得到这个问题,奇怪的是'nextProps.data'获得id时成功我做'console.log(nextProps.data +“=这个id”)'(例如,输出是“4 = this id”),但ids状态仍然没有更新,即使我已经做了'this.setState({ids :nextProps.data})''componentWillReceiveProps''内:'( – janotama