简体   繁体   English

React Native中未声明和React导航的问题

[英]Issue with Unstated and React Navigation in React Native

I have the function 我有功能

onPress = (store) => {
//store.flipState();
this.props.navigation.navigate('anotherScreen');
console.log('hi');
}

If I run it as above the navigation works. 如果我像上面一样运行它,导航工作。 If I uncomment the store.flipState() line the state changes but the navigation doesn't work (the screen just refreshes). 如果我取消对store.flipState()行的注释,则状态会更改,但导航将不起作用(屏幕会刷新)。 The console.log works in both cases. console.log在两种情况下均有效。

How can I change the state and navigate at the same time? 如何更改状态并同时导航?

I use Unstated and React Navigation in React Native. 我在React Native中使用Unstated和React Navigation。

Thank you. 谢谢。

I know this is really old, but what if you pass the navigate action to flipState 我知道这确实很老,但是如果您将导航动作传递给flipState会怎样?

const {navigation: {navigate}} = this.props

store.flipState(navigate('anotherScreen'))

then, in flipState, when you call setState, pass the navigate as the success action callback 然后,在flipState中,当您调用setState时,将导航作为成功操作回调传递

flipState = (callback) => {
  this.setState((state) => {
    return { flippedState: !state.flippedState };
  }, callback);
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM