【问题标题】:ListView not working: "undefined is not an object (evaluating 'datasource.row identities')"ListView 不工作:“未定义不是对象(评估'datasource.row 身份')”
【发布时间】:2017-11-12 03:54:28
【问题描述】:

我试图在我的 ListView 中查看项目“Pizza”,但出现错误。我查看了其他一些具有相同错误的 SO 帖子,但我无法找出问题所在。我们应该如何设置我的 ListView?我一直在关注这个guide 以供参考。我的代码如下:

class MyNewAppreactold extends Component {

  constructor(props) {
    super(props);
    this.state = {
      dataSource: new ListView.DataSource({
        rowHasChanged: (row1, row2) => row1 !== row2,
      })
    };
  }


  componentDidMount() {
    this.setState({
      dataSource: this.state.dataSource.cloneWithRows([{ title: 'Pizza' }])
    })
  }

  _renderItem(item) {
    return (
      <ListItem item={item} />
      // <ListItem item={item} onPress={onPress} />
      // <ListItem item={item} onPress={() ==""> {}} />
    );
  }

  render() {
    return (
      <View style={styles.container}>

        <StatusBar title="Grocery List" />

        <ListView datasource={this.state.dataSource}
          renderrow={this._renderItem.bind(this)}
          style={styles.listview}/>

        <ActionButton title="Add" />

      </View>
    )
  }
}

AppRegistry.registerComponent('MyNewAppreactold', () => MyNewAppreactold);

【问题讨论】:

  • 你能在你的代码中像这样制作 renderRow 并检查
  • 你使用的是renderrow,都是小写的。您可以像上面那样尝试并检查
  • @SivajeeBattina 我尝试了 renderRow。仍然出现同样的错误。

标签: javascript android ios listview react-native


【解决方案1】:

您是否尝试更改为dataSource

<ListView 
     dataSource={this.state.dataSource}
     renderRow={(rowData) => <ListItem item={rowData} />}
/> 

<ListView 
    dataSource={this.state.dataSource}
    renderRow={this.renderRow} /> 

然后

renderRow(rowData) { 
    return (
        <ListItem item={item} />
    ); 
}

【讨论】:

    【解决方案2】:

    请使用正确的道具名称: 将 datasource 更改为 dataSource,将 renderrow 更改为 renderRow。

    这段代码可以正常工作。

     <ListView dataSource={this.state.dataSource}
          renderRow={this._renderItem.bind(this)}
          style={styles.listview}/>
    

    【讨论】:

      【解决方案3】:

      如果有人偶然发现这个问题,

      • ListView 的“cloneWithRows”绑定在“componentDidMount”中不起作用。您需要将其推送到“componentWillMount”下。我不知道为什么,但我花了相当多的时间来弄清楚:-(

      【讨论】:

        猜你喜欢
        • 2016-12-04
        • 2019-09-26
        • 2019-12-12
        • 2019-08-16
        • 2017-01-16
        • 2020-08-06
        • 2020-01-09
        • 2017-05-03
        • 2023-03-07
        相关资源
        最近更新 更多