[英]The value of a constant changes when I save a js file or refresh the browser
I have this react component:我有这个反应组件:
import "./App.scss";
import { useState } from "react";
import { useEffect } from "react";
const quotesDbUrl =
"https://gist.githubusercontent.com/camperbot/5a022b72e96c4c9585c32bf6a75f62d9/raw/e3c6895ce42069f0ee7e991229064f167fe8ccdc/quotes.json";
function App() {
const [quotesArray, setQuotesArray] = useState(null);
const fetchQuotes = async (url) => {
const response = await fetch(url);
const parsedJSON = await response.json();
setQuotesArray(parsedJSON.quotes);
console.log(quotesArray);
};
useEffect(() => {
fetchQuotes(quotesDbUrl);
}, [quotesDbUrl]);
return (
<div></div>
);
}
export default App;
When I console.log(quotesArray)
on the fetchQuotes
function, I get null
.当我在
fetchQuotes
function 上console.log(quotesArray)
时,我得到null
。 If I edit the file, say console.log(parsedJSON.quotes)
, I get the value I expected (an array).如果我编辑文件,比如
console.log(parsedJSON.quotes)
,我会得到我期望的值(一个数组)。 I can go back to console.log(quotesArray)
and I get the same array.我可以 go 回到
console.log(quotesArray)
并且得到相同的数组。 If I refresh the browser, I get null again.如果我刷新浏览器,我会再次得到 null。
I have no idea what's going on, I just want to setQuotesArray to an array feched from the Url.我不知道发生了什么,我只想将 setQuotesArray 设置为从 Url 获取的数组。 any clues?
任何线索?
console.log(quotesArray) and console.log(parsedJSON.quotes) are different, and what you are seeing has to do with closures and asynchronous nature of setState calls. console.log(quotesArray) 和 console.log(parsedJSON.quotes) 是不同的,您所看到的与 setState 调用的闭包和异步性质有关。
When the function is first created, a closure is created around quotesArray at the time of render (before your function is called).首次创建 function 时,会在渲染时围绕引号创建闭包(在调用 function 之前)。 At this point in time, quotesArray will be null as you initialized it
在这个时间点,quotesArray 将是 null 因为你初始化它
const fetchQuotes = async (url) => {
const response = await fetch(url);
const parsedJSON = await response.json();
setQuotesArray(parsedJSON.quotes);
console.log(quotesArray);
};
is equivalent to相当于
const fetchQuotes = async (url) => {
const response = await fetch(url);
const parsedJSON = await response.json();
setQuotesArray(parsedJSON.quotes); //setting state is async, this will not be available in quotesArray until the component rerendered, and this function is recreated.
console.log(null); //quotesArray was null when the fetchQuotes closure was created
};
When the function is called the first time, and you log quotesArray, it will display null.第一次调用 function 并记录quotesArray 时,它将显示 null。 parsedJSON is a local variable which is updated within the scope of the function, hence it will display your data.
parsedJSON 是一个局部变量,它在 function 的 scope 中更新,因此它将显示您的数据。
If you want to see quotesArray updating, you can create a useEffect with this as its dependency to see this within your App component.如果您想查看quotesArray 更新,您可以创建一个useEffect 并以此作为其依赖项,以便在您的App 组件中查看它。
useEffect(() => {
console.log(`quotesArray updated: ${quotesArray}`)
},[quotesArray])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.