【问题标题】:The functions called inside .then(data=> - will disappear within few seconds in react.then(data=> - 内部调用的函数将在几秒钟内消失
【发布时间】:2021-01-19 02:38:01
【问题描述】:
enter image description here我在提交注册按钮时尝试从服务器获取数据。提交按钮时,应将其定向到下一页。我从服务器得到正确的响应。但是,当我调用函数或 setState 函数时,它会指向下一页,但它会在几秒钟内消失。如果我做了 console.log(data) 它将在控制台中显示数据几秒钟。这是我的获取代码。
【问题讨论】:
标签:
reactjs
react-native
react-redux
【解决方案1】:
then(response => {response.json()}.then(data => //.....)
你只能在 promise 上调用 then,这意味着 then(response =>{response.json()} 必须这样做
a.) 返回一些东西,目前是void,因为你的括号没有明确的return 语句。
b.) 让某事成为一个承诺,然后可以跟一个then 子句。
.then(response => Promise.resolve(response.json()).then(data => //.....)