2017-04-24 30 views
-1

我試圖JSON URL調用後更新數據源,但ListView控件只是沒有更新,問題是當應用程序加載是第一次,我看不到ListView中的數據,但是,當我使用「熱更新...」時只需按下Ctrl + S在Visual Studio代碼中,列表突然出現在ListView中..陣營本地的,ListView控件+ JSON數據源獲取不更新

可能是什麼問題所以列表不會在第一次加載?只是第二次?

constructor(props) { 
    super(props); 
    var ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2}); 
    this.state = { 
     dataSource: ds.cloneWithRows([]) 
    }; 
    } 


states = { 
    index: 1, 
    routes: [ 
     { key: '1', title: 'RECOMMANDED' }, 
     { key: '2', title: 'MY STATIONS' } 
    ] 
    }; 

    _handleChangeTab = (index) => { 
    this.setState({ index }); 
    }; 

    _renderHeader = (props) => { 
    return <TabBar {...props} />; 
    }; 

    _renderScene = ({ route }) => { 


    switch (route.key) { 
    case '1': 
     return <View style={[ styles.page, { backgroundColor: '#fff' } ]}><ListView 
      dataSource={this.state.dataSource} 
      renderRow={this.renderRow.bind(this)} 
      enableEmptySections={true} 
     /></View>; 
    case '2': 
     return <View style={[ styles.page, { backgroundColor: '#fff' } ]}><Text>2</Text></View>; 
    default: 
     return null; 
    } 
    }; 

componentDidMount() { 
    fetch('https://facebook.github.io/react-native/movies.json') 
     .then((response) => response.json()) 
     .then((responseJson) => { 
     var movies = responseJson.movies; 
     this.setState({ 
      dataSource: this.state.dataSource.cloneWithRows(responseJson.movies) 
     }); 
     }).done(); 
    } 



renderRow(dataRow) { 
    return(
    <TouchableHighlight> 
     <View> 
      <Text 
      style={{fontSize: 20, color: '#000000'}} 
      numberOfLines={1}> 
      { dataRow.title } - { dataRow.releaseYear } 
      </Text> 
      <View style={{height: 1, backgroundColor: '#dddddd'}} /> 
     </View> 
     </TouchableHighlight>); 
    } 

    render() { 
    return (
     <View style={styles.container}> 
     <TabViewAnimated 
      style={styles.tabContainer} 
      navigationState={this.states} 
      renderScene={this._renderScene} 
      renderHeader={this._renderHeader} 
      onRequestChangeTab={this._handleChangeTab} /> 
     </View> 
    ); 
    } 

我使用react-native-tab-view模塊。

感謝

+0

'this.states'應該是'this.state' –

+0

我使用'this.states',而不是'this.state'因爲我已經設置好的狀態爲TabNav – John

+0

我現在看到它的工作,但只是後我只是按下「Ctrl + S」和「熱載入」應用程序..否則它顯示爲空,之後它顯示列表...可能是什麼問題? – John

回答

0

更改renderRow道具:

renderRow={this.renderRow.bind(this)} 

您應該手動此綁定到你的renderRow。使用ES6語法你不需要自動綁定,所以你需要自己做。如果您不想手動處理此綁定,則可以使用箭頭語法。

renderRow(movie) { 
} 

曾經對我變了樣

renderRow = (movie) => { 
} 

然後,像@約翰說,你應該使用狀態。一個this.state屬性可能有很多狀態。

+0

它不會工作出於某種原因,我改變了'renderRow = {this.renderRow.bind(this)}'並且它在第一次加載應用程序時仍然不工作,只是當我正在進行「熱更新」時,我可以看到列表。 – John

+0

沒有錯誤或警告? – Ludovic

+0

不,沒有,只是沒有加載列表,當應用程序第一次加載.. – John