ListView数据源未更新 - React-Native

时间:2017-08-24 20:54:04

标签: android listview react-native datasource

我正在尝试从函数更新listview,但目前它没有更新,这是完整的源代码。

还请提一下我做错了什么

     import React, { Component } from 'react';
     import {
       AppRegistry,
       ListView,
       TextView,
       Text,
       View
     } from 'react-native';


     const ds = new ListView.DataSource({ rowHasChanged: (row1, row2) => row1 != row2 });

     var myarray = [];
     export default class filesix extends Component {
       constructor() {
         super();
         this.state = {
           dataSource: ds.cloneWithRows(myarray),
         };
         console.log(`ds const =${myarray}`);
       }


       componentDidMount() {
         myarray = ['11', '22'];
         console.log(myarray);
         this.setState = ({
           datasource: this.state.dataSource.cloneWithRows(myarray),
         });
         this.prepareDataSource();
         console.log('this componentDidMount');
       }

       prepareDataSource() {    
         myarray = ['11', '22'];
         console.log(myarray);
       }

       renderRow(rowData) {
         return <Text>{JSON.stringify(rowData)}</Text>
       }

       render() {
         return (
           <View style={{ flex: 1, borderWidth: 2 }}>
             <ListView
               enableEmptySections={true}
               dataSource={this.state.dataSource}
               renderRow={this.renderRow}
             />
           </View>
         );
       }
     }

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

我已经花了一整天来更新价值观,但没有运气,请纠正我的理解。

1 个答案:

答案 0 :(得分:1)

您的componentDidMount有拼写错误。它在datasource上设置状态,但您的ListView正在使用dataSource