React native 在react本机导航中使用AsyncStorage时,initialRouteName未正确使用
我需要根据react native navigation中的AsyncStorage值进行导航。但它总是把我带到登录页,即使我在异步存储中有值 低于我迄今为止的尝试React native 在react本机导航中使用AsyncStorage时,initialRouteName未正确使用,react-native,react-native-android,react-native-navigation,React Native,React Native Android,React Native Navigation,我需要根据react native navigation中的AsyncStorage值进行导航。但它总是把我带到登录页,即使我在异步存储中有值 低于我迄今为止的尝试 function App(props) { const [storeId, setStoreId] = useState(null); AsyncStorage.getItem(STORAGE_KEYS.LOCATION_ID).then((id) => { setStoreId(id); });
function App(props) {
const [storeId, setStoreId] = useState(null);
AsyncStorage.getItem(STORAGE_KEYS.LOCATION_ID).then((id) => {
setStoreId(id);
});
const initialRoute = storeId && 'NfcScreen';
return (
<Provider store={store}>
<NavigationContainer>
<Stack.Navigator
initialRouteName={initialRoute}
screenOptions={!isTablet ? styles : ''}>
{isTablet && (
<Stack.Screen
options={{headerShown: false}}
name={appPages.locationIdentification}
component={Locationidentification}
/>
)}
<Stack.Screen
name={appPages.nfc}
component={NfcPage}
options={{headerShown: false}}
/>
</Stack.Navigator>
</NavigationContainer>
</Provider>
功能应用程序(道具){
const[storeId,setStoreId]=useState(null);
AsyncStorage.getItem(存储密钥.位置ID)。然后((ID)=>{
设置存储id(id);
});
const initialRoute=storeId&“NfcScreen”;
返回(
{isTablet&&(
)}
我注意到第一个状态返回null,然后返回asyncstorage值。因为状态更新缓慢,locationIdentification页面被呈现。有什么方法可以尝试吗
这将是一个非常有用的帮助。谢谢:)每个设置状态操作都会重新呈现页面,因此您需要访问最新状态。
我认为您应该将AsyncStorage代码置于usseEffect中,并将initialRouteName值定义为一个函数我也这样做了,但没有解决问题@Jamal:(根据其名称,AsyncStorage有一个小延迟,因为您在路由后更新状态,应该将其保留到状态更新为止