[英]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.