React native react native router flux:如何返回到前一场景强制重新加载数据?
我使用的是列表视图React native react native router flux:如何返回到前一场景强制重新加载数据?,react-native,react-native-router-flux,React Native,React Native Router Flux,我使用的是列表视图 class GroupList extends Component { componentWillMount() { this.props.fetchGroups(); 我从firebase获取了一个非常简短的元素列表 export const fetchGroups = () => { const { currentUser } = firebase.auth(); return (dispatch) => { dispatch(
class GroupList extends Component {
componentWillMount() {
this.props.fetchGroups();
我从firebase获取了一个非常简短的元素列表
export const fetchGroups = () => {
const { currentUser } = firebase.auth();
return (dispatch) => {
dispatch({
type: FETCHING_GROUPS
});
firebase.database().ref(`/users/${currentUser.uid}/groups`)
.on('value', snapshot => {
dispatch({
type: GROUPS_FETCHED,
payload: snapshot.val()
});
});
};
};
要创建一个新项目,我使用react native router flux提供的右上角按钮
<Scene
key="groupList"
component={GroupList}
title="Gruppi di servizio"
hideNavBar={false}
rightTitle="Nuovo"
onRight={() => Actions.groupForm({ formType: NEW_GROUP_FORM_TYPE })}
sceneStyle={{ paddingTop: 65 }}
/>
有什么问题吗?
第一轮投票后
Actions.groupForm({ formType: NEW_GROUP_FORM_TYPE })}
及
我的组列表已成功刷新,因为componentWillMount再次被调用
问题是,在第二轮groupForm->groupList之后,我的列表不再刷新
向componentWillMount添加控制台日志时,我注意到它不再执行
为什么?
返回场景“groupList”的正确方法是什么,不惜任何代价强制刷新?我没有使用react native router flux,但我遇到了同样的问题,希望能给您足够的信息,让您了解如何使用该库
基本上它不会再次被调用(componentWillMount),因为组件已经被挂载,它只是将焦点返回到该组件。您需要做的是将
fetchGroups()
传递到下一个场景所在的位置,并在返回到GroupList场景后再次调用它,这将导致使用最新信息重新渲染。如果我需要进一步澄清,请告诉我 我通过以下方法解决了这个问题
Actions.replace(sceneKey, props );
为您的代码
Actions.replace('groupForm', { formType: NEW_GROUP_FORM_TYPE } );
希望这有帮助 这对我很有效。试试看。 其要点是定义场景时,可以覆盖其“onBack”功能。为了强制刷新上一页,您每次都必须为其提供一个具有不同值的道具,因此Math.random()
Actions.pop({refresh:Math.random()})
/>
受@Matt解决方案的启发,我解决了我的问题。我将发布我的示例代码
这个想法是,如果您想在从子页面返回时刷新父页面,我的方法是将destroy和update回调函数传递给子页面。destroy函数确保页面返回初始状态,而update函数将获取最新数据
// parent scene
Actions.childSceneName({
onBack: () => this.props.fetchData(),
destroyParent: () => this.props.destroyPage(),
})
这样子页面可以在路由返回之前调用函数
// child scene
componentWillUnmount() {
this.props.destroyParent()
this.props.onBack()
}
<Scene
key='title'
component={YourComponent}
title='Title'
onBack={() => Actions.pop({ refresh: Math.random() })}
/>
// parent scene
Actions.childSceneName({
onBack: () => this.props.fetchData(),
destroyParent: () => this.props.destroyPage(),
})
// child scene
componentWillUnmount() {
this.props.destroyParent()
this.props.onBack()
}