在cloneWithRows

时间:2017-03-02 07:25:18

标签: android listview react-native react-jsx

我想将数组传递给cloneWithRows方法。 下面是代码

export default class FirstReactNativeApp extends Component {

          constructor() {
            super();
            const ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});

          this.state = {
            modalVisible: false,
            todoText:'',
            todoListArray:['foo 1','foo 2'],
           dataSource: ds.cloneWithRows(this.state.todoListArray)

          };
        }

这是Listview

<ListView
        dataSource={this.state.dataSource}
        renderRow={(rowData) => <Text>{JSON.stringify(rowData)</Text>}
/>

我收到错误 undefined不是对象(评估&#39; _this.state.todoListArray&#39;)

我无法解决这个问题。请指导我!

由于

3 个答案:

答案 0 :(得分:3)

试试这个 -

<ListView
        dataSource={this.ds.cloneWithRows(this.state.todoListArray)}
        renderRow={(rowData) => <Text>{JSON.stringify(rowData)</Text>}
/>
PHP DOM

答案 1 :(得分:1)

在设置之前,您正在访问state对象。所以它返回undefined。试试这个

export default class FirstReactNativeApp extends Component {

      constructor() {
        super();
        const ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});

      this.state = {
        modalVisible: false,
        todoText:'',
        todoListArray:['foo 1','foo 2'],
       dataSource: ds.cloneWithRows(['foo 1','foo 2'])

      };
  }

稍后当todoListArray内容发生变化时,您可以将其设置为dataSource

this.setState({
                dataSource: this.state.dataSource.cloneWithRows(this.state.todoListArray),
              });

答案 2 :(得分:0)

vinayr给出的上述答案给了我一个线索。

      this.state = {
        modalVisible: false,
        todoText:'',
        todoListArray:['foo 1','foo 2'],
        dataSource: ds

      };

然后在列表视图中

<ListView
  dataSource= {this.state.dataSource.cloneWithRows(this.state.todoListArray)}
  renderRow={(rowData) => <Text>{rowData}</Text>} />

有效!

由于