反应通行处理屏幕状态
我有一个如下所示的导航组件。我想根据一些状态变量(isLoading、isAuthenticated、isOnboarded)设置导航屏幕。然而,当前的设置给了我一个错误:
错误:重新渲染次数过多。 React 将渲染次数限制为 防止无限循环。
我知道这种情况正在发生,因为一旦调用此函数导致重新渲染,我就会立即设置状态,但我不确定如何更改它以实现我想要的。
then替换该行
const [screens, setScreens] = useState();
我考虑用
let screen;
,而不是在任何地方使用 setScreen() ,我将 screen =
但是这也不起作用,因为有时状态(isLoading、isAuthenticated、isOnboarded)不满足任何条件(例如,当在过渡到注销的过程中)在这种情况下,我希望屏幕保持其先前的状态,但它被设置为未定义,这会引发错误。
const Navigation = () => {
const { isLoading, isAuthenticated, isOnboarded } = useGlobalContext();
const [screens, setScreens] = useState();
// Splash screen while app loading
if (isLoading)
setScreens(
<>
<Stack.Screen
name="Splash"
component={SplashScreen}
/>
</>
);
// Authentication screens while user not authenticated
else if (!isLoading && !isAuthenticated && isOnboarded == -1)
setScreens(
<>
<Stack.Screen
name="Register"
component={RegisterScreen}
/>
<Stack.Screen
name="Login"
component={LoginScreen}
options={{ animationEnabled: false }}
/>
</>
);
// Onboarding screens while user authenticated but not onboarded
else if (!isLoading && isAuthenticated && !isOnboarded)
setScreens(
<>
<Stack.Screen
name="OnboardingBirthday"
component={OnboardingBirthdayScreen}
/>
<Stack.Screen
name="OnboardingGender"
component={OnboardingGenderScreen}
/>
</>
);
// All other screens while user authenticated and onboarded
else if (!isLoading && isAuthenticated && isOnboarded === true)
setScreens(
<>
<Stack.Screen name="Home" component={TabNavigator} />
<Stack.Screen name="Product" component={ProductScreen} />
</>
);
return (
<NavigationContainer theme={Theme}>
<Stack.Navigator headerMode="none">{screens}</Stack.Navigator>
</NavigationContainer>
);
};
I have a navigation component that looks like this. I want to set the navigation screens based on some state variables (isLoading, isAuthenticated, isOnboarded). However this current setup gives me an error saying:
Error: Too many re-renders. React limits the number of renders to
prevent an infinite loop.
I understand this is happening because I am setting the state right away as soon as this function is invoked causing a re-render, but I'm not sure how I can change this to implement what I want.
I considered replacing the line
const [screens, setScreens] = useState();
with
let screen;
Then instead of having setScreen() everywhere I would have screen =
However this did not work either since there are times when the states (isLoading, isAuthenticated, isOnboarded) don't satisfy any of the conditions (eg when in transition to signing out) in which case I would want screens to keep its previous state but it is instead set to undefined which throws an error.
const Navigation = () => {
const { isLoading, isAuthenticated, isOnboarded } = useGlobalContext();
const [screens, setScreens] = useState();
// Splash screen while app loading
if (isLoading)
setScreens(
<>
<Stack.Screen
name="Splash"
component={SplashScreen}
/>
</>
);
// Authentication screens while user not authenticated
else if (!isLoading && !isAuthenticated && isOnboarded == -1)
setScreens(
<>
<Stack.Screen
name="Register"
component={RegisterScreen}
/>
<Stack.Screen
name="Login"
component={LoginScreen}
options={{ animationEnabled: false }}
/>
</>
);
// Onboarding screens while user authenticated but not onboarded
else if (!isLoading && isAuthenticated && !isOnboarded)
setScreens(
<>
<Stack.Screen
name="OnboardingBirthday"
component={OnboardingBirthdayScreen}
/>
<Stack.Screen
name="OnboardingGender"
component={OnboardingGenderScreen}
/>
</>
);
// All other screens while user authenticated and onboarded
else if (!isLoading && isAuthenticated && isOnboarded === true)
setScreens(
<>
<Stack.Screen name="Home" component={TabNavigator} />
<Stack.Screen name="Product" component={ProductScreen} />
</>
);
return (
<NavigationContainer theme={Theme}>
<Stack.Navigator headerMode="none">{screens}</Stack.Navigator>
</NavigationContainer>
);
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您尝试过这种方法吗?
另外,您可以尝试在单独的文件中为每个文件中的每个功能都使用React组件,而不是将所有函数使用。
Did you try such approach?
Alternatively, instead of having all functions in one file, you could try to use React component for each of them in separate files.