如何在react-native上刷新/重新呈现平面列表?

时间:2019-08-31 19:52:27

标签: react-native refresh render

im试图从某个页面刷新我的平面列表而不返回主菜单,但这不起作用。

我已经阅读了ExtraData,但是它也不起作用。

大概我的程序是这样的: 我有一个名为“密码” 的页面,并且从另一个名为“添加密码” 的页面中添加了一些密码。当我单击添加密码时,我想从“密码” 页面刷新清单,以向我显示我刚刚添加的密码。

这是我在“添加密码”

页面上的代码
...

state = {
arr: [],
local: '',
password: '',
obj: {
  local: '',
  password: ''
},
count: 1,
texto: ''
};

componentDidMount() {
//Here is the Trick
const { navigation } = this.props;
//Adding an event listner om focus
//So whenever the screen will have focus it will set the state to zero
this.focusListener = navigation.addListener('didFocus', () => {
  this.setState({ count: 0 });
});
}

storeItem(item) {
try {
  //we want to wait for the Promise returned by AsyncStorage.setItem()
  //to be resolved to the actual value before returning the value~
  console.log(item)
  var joined = this.state.arr.concat(item);
  console.log(joined)

  this.setState({ arr: joined })

  AsyncStorage.setItem('array', JSON.stringify(joined));
  console.log(this.state.arr)

} catch (error) {
  console.log(error.message);
}
}

 componentWillMount() {
 AsyncStorage.getItem('array').then(array => {
  item = JSON.parse(array)
  array ? this.setState({ arr: item }) : null;
  console.log(item)


})
}



render() {

return (

  <View style={styles.container}>
    <TextInput
      style={{ height: 40, borderColor: 'gray', borderWidth: 1 }}
      onChangeText={(text) => this.setState({ local: text })}
      value={this.state.local}
    />

    <TextInput
      secureTextEntry={true}
      style={{ height: 40, borderColor: 'gray', borderWidth: 1 }}
      onChangeText={(text) => this.setState({ password: text })}
      value={this.state.password}
    />


    <Button title='Adicionar'
      onPress={() => this.storeItem({ local: this.state.local, password: this.state.password }) + alert("Adicionado com sucesso!") + this.props.navigation.navigate('Passwords')}      
    ></Button>

  </View>

);
}
}

这是我要刷新的页面“密码”

componentWillMount() {
const { navigation } = this.props;
this.willFocusListener = navigation.addListener(
  'willFocus',
  () => {
    this.setState({ count: 10 })
  }
)

AsyncStorage.getItem('array').then(array => {
  item = JSON.parse(array)

  item ? this.setState({ arr: item }) : null;
  console.log(this.state.arr)
})

}

 renderItem = ({ item }) => (
<View style={{ flexDirection: 'row' }} style={styles.passwordContainer}>
  <Text> {item.local} </Text>
  <Text> {item.password} </Text>
</View>

render() {
return (
  <View style={styles.container}>
    <FlatList
      data={this.state.arr}
      renderItem={this.renderItem}
      extraData={this.state} //this is what i tryied
    />
  </View>
);

3 个答案:

答案 0 :(得分:0)

  

当前,您不更改父组件的状态,而是要将父组件的状态从child更改为在parent和paass中定义一个处理程序函数,将child定义为prop并在需要的地方从child调用该函数

答案 1 :(得分:0)

任何状态更改都会重新提交项目。

答案 2 :(得分:0)

您可以使用侦听器来更新状态。

componentWillMount() {
  this.willFocusListener = navigation.addListener(
   'willFocus',
   () => this.updateData()
}

updateData = () =>  {
  this.setState({ count: 10 });
  AsyncStorage.getItem('array').then(array => {
    item = JSON.parse(array)
    item ? this.setState({ arr: item }) : null;
    console.log(this.state.arr)
  });
}