【问题标题】:How to save dataSource renderRow values into class variable in React Native如何将 dataSource renderRow 值保存到 React Native 中的类变量中
【发布时间】:2018-01-09 11:23:27
【问题描述】:

我是 React Native 的新手,我尝试使用 React Native 中的“fetch”方法获取学生记录并保存响应(我的响应看起来像)

   {"student":[{"student_id":"1","student_name":"JHON","student_class_id":"22"},
   {"student_id":"2","student_name":"BONY","student_class_id":"22"}],"error":false}) 

到dataSource如下

   .then((responseJson) => {
      let ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
      this.setState({
           dataSource: ds.cloneWithRows(responseJson.student)
        }, function() {
          // do something with new state
      });
    })

然后像这样渲染它

 getClassId(){
    // here I want to access rowData.student_class_id
 }

 render() {

    return(
     ....
     ....
 <View style={styles.card}>
          <ListView
              dataSource={this.state.dataSource}
              renderRow={
                (rowData) =>
                <View>
                  <Text style={styles.text}>{rowData.student_id}</Text>

                  <Text style={styles.text}>{rowData.student_name}</Text> 
                </View>
              }
          />
        </View> 
  );
} 

从响应中我们可以看到“student_class_id”字段。我不想在文本视图中显示它。但我想在这个渲染方法之外访问这个值。那是同一类的其他方法中的一些地方。

我没有得到任何可行的解决方案。请指导我实现它。任何建议表示赞赏。

【问题讨论】:

    标签: react-native fetch


    【解决方案1】:

    这是一个建议:

    1. 请不要使用 ListView,请改用 FlatList 或 SectionList。查看此blog post 了解更多信息
    2. 您可以在getClassId 中访问this.state.dataSource。您可以映射数据源并获取所需的 id

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-09-21
      • 1970-01-01
      • 1970-01-01
      • 2020-05-05
      • 1970-01-01
      • 1970-01-01
      • 2019-06-02
      相关资源
      最近更新 更多