• <bdo id='cE5q2'></bdo><ul id='cE5q2'></ul>
      1. <legend id='cE5q2'><style id='cE5q2'><dir id='cE5q2'><q id='cE5q2'></q></dir></style></legend>
        <i id='cE5q2'><tr id='cE5q2'><dt id='cE5q2'><q id='cE5q2'><span id='cE5q2'><b id='cE5q2'><form id='cE5q2'><ins id='cE5q2'></ins><ul id='cE5q2'></ul><sub id='cE5q2'></sub></form><legend id='cE5q2'></legend><bdo id='cE5q2'><pre id='cE5q2'><center id='cE5q2'></center></pre></bdo></b><th id='cE5q2'></th></span></q></dt></tr></i><div id='cE5q2'><tfoot id='cE5q2'></tfoot><dl id='cE5q2'><fieldset id='cE5q2'></fieldset></dl></div>

      2. <tfoot id='cE5q2'></tfoot>

        <small id='cE5q2'></small><noframes id='cE5q2'>

        使用 JSON 数据反应更新状态变量

        React update state variable with JSON data(使用 JSON 数据反应更新状态变量)
          <tbody id='xyM2E'></tbody>

            • <bdo id='xyM2E'></bdo><ul id='xyM2E'></ul>
                <i id='xyM2E'><tr id='xyM2E'><dt id='xyM2E'><q id='xyM2E'><span id='xyM2E'><b id='xyM2E'><form id='xyM2E'><ins id='xyM2E'></ins><ul id='xyM2E'></ul><sub id='xyM2E'></sub></form><legend id='xyM2E'></legend><bdo id='xyM2E'><pre id='xyM2E'><center id='xyM2E'></center></pre></bdo></b><th id='xyM2E'></th></span></q></dt></tr></i><div id='xyM2E'><tfoot id='xyM2E'></tfoot><dl id='xyM2E'><fieldset id='xyM2E'></fieldset></dl></div>

                  <small id='xyM2E'></small><noframes id='xyM2E'>

                  <legend id='xyM2E'><style id='xyM2E'><dir id='xyM2E'><q id='xyM2E'></q></dir></style></legend>
                1. <tfoot id='xyM2E'></tfoot>
                  本文介绍了使用 JSON 数据反应更新状态变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  App.js:

                  function App() {
                    const [items, setItems] = useState([]);
                    useEffect(() => {
                      const searchDB = () => {
                        fetch("http://127.0.0.1:8443/subColumns/5/?key=fc257229-8f91-4920-b71f-885403114b35", {
                          mode: 'cors',
                          credentials: 'include'
                        })
                          .then(res => res.json())
                          .then((json) => {
                            setItems(json);
                          })
                    console.log({items});
                      }
                      searchDB();
                    }, [])
                  

                  我需要将 json 响应保持在状态变量中,因为在未来,API 请求将不会被硬编码,我希望用户会在不刷新的情况下发出多个 API 请求,结果必须映射到不同的组件.目前,尝试将 {items} 打印到控制台会返回一个空数组.

                  I need to keep the json response in a state varibale because in the future, the API request will nt be hard coded and I expect the user will make multiple API requests without refreshing, and the results will have to be mapped to different components. At the moment, trying to print {items} to the console returns an empty array.

                  推荐答案

                  由于 setItems 是异步方法,所以无法在 setItems 之后立即获取更新的值.您应该使用另一个具有依赖关系的 useEffect 来查看该值.

                  Since setItems is the asynchronous method, you can't get the updated value immediately after setItems. You should use another useEffect with dependency to see the value.

                  useEffect(() => {
                    console.log(items);
                  }, [items]);
                  

                  这篇关于使用 JSON 数据反应更新状态变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会
                  问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转
                  Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
                  quot;Each child in an array should have a unique key propquot; only on first time render of page(“数组中的每个孩子都应该有一个唯一的 key prop仅在第一次呈现页面时)
                  CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)
                  Ordinals in words javascript(javascript中的序数)

                    1. <tfoot id='17vqJ'></tfoot>
                    2. <small id='17vqJ'></small><noframes id='17vqJ'>

                        <tbody id='17vqJ'></tbody>

                      <legend id='17vqJ'><style id='17vqJ'><dir id='17vqJ'><q id='17vqJ'></q></dir></style></legend>
                        <bdo id='17vqJ'></bdo><ul id='17vqJ'></ul>

                          • <i id='17vqJ'><tr id='17vqJ'><dt id='17vqJ'><q id='17vqJ'><span id='17vqJ'><b id='17vqJ'><form id='17vqJ'><ins id='17vqJ'></ins><ul id='17vqJ'></ul><sub id='17vqJ'></sub></form><legend id='17vqJ'></legend><bdo id='17vqJ'><pre id='17vqJ'><center id='17vqJ'></center></pre></bdo></b><th id='17vqJ'></th></span></q></dt></tr></i><div id='17vqJ'><tfoot id='17vqJ'></tfoot><dl id='17vqJ'><fieldset id='17vqJ'></fieldset></dl></div>