Một cách viết tắt đến khó hiểu :) (ok)

https://medium.com/@bretdoucette/understanding-this-setstate-name-value-a5ef7b4ea2b4

const onInputChange = e => {
    const { target: { name, value } } = e;
    console.log(value);
    const updatedInputs = { ...inputs, [name]: value };
    setInputs(updatedInputs);
    onScreenData.current = updatedInputs;
};
<div>
  <h2>Use refs to get the latest inputs value</h2>
  <label>Title: </label>
  <input name="title" value={inputs.title || ""} onChange={onInputChange} />
  <label>Note: </label>
  <input name="note" value={inputs.note || ""} onChange={onInputChange} />
</div>

Ví dụ:

var e = {
  target: {
    name: "Lionel",
    value: 12
  }
}
var { target: { name, value } } = e;
console.log(value);
var updatedInputs = { ...e, [name]: value };
console.log(updatedInputs);

Kết quả:

{
    "target": {
        "name": "Lionel",
        "value": 12
    },
    "Lionel": 12
}

Last updated

Was this helpful?