【发布时间】:2020-08-14 20:22:53
【问题描述】:
我有一个 React 应用程序,它会立即(加载时)检查是否有 cookie,如果有 cookie,它会执行功能等。
但应用程序将通过重定向加载
这是目前为止的代码:
export class Landing extends Component {
constructor(props) {
super(props);
this.state = {};
}
componentDidMount() {
function onLoad(props) {
const cookieValue = Cookies.get("cookieJWT");
if(cookieValue){
console.log(cookieJWT);
} else { console.log('No cookie found!') }
}
onLoad();
}
}
问题是,onLoad() 函数在重定向的 cookie 准备好之前运行得非常快,所以返回不存在!
我曾尝试使用 componentDidMount(),但认为我需要使用 async 和 await,但我该怎么做才能等待 cookie 加载/重定向正确完成?
提前致谢:)
【问题讨论】:
标签: javascript reactjs redirect routes http-redirect