我试着用下面的代码将数据动态地添加到我的平面列表中:
import React, { Component } from 'react';
import { View, Text,FlatList,Button,Alert} from 'react-native';
export default class Lista extends Component {
constructor(props) {
super(props);
this.state = {dados: [
{key:"valeu 1"},
{key:"value 2"}
refresh : false
adicionar(){
this.state.dados.push({key:"value 3"})
this.state.refresh = !this.state.refresh
render() {
return (
<FlatList
data= {this.state.dados}
extraData = {this.state.refresh}
renderItem={({item}) => <Text>{item.key}</Text>}
<Button title="Adicionar"
onPress={this.adicionar.bind(this)}>
</Button>
</View>
}
我可以使用
this.state.dados.push({key:"value 3"})
将数据推送到"dados“对象,但是当我将
this.state.refresh
变量切换为true时,Flatlist不会呈现最后一个值。
提前感谢
发布于 2018-12-06 04:44:56
你不应该直接改变状态。尝试调用
this.setState()
函数,以便执行更好的应用程序开发。这可能是为什么Flatlist没有被重新渲染的原因。
adicionar(){