亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在每個子掛載上更新父母的狀態,而不是在所有子掛載之后?

如何在每個子掛載上更新父母的狀態,而不是在所有子掛載之后?

浮云間 2022-10-27 15:12:14
我正在嘗試從子元素中更新父元素的狀態。我希望更新在每個子元素安裝后立即發生,因此我使用useEffect鉤子并從那里調用更新函數setData(從父級傳遞下來)。然而,似乎每個隨后被安裝的子元素都沒有注意到先前安裝的子元素對數據所做的更改。也就是說,它對一個空data數組進行操作,而對于第二個、第三個和第四個元素,該數組不應再為空。我相信這是因為setData僅在所有元素安裝后才計劃執行。這個對嗎?如果是這樣,我該如何解決這個問題? function Parent() {                        const [data, setData] = React.useState([])                                      function changeData(index, value) {                logData()                let new_data = Array.from(data)                new_data[index] = value                setData(new_data)            }                        function logData() {                console.log(data)            }                        let children = Array(4).fill(null).map((item, index) => {                return <Child id={index} changeData={changeData} />            })                        return (                <div>                    {children}                    <button onClick={logData}>Log data</button>                </div>            )        }  function Child(props) {      const ref = React.useRef(null)      React.useEffect(() => {          props.changeData(ref.current.id, ref.current.id)      }, [])      function onClickHandler(e) {          let element_id = e.target.id          props.changeData(element_id, element_id)      }      return (          <button ref={ref} id={props.id} onClick={onClickHandler}>Child</button>      )  }  ReactDOM.render(<Parent />, document.getElementById('root'))<!DOCTYPE html>    <html>        <body>            <head>                <script src="https://unpkg.com/react@^16/umd/react.production.min.js"></script>                <script src="https://unpkg.com/[email protected]/umd/react-dom.production.min.js"></script>                <script src="https://unpkg.com/[email protected]/babel.js"></script>            </head>            <div id="root"></div>        </body>    </html>
查看完整描述

1 回答

?
MM們

TA貢獻1886條經驗 獲得超2個贊

根據當前狀態更新狀態時,您應該始終使用setState. 有關詳細信息,請參閱為什么 setState 給了我錯誤的值。

如果您不使用回調版本,setState對依賴于當前狀態的連續調用將相互覆蓋,如果它們被 react 批處理,可能會導致不正確的狀態。

function changeData(index, value) {

    logData()

    setData(current => {

        // current is the current state including all previous calls to setState in this batch

        const new_data = Array.from(current);

        new_data[index] = value;

        return new_data;

    });

}

更新示例:


function Parent() {

            

            const [data, setData] = React.useState([])

                          

            function changeData(index, value) {

                logData()

                setData(current => {

                  const new_data = Array.from(current);

                  new_data[index] = value;

                  return new_data;

                });

            }

            

            function logData() {

                console.log(data)

            }

            

            let children = Array(4).fill(null).map((item, index) => {

                return <Child id={index} changeData={changeData} />

            })

            

            return (

                <div>

                    {children}

                    <button onClick={logData}>Log data</button>

                </div>

            )

        }


  function Child(props) {


      const ref = React.useRef(null)


      React.useEffect(() => {

          props.changeData(ref.current.id, ref.current.id)

      }, [])


      function onClickHandler(e) {

          let element_id = e.target.id

          props.changeData(element_id, element_id)

      }


      return (

          <button ref={ref} id={props.id} onClick={onClickHandler}>Child</button>

      )

  }


  ReactDOM.render(<Parent />, document.getElementById('root'))

<!DOCTYPE html>

    <html>

        <body>

            <head>

                <script src="https://unpkg.com/react@^16/umd/react.production.min.js"></script>

                <script src="https://unpkg.com/[email protected]/umd/react-dom.production.min.js"></script>

                <script src="https://unpkg.com/[email protected]/babel.js"></script>

            </head>

            <div id="root"></div>

        </body>

    </html>

編輯useEffect:我創建了一個帶有可能解決方案的沙箱,您的孩子不需要:



查看完整回答
反對 回復 2022-10-27
  • 1 回答
  • 0 關注
  • 98 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號