React native 反应本机:当道具更改时更新ListView行

React native 反应本机:当道具更改时更新ListView行,react-native,react-native-listview,shoutem,React Native,React Native Listview,Shoutem,我确实有一个带有renderRow()函数的ListView组件。我的自定义ListBountiesView组件呈现一个ListView行,它还采用一个名为cr的道具,并根据cr的值在每一行中显示一些内容 问题是当this.props.customerRelationship更改其值时,ListView行不会得到更新 我使用:this.props.customerRelationship.points+=responseJson.points 我猜ListView仅在data属性更改时更新,但是

我确实有一个带有
renderRow()
函数的ListView组件。我的自定义
ListBountiesView
组件呈现一个ListView行,它还采用一个名为
cr
的道具,并根据
cr
的值在每一行中显示一些内容

问题是当
this.props.customerRelationship
更改其值时,ListView行不会得到更新

我使用:
this.props.customerRelationship.points+=responseJson.points

我猜
ListView
仅在
data
属性更改时更新,但是如何将道具移动到我的renderRow组件,以便它们也更新ListView

SuperScreen.js

renderRow(bounty) {
  const { customerRelationship } = this.props;
  return (
    <ListBountiesView
      key={bounty.id}
      bounty={bounty}
      cr={customerRelationship}
      onPress={this.openDetailsScreen}
    />
  );
}

render() {
    const { bounties } = this.props;

    return (
      <ListView
          data={bounties}
          renderRow={this.renderRow}
          loading={loading}
      />
    )
renderRow(赏金){
const{customerRelationship}=this.props;
返回(
);
}
render(){
const{bounties}=this.props;
返回(
)

将您的
customerRelationship
移动到
bounty
对象。每个
bounty
都应该具有此属性,然后检查它在
行hasChanged
中的值是否更改。另一种方法是检查
组件WillReceiveProps
函数中的
customerRelationship
,如果它的值更改了绑定
,使其所有子对象都具有新的对象引用。

将您的
客户关系
移动到
赏金
对象。每个
赏金
都应具有此属性,然后检查
行中更改的值是否已更改
。另一种方法是检查
组件中的
客户关系
s
函数,如果其值已更改,则克隆
奖金
,使其所有子对象都具有新的对象引用。

数据
道具获得新引用时,列表视图刷新数据源:

 componentWillReceiveProps(nextProps) {
    if (nextProps.data !== this.props.data) {
      this.setState({ dataSource: this.listDataSource.clone(nextProps.data) });
    }

    if (nextProps.loading !== this.props.loading) {
      this.setLoading(nextProps.loading);
    }
  }

同样,React Native的ListView会在其数据源更改时刷新。这在他们的GitHub和许多其他线程中都有讨论。普遍接受的解决方法是创建一个新的数据数组。在您的情况下,只要
customerRelationship
发生更改,就在
componentWillReceiveProps中执行此操作。

ListView会刷新当
数据
道具获得新的引用时,数据源:

 componentWillReceiveProps(nextProps) {
    if (nextProps.data !== this.props.data) {
      this.setState({ dataSource: this.listDataSource.clone(nextProps.data) });
    }

    if (nextProps.loading !== this.props.loading) {
      this.setLoading(nextProps.loading);
    }
  }

同样,React Native的ListView会在其数据源更改时刷新。这在他们的GitHub和许多其他线程中都有讨论。普遍接受的解决方法是创建一个新的数据数组。在您的情况下,只要
customerRelationship
发生更改,就在
componentWillReceiveProps中执行此操作。

您没有更新ode>dataSource
任何地方,这就是它不发生变化的原因。正如其他人所建议的,您需要使用其他生命周期事件来捕获变化。或者,您可以考虑使用
FlatList
,它不需要您设置
dataSource
对象,只需要获取一个数组,这样,它会在y时自动更新我们的道具会发生变化。你不会在任何地方更新你的
数据源
,这就是它不会变化的原因。正如其他人所建议的,你需要使用其他生命周期事件来捕获变化。或者,你可以考虑使用
平面列表
,它不需要你设置
数据源
对象,只需要一个数组-这样,当您的道具更改时,它会自动更新。
cloneWithRows
cloneWithRows