第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何從 e.target.name 更新狀態(tài)中的對(duì)象

如何從 e.target.name 更新狀態(tài)中的對(duì)象

慕桂英3389331 2021-11-12 10:43:40
我正在嘗試從這樣的表單輸入數(shù)據(jù) -<h1>Company Position</h1><input    name="company.position"    type="text"    onChange={(e) => functions.setData(e)}    value={data.company.position}/>進(jìn)入這樣的狀態(tài)對(duì)象 -const [ form, setValues ] = useState({    name        : 'Jo Smith',    email       : 'JoSmith@domain.com',    phone       : null,    company     : {        name     : null,        position : null    }});使用我傳入目標(biāo)的 handleStateChange 函數(shù)const handleStateChange = (e) => {    e.preventDefault();    setValues({        ...form,        [e.target.name]: e.target.value    });};我似乎無法更新狀態(tài)內(nèi)部的公司對(duì)象,我認(rèn)為它會(huì)將 company.name 識(shí)別為目標(biāo)名稱。任何幫助,將不勝感激。
查看完整描述

2 回答

?
幕布斯6054654

TA貢獻(xiàn)1876條經(jīng)驗(yàn) 獲得超7個(gè)贊

e.target.name是company.position,您不能設(shè)置嵌套屬性,例如obj["company.position"],您必須將其拆分:


<input

  name="company.position"

  type="text"

  onChange={e => functions.setData(e)}

  value={data.company.position}

/>;


const handleStateChange = e => {

  e.preventDefault();

  const [section, key] = e.target.name.split(".");


  // section is : company

  // key is : position


  if (key) {

    // if you have nested keys to update

    setValues({

      ...form,

      [section]: {

        ...form[section],

        [key]: e.target.value

      }

    });

  } else {

    // if you're updating on the first level

    setValues({

      ...form,

      [section]: e.target.value

    });

  }

};


查看完整回答
反對(duì) 回復(fù) 2021-11-12
?
搖曳的薔薇

TA貢獻(xiàn)1793條經(jīng)驗(yàn) 獲得超6個(gè)贊

const nested = e.target.name.split(".");

    const [section, key] = nested; 

     if(nested.length > 2){

        let total = nested.length;

        let ultimo = nested[total-1];

        saveclinicHistory({

            ...clinicHistory,

            [section]: {//patient

                ...clinicHistory[section],

                [key]: {//address

                    ...clinicHistory[section][key],

                    [ultimo]:e.target.value


                }

            }

        });


查看完整回答
反對(duì) 回復(fù) 2021-11-12
  • 2 回答
  • 0 關(guān)注
  • 250 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)