我的应用程序中有多个屏幕,我使用 CreateStackNavigator 进行导航。
如果我第二次访问屏幕,则不会调用构造函数。
假设我有四个屏幕 A、B、C 和 D,目前我位于 A。
然后我分别转到C 屏和D 屏。
现在,如果我再次单击 C,则不会调用 C 的构造函数。
我也使用了 ComponentReceivedMount() 但它不起作用。
我的代码是:-
constructor(props) {
super(props);
ToastAndroid.show('heeeelll', ToastAndroid.LONG);
this.state = {
drawerOpen: false,
op: 1,
cl: '#ffffff',
swiper: this.renderSwpier,
showSwiper: false,
ftc: '#2c3554',
stc: '#c8c8c8',
firstTopcolor: 1,
secondTopColor: 0,
showview: true,
}
}
先感谢您。
React 导航有自己的导航生命周期 api 可用,它允许您确定屏幕何时变为活动状态。
有关更多信息,请查看文档:https://reactnavigation.org/docs/en/navigation-lifecycle.html https://reactnavigation.org/docs/en/navigation-lifecycle.html
withNavigationFocus HOC 提供了 isFocused 属性,允许您确定屏幕是否可见。
Example:
import { withNavigationFocus } from 'react-navigation';
class YourScreen extends React.Component {
render() {
...
}
componentDidUpdate(prevProps) {
if (this.props.isFocused && !prevProps.isFocused) {
// Screen has now come into focus, perform your tasks here!
}
}
}
export default withNavigationFocus(YourScreen)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)