The problem is that initial value of isLoggedIn is falsey, which triggers the redirect (before the state is updated asynchronously):isLoggedIn ? (<Comp {...props} />) : (<Redirect to="/" />)您需要的是加载"、登录成功"和登录失败"的 3 个状态.What you need is 3 states for 'loading', 'login succes' and 'login fail'.具有 2 个布尔变量的示例:Example with 2 boolean variables:state = { isLoading: true, isLoggedIn: false, user: {}}... this.setState({ isLoading: false, isLoggedIn: false })... isLoggedIn ? (<Comp {...props} />) : (isLoading ? 'Loading...' : <Redirect to="/" />)(如果你喜欢更少的变量,同样可以用初始状态 isLoggedIn: null 和条件 isLoggedIn === null 来实现)(the same can be achieved with initial state isLoggedIn: null and condition isLoggedIn === null if you prefer fewer variables) 这篇关于REACT - 在渲染 APP 之前检查身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-16 04:19