• <legend id='wLXYs'><style id='wLXYs'><dir id='wLXYs'><q id='wLXYs'></q></dir></style></legend>
      <bdo id='wLXYs'></bdo><ul id='wLXYs'></ul>

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

      1. <tfoot id='wLXYs'></tfoot>

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

      2. 当 value 相等时,React hooks useState setValue 仍然重新渲染一次

        时间:2023-09-07
        • <bdo id='T6p2e'></bdo><ul id='T6p2e'></ul>
        • <legend id='T6p2e'><style id='T6p2e'><dir id='T6p2e'><q id='T6p2e'></q></dir></style></legend>
            <i id='T6p2e'><tr id='T6p2e'><dt id='T6p2e'><q id='T6p2e'><span id='T6p2e'><b id='T6p2e'><form id='T6p2e'><ins id='T6p2e'></ins><ul id='T6p2e'></ul><sub id='T6p2e'></sub></form><legend id='T6p2e'></legend><bdo id='T6p2e'><pre id='T6p2e'><center id='T6p2e'></center></pre></bdo></b><th id='T6p2e'></th></span></q></dt></tr></i><div id='T6p2e'><tfoot id='T6p2e'></tfoot><dl id='T6p2e'><fieldset id='T6p2e'></fieldset></dl></div>
            <tfoot id='T6p2e'></tfoot>

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

                  <tbody id='T6p2e'></tbody>

                1. 本文介绍了当 value 相等时,React hooks useState setValue 仍然重新渲染一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  I have sample code below:

                  function App() {
                    console.log("render");
                    const [val, setVal] = React.useState(0);
                    return (
                      <div className="App">
                        <h1>{val}</h1>
                        <button onClick={() => setVal(12)}>Update with same value</button>
                      </div>
                    );
                  }
                  

                  When I click a button multiple times, the console log 3 times with 'render' message. For me, it should be 2 times only:

                  • 1 for first render

                  • 2 for the update from val 0 to 12 (when click button)

                  and since this time, it should not re-render because the same value (12) is updated to val.

                  But why it appears 3 times? That mean it still re-render one more time despite the same value was updated.

                  Anyone who know please explain this, thanks in advance.

                  P/S: I've figured out that it's only cause an extra re-render when the value changed then has been updated with the same

                  function App() {
                    console.log("render");
                    const [val, setVal] = useState(4);
                    return (
                      <div className="App">
                        <h1>{val}</h1>
                        <button onClick={() => {
                          setVal(val => val + 1)
                        }}>Update</button>
                        <button onClick={() => {
                          setVal(val => val)
                        }}>Update with same value</button>
                      </div>
                    );
                  }
                  

                  When first click on 2nd button, no re-render call, but if you click the 1st button then 2nd button, 2nd button cause 1 extra re-render

                  解决方案

                  This thread may help you : React: Re-Rendering on Setting State - Hooks vs. this.setState

                  Also, you can check the second paragraph over here which says:

                  Note that React may still need to render that specific component again before bailing out. That shouldn’t be a concern because React won’t unnecessarily go "deeper" into the tree. If you’re doing expensive calculations while rendering, you can optimize them with useMemo.

                  这篇关于当 value 相等时,React hooks useState setValue 仍然重新渲染一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:jsTree:progressive_render 与 ajax/从数组渲染节点 下一篇:HTML5 数据属性排序

                  相关文章

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

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

                      <bdo id='zOwDy'></bdo><ul id='zOwDy'></ul>
                    1. <tfoot id='zOwDy'></tfoot>
                    2. <legend id='zOwDy'><style id='zOwDy'><dir id='zOwDy'><q id='zOwDy'></q></dir></style></legend>