Reactjs 反应本机刷新数据
当用户重新访问屏幕时,我试图刷新一些数据。我使用其他地方的方式,而且很有效。但我想不出为什么这个屏幕上不会显示Reactjs 反应本机刷新数据,reactjs,react-native,Reactjs,React Native,当用户重新访问屏幕时,我试图刷新一些数据。我使用其他地方的方式,而且很有效。但我想不出为什么这个屏幕上不会显示 componentDidMount = () => { this.props.navigation.addListener('didFocus', this.handleDidFocus) } async handleDidFocus() { ... } 这就是我第一次加载数据的方式,当用户再次访问时,我希望再次加载数据 componentWillMount() {
componentDidMount = () => {
this.props.navigation.addListener('didFocus', this.handleDidFocus)
}
async handleDidFocus() {
...
}
这就是我第一次加载数据的方式,当用户再次访问时,我希望再次加载数据
componentWillMount() {
this.getGroupAccepted();
}
async getGroupAccepted() {
if (this.state.token == null) {
var token = await AsyncStorage.getItem("token");
this.setState({ "token": token });
}
fetch('https://.../api/group/getActive', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
token: this.state.token
})
})
.then(response => response.json())
.then((data) => {
this.setState({
groups_accepted: data.groups_active,
loading: false,
});
})
.catch((error) => {
console.error(error);
});
}
这就是成功的原因。现在,当用户重新访问屏幕时,它会再次加载数据
componentDidMount = () => {
this.props.navigation.addListener('didFocus', this._handleDataChange)
}
_handleDataChange = () => {
this.getGroupAccepted();
}
您在哪里调用getGroupAccepted?您是否尝试将
addListener
函数移到componentDidMount()
之外?@AdrianLineweaver我在componentWillMount(){this.getGroupAccepted();}@UzairA中调用它。我应该将其移动到哪里?尝试移动到componentDidMount