[英]React: ReferenceError: obj is not defined
我正在尝试将特定组件呈现到屏幕上,条件是我返回一个已解析的 Promise 数据为{result: "clear"}
,但是,尽管已将if
条件放置在setTimeout()
中,但我仍然得到obj is not defined
,即使 Promise 首先解析。
const useOnfidoFetch = (URL) => {
const [token, setToken] = useState();
const [id, setId] = useState();
const [isClear, setIsClear] = useState(false);
useEffect(() => {
axios
.get("http://localhost:5000/post_stuff")
.then((response) => response.data.data.data.json_data)
.then((json_data) => {
console.log("this is the json data", json_data);
const id = json_data.applicant_id;
const token = json_data.onfido_sdk_token;
setId(id);
setToken(token);
});
}, [URL]);
useEffect(() => {
if (!token) return;
console.log("this is working!");
OnfidoSDK.init({
token,
containerId: "root",
steps: [
{
type: "welcome",
options: {
title: "Open your new bank account",
},
},
"document",
],
onComplete: function (data) {
console.log("everything is complete");
console.log("this is the applicant id", id);
axios
.get("http://localhost:5000/post_id", {
applicant_id: id,
})
.then((response) => {
let obj;
obj = response.data.data.data.json_data.result;
setIsClear(true);
});
setTimeout(() => {
if (obj === clear) {
renderResult();
}
}, 3000);
},
});
}, [id, token, setIsClear]);
function renderResult() {
return <Redirect to="/result" />;
}
};
let
关键字将它们的 scope 保留在声明它们的块中:
// the `obj` can only be called inside this scope
.then((response) => {
let obj;
obj = response.data.data.data.json_data.result;
setIsClear(true);
});
参考: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let#scoping_rules
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.