Listview 当行是包含它的组件时,反应本机列表视图不更新';中国自己的国家

Listview 当行是包含它的组件时,反应本机列表视图不更新';中国自己的国家,listview,react-native,Listview,React Native,以下是我的主要组件的简化摘要: // this is only to simulate a change componentDidMount() { setTimeout( () => { this.setState({ dataSource: this.state.dataSource.cloneWithRows([ newVehicle ]), }) }, 5000) } render() { return ( <ListView

以下是我的主要组件的简化摘要:

// this is only to simulate a change
componentDidMount() {
  setTimeout( () => {
    this.setState({
       dataSource: this.state.dataSource.cloneWithRows([ newVehicle ]),
    })
  }, 5000)
}

render() {
 return (
  <ListView
    dataSource={this.state.dataSource}
    renderRow={this.renderVehicle}
  />
 )
}

renderVehicle(vehicle) {
  // here vehicle.name is properly updated
  return <Text>{vehicle.name}</Text> // this would be working
  return <Vehicle parentObject={ vehicle } /> // this is not working
}
//这只是为了模拟更改
componentDidMount(){
设置超时(()=>{
这是我的国家({
dataSource:this.state.dataSource.cloneWithRows([newVehicle]),
})
}, 5000)
}
render(){
返回(
)
}
renderVehicle(车辆){
//此处正确更新了vehicle.name
return{vehicle.name}//这将起作用
return//这不起作用
}
这是我的车辆部件:

// In here it seams the component is not rebuilt,
// and this.props.parentObject is always the old value...

this.state = this.props.parentObject

return() {
  <Text>{this.state.name}</Text>
}
//在这里,组件未重建,
//而这个.props.parentObject始终是旧值。。。
this.state=this.props.parentObject
返回(){
{this.state.name}
}

注意:在现实生活中,车辆是一个复杂的组件,需要有自己的状态。

据我所知,您需要在车辆中使用
组件WillReceiveProps(nextProps:Object)
,并使用nextProps更新状态

简单的例子:

componentWillReceiveProps(nextProps) {
  const { name } = nextProps;

  this.setState({name});
}

就这样!谢谢