2017-08-24 87 views
0

我試圖從函數更新listview,但目前它沒有更新,下面是完整的源代碼。ListView數據源未更新 - React-Native

請還提到,我在做什麼錯

 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); 

我已經花了我整整一天來更新值,但沒有運氣,請糾正我的理解。

+0

什麼沒有被更新?它渲染這兩行嗎? –

+0

不,它不是渲染兩行 –

回答

1

你的componentDidMount有一個錯字。它的設置狀態爲datasource,但是您的ListView使用的是dataSource

+0

添加更多的東西會是多餘的。 –