更改状态后重新读取屏幕
我对usestate有问题,事实是,在状态更改之后,它不会恢复屏幕:
例如,当用户登录时,我通过我的API获取cookie,但是主页没有自动加载自动,我需要在我的vscode文件中执行ctrl + s以“更新”状态更改,从而更改页面 同样的问题如果用户要断开连接,则用户必须将页面更改为“更新”状态,或者我必须执行ctrl + s
app.js
const AuthStack = () => {
const [isLoggedIn, setIsLoggedIn] = useState(null);
//Method to check if the session cookie exists on the device
checkSessionCookie().then((isLoggedIn) => setIsLoggedIn(isLoggedIn));
return (
<Stack.Navigator initialRouteName="HomeScreen">
{isLoggedIn ? (
<>
<Stack.Screen
name="TabNavigator"
component={TabNavigator}
options={{
headerShown: false,
}}
/>
</>
) : (
<>
<Stack.Screen
name="HomeScreen"
component={HomeScreen}
options={{
headerShown: false,
}}
/>
<Stack.Screen
name="LoginScreen"
component={LoginScreen}
options={{
headerShown: false,
}}
/>
<Stack.Screen
name="RegisterScreen"
component={RegisterScreen}
options={{
headerShown: false,
}}
/>
</>
)}
</Stack.Navigator>
);
};
I have a problem with useState and the fact that it doesn't rerender the screen after a state change :
For example, when the user logs in, I get the cookie via my api, but the main page doesn't load automaticaly, I need to do a ctrl + s in my vscode file to "update" the state change and thus change the page
Same problem if the user wants to disconnect, the user must either change the page to "update" the state, or I must do a ctrl + s
App.js
const AuthStack = () => {
const [isLoggedIn, setIsLoggedIn] = useState(null);
//Method to check if the session cookie exists on the device
checkSessionCookie().then((isLoggedIn) => setIsLoggedIn(isLoggedIn));
return (
<Stack.Navigator initialRouteName="HomeScreen">
{isLoggedIn ? (
<>
<Stack.Screen
name="TabNavigator"
component={TabNavigator}
options={{
headerShown: false,
}}
/>
</>
) : (
<>
<Stack.Screen
name="HomeScreen"
component={HomeScreen}
options={{
headerShown: false,
}}
/>
<Stack.Screen
name="LoginScreen"
component={LoginScreen}
options={{
headerShown: false,
}}
/>
<Stack.Screen
name="RegisterScreen"
component={RegisterScreen}
options={{
headerShown: false,
}}
/>
</>
)}
</Stack.Navigator>
);
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用效果,因为当组件渲染时,
第一个状态将初始化并执行返回代码,然后使用使用效率
bu这个代码应该像
Use useEffect, because when components render
The first state gets initialized and the return code is executed and then useEffect is called
bu this your code should be like