reactjs - 按新闻更新平面列表项

标签 reactjs react-native native

如何更新flatlist项目onpress中的项目文本?

renderEntries({ item, index }) {
    return(
        <TouchableHighlight onPress={()=> this.setState({value: this.state.value+1})>
             <Text>{this.state.value}</Text>
        </TouchableHighlight>
    )
}

我收到此错误:

enter image description here

最佳答案

我最近遇到了同样的问题,解决方案对我有用

import React, { Component } from 'react';
import {
  AppRegistry,
  StyleSheet,
  SectionList,
  Text,
  NativeModules,
  Button,
  FlatList,
  Switch,
  View
} from 'react-native';


export default class alarm extends Component {

  state={
    data:[
      { name:'First' , isOn:true },
      { name:'Second', isOn:true },
      { name:'Third' , isOn:true },
      { name:'Fourth' , isOn:false }
    ],
    selected:true,
  }

  _keyExtractor = (item, index) => item.name;

  _onValueChanged = (item,value) => 
  {    
      var items = this.state.data;
      var index = items.indexOf(item);
      items[index].isOn = value;
      this.setState({data:items});
      this.setState({selected:!this.state.selected});  
  };

  _renderItem = ({item}) => 
  (    
    <View>  
        <Text>{ item.name }</Text>
        <Switch value={item.isOn}  
        onValueChange={(value) =>
        {        
         this._onValueChanged(item,value);
        }}/>
    </View>
  )

  render() {
    return (

       <FlatList
          data={this.state.data}
          renderItem={this._renderItem}
          extraData={this.state.selected}  // This is the Key you need to privde extra data parmater
          keyExtractor={this._keyExtractor}
        />

    );
  }
}
AppRegistry.registerComponent('alarm', () => alarm);

关于reactjs - 按新闻更新平面列表项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43393369/

相关文章:

ios - React-native 阻止函数异步执行?

java - 从 .so 在 tomcat 中调用 Java 中的 native 库时出错

html - ReactJS 的样式问题

css - 无法摆脱蓝色高光

reactjs - Redux 使用 jest、nock、axios 和 jsdom 测试 multipart/form-data

linux - 无法获取共享库(.so) -在 Jboss 上测试 .war 文件的正确地址 API 时出现 UnsatisfiedLink 错误(Linux 环境)

java - 记录 native 代码发出的调用

reactjs - 处理连接到 Azure Cosmos DB (SQL API) 的 React 应用程序中的 "Access to fetch blocked by CORS policy"异常

javascript - 如何在 React Native 中更改原生 Picker fontSize 和 fontFamily

android - React Native Keyboard 推送标题(Android)