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

為了賬號安全,請及時綁定郵箱和手機立即綁定

為什么 onChange 觸發器更新后我的頁面會重新加載?

為什么 onChange 觸發器更新后我的頁面會重新加載?

達令說 2023-11-12 14:27:20
我已經用不同的方法添加了不同的表單,但是當我在輸入字段中輸入任何內容時,頁面會重新加載并保持狀態,并且我必須再次單擊該字段并輸入,然后會發生相同的循環。如果我添加所有內容作為回報,它工作正常。有人可以解釋為什么會發生這種情況以及如何阻止它嗎?我也分享一段代碼。function MyForm() {    const [commentForm, setCommentForm] = useState({        Comment: "",    });    const onCommentChange = (obj) => {        setCommentForm((prevState) => {            return {                ...prevState,                ...obj,            };        });    };    const IForm = () => (        <Table>            <CardBody>                <Row>                    <Col className="col-2">                        <Label>Comment: </Label>                    </Col>                    <Col className="col-1">                        <Input type="text"                            value={commentForm.Comment}                            onChange={(e) =>                                onCommentChange({ Comment: e.target.value })} />                    </Col>                </Row>            </CardBody>        </Table>    );    return (        <div>            <IForm />        </div>    )}export default MyForm
查看完整描述

3 回答

?
四季花海

TA貢獻1811條經驗 獲得超5個贊

那是因為您IForm在當前組件內定義為 A 組件,這是不正確的。所以你有兩個解決方案。


1 - 移出IFORM Component當前反應。


function MyForm() {

  const [commentForm, setCommentForm] = React.useState({

    Comment: ""

  });


  const onCommentChange = (obj) => {

    setCommentForm((prevState) => {

      return {

        ...prevState,

        ...obj

      };

    });

  };


  return (

    <div>

      <IForm commentForm={commentForm} onCommentChange={onCommentChange} />

    </div>

  );

}

export default MyForm;


const IForm = ({ commentForm, onCommentChange }) => (

  <Table>

        <CardBody>

            <Row>

                <Col className="col-2">

                    <Label>Comment: </Label>

                </Col>

                <Col className="col-1">

                    <Input type="text"

                        value={commentForm.Comment}

                        onChange={(e) =>

                        onCommentChange({ Comment: e.target.value })} />

                </Col>

            </Row>

        </CardBody>

  </Table>

);

2 - 將 IForm 聲明為當前組件內的普通函數。


function MyForm() {

  const [commentForm, setCommentForm] = React.useState({

    Comment: ""

  });


  const onCommentChange = (obj) => {

    setCommentForm((prevState) => {

      return {

        ...prevState,

        ...obj

      };

    });

  };


  const form = () => (

    <Table>

        <CardBody>

            <Row>

                <Col className="col-2">

                    <Label>Comment: </Label>

                </Col>

                <Col className="col-1">

                    <Input type="text"

                        value={commentForm.Comment}

                        onChange={(e) =>

                            onCommentChange({ Comment: e.target.value })} />

                </Col>

            </Row>

        </CardBody>

    </Table>

  );


  return <div> {form()} </div>;

}

export default MyForm;


查看完整回答
反對 回復 2023-11-12
?
慕的地8271018

TA貢獻1796條經驗 獲得超4個贊

原因是 IForm 組件是在 MyForm 組件內部聲明的。這意味著每當 MyForm 組件的狀態發生變化時,它都會刷新其 dom 樹。當 dom 重新渲染功能組件時,IForm 將再次執行,這就是為什么你總是會失去輸入的焦點,但永遠不會失去 MyForm 組件的狀態。

要阻止這種情況發生,請在 MyForm 組件外部聲明 IForm 組件,或者將 IForm 的 jsx 移至 MyFOrm 組件的 Return 內部。


查看完整回答
反對 回復 2023-11-12
?
慕容森

TA貢獻1853條經驗 獲得超18個贊

你應該只是setCommentForm價值。我認為你不需要傳播 prevState。

您想要實現的是將狀態值設置為新值。

還有,你沒有useEffect權利嗎?


查看完整回答
反對 回復 2023-11-12
  • 3 回答
  • 0 關注
  • 234 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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