How do I update states onchange in an array of object in React Hooks

前端 未结 3 384
醉话见心
醉话见心 2020-12-03 00:41

I have retrieved datas stored using useState in an array of object, the datas was then outputted into form fields. And now I want to be able to update the fields (state) as

相关标签:
3条回答
  • 2020-12-03 01:30

    Here is how you do it:

    // sample datas structure
    /* const datas = [
        {
          id:   1,
          name: 'john',
          gender: 'm'
        }
        {
          id:   2,
          name: 'mary',
          gender: 'f'
        }
    ] */ // make sure to set the default value in the useState call (I already fixed it)
    
    const [datas, setDatas] = useState([
        {
          id:   1,
          name: 'john',
          gender: 'm'
        }
        {
          id:   2,
          name: 'mary',
          gender: 'f'
        }
    ]);
    
    const updateFieldChanged = index => e => {
    
        console.log('index: ' + index);
        console.log('property name: '+ e.target.name);
        let newArr = [...datas]; // copying the old datas array
        newArr[index] = e.target.value; // replace e.target.value with whatever you want to change it to
    
        setDatas(newArr); // ??
    }
    
    return (
        <React.Fragment>
            { datas.map( (data, index) => {
                  <li key={data.name}>
                    <input type="text" name="name" value={data.name} onChange={updateFieldChanged(index)}  />
                  </li>
              })
            }
        </React.Fragment>
    )
    
    0 讨论(0)
  • 2020-12-03 01:30

    This is what I do:

    const [datas, setDatas] = useState([
      {
        id: 1,
        name: "john",
        gender: "m",
      },
      {
        id: 2,
        name: "mary",
        gender: "f",
      },
    ]);
    
    const updateFieldChanged = (name, index) => (event) => {
      let newArr = datas.map((item, i) => {
        if (index == i) {
          return { ...item, [name]: event.target.value };
        } else {
          return item;
        }
      });
      setDatas(newArr);
    };
    
    return (
      <React.Fragment>
        {datas.map((data, index) => {
          <li key={data.name}>
            <input
              type="text"
              name="name"
              value={data.name}
              onChange={updateFieldChanged("name", index)}
            />
          </li>;
          <li key={data.gender}>
            <input
              type="text"
              name="gender"
              value={data.gender}
              onChange={updateFieldChanged("gender", index)}
            />
          </li>;
        })}
      </React.Fragment>
    );
    
    0 讨论(0)
  • 2020-12-03 01:35

    You can do this without mutation by mapping your old array into a new one, swapping what you want to change for an updated item along the way.

    setDatas(
        datas.map(item => 
            item.id === index 
            ? {...item, someProp : "changed"} 
            : item 
    ))
    
    0 讨论(0)
提交回复
热议问题