简体   繁体   English

保存js文件或刷新浏览器时常量的值发生变化

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

相关问题 ReactApp-当我在VS代码编辑器中将js文件中的更改保存时,浏览器不会刷新页面。 - ReactApp - The browser does not refresh the page when i save changes in my js file in VS code editor . vuejs - 当我保存文件时,npm run dev webserver 自动刷新工作,但刷新浏览器没有 - vuejs - npm run dev webserver auto refresh works when I save a file, but refreshing the browser does not 在VI中保存文件后如何刷新Web浏览器 - How to refresh my web browser after I save a file in VI 为什么当我将 VS 代码中的代码文件保存为 .js 文件时,它不会在浏览器中运行或打开? - why is that when I save a code file in VS code as a .js file it wont run or open in a browser? 如何将JS事件更改保存到浏览器历史记录中 - How save JS event changes into browser history 如何将更改保存在js文件中? - How to save changes in a js file? 在同一浏览器上对另一个标签进行任何更改时,如何刷新标签数据 - How to refresh tab data when I m making any changes to another tab on same browser 为什么我保存更改时“webpack serve”命令不刷新 html 页面? - Why the 'webpack serve' command doesn't refresh the html page when I save the changes? Angular 4刷新 <img src={{ … }} > 当价值变化时 - Angular 4 refresh <img src={{ … }} > when the value changes Backbone.js应用程序-浏览器在源代码保存时刷新 - Backbone.js app - browser refresh on source save
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM