根据组件状态,按向后按自然响应

问题描述 投票:0回答:1

我正在尝试在我的本机应用程序上自定义后退导航事件。基本上,根据currentSection状态变量,我在组件的主体中渲染了不同的部分。我发现,在[[onBackPress函数中的currentSection的值未得到更新,并且显然始终具有首次调用时的值(在useEffect调用期间)。

const [currentSection, setCurrentSection] = useState<ProviderDetailSections>(ProviderDetailSections.MAIN); useEffect(() => { BackHandler.addEventListener( "hardwareBackPress", onBackPress ); return () => BackHandler.removeEventListener( "hardwareBackPress", onBackPress ); }, []); function onBackPress(): boolean { console.log(`onBackPress currentSection: ${currentSection}`); if (currentSection === ProviderDetailSections.MAIN) { return false; } else { setCurrentSection(ProviderDetailSections.MAIN); return true; } }
onBackPress
中的

console.log()]始终记录同一部分(MAIN),无论实际的当前部分是什么。任何帮助将不胜感激!

react-native react-navigation
1个回答
0
投票
© www.soinside.com 2019 - 2024. All rights reserved.