简体   繁体   English

map 通过输入和 setstate onchange

[英]map through inputs and setstate onchange

I am trying to return several text inputs based on the data in the state below (placementData) and then handle updating their respective state using a single onChange handler.我正在尝试根据下面 state 中的数据 (placementData) 返回几个文本输入,然后使用单个 onChange 处理程序处理更新它们各自的 state。

I can comfortably.map and return the inputs and their placeholders, values etc but I am stuck with how to carry out the onChange.我可以 comfortable.map 并返回输入及其占位符、值等,但我对如何执行 onChange 感到困惑。

I really don't want to update each one separately/我真的不想分别更新每一个/

  const [placementData, setPlacementData] = useState([
    { key: 1, position: 'Long side panel', value: 10 },
    { key: 2, position: 'Custom wrap', value: 0 },
    { key: 3, position: 'Seatback', value: 0 },
    { key: 4, position: 'Full wrap', value: 20 },
    { key: 5, position: 'Driver’s cabin', value: 0 },
    { key: 6, position: 'Stop side panel', value: 0 },
    { key: 7, position: 'Max Parade', value: 60 },
    { key: 8, position: 'Parade', value: 0 },
    { key: 9, position: 'Full rear', value: 70 },
    { key: 10, position: 'Rear panel', value: 0 },
  ]);

This is my current broken solution:这是我目前损坏的解决方案:

  const handleInputChange = (e, index) => {
    const { name, value } = e.target;
    const list = [...placementData];
    list[index][name] = value;
    setPlacementData(list);
  };

return (
  <div>
              <h4>Add Campaign Placements</h4>
              <Row>
                {placementData.map((key, i) => (
                  <Col xs={6} md={6} lg={3} key={key.position}>
                    <TextField
                      label={key.position}
                      name={key.position}
                      value={key.value}
                      // defaultValue={selected[0].company}
                      variant="outlined"
                      // onChange={handleInputChange}
                      onChange={(e) => handleInputChange(e, key, i)}
                      InputProps={
                        {
                          // readOnly: isReadOnly,
                        }
                      }
                    />
                  </Col>
                ))}
              </Row>
            </div>

I also tried:我也试过:

  // const handleInputChange = (event) => {
  //   const { value } = event.target;
  //   setPlacementData({
  //     ...placementData,
  //     [event.target.name]: value,
  //   });
  // };

and


  const handleInputChange = (event, idx) => {
    setPlacementData(() => placementData.map((item, i) => i.value));
  };

Does anyone know how I can actually complete this?有谁知道我如何才能真正完成这个?

handleInputChange(e, key, i) is how you're calling your handler, but the signature of your handler is (e, index) . handleInputChange(e, key, i)是您调用处理程序的方式,但处理程序的签名是(e, index) So, you're passing the key in where you're supposed to be passing in the index.因此,您将密钥传递到您应该传递索引的位置。

Update your handler to this and I think you should be good to go.将您的处理程序更新为此,我认为您应该对 go 满意。

 const handleInputChange = (e, index) => {
    const { name, value } = e.target;
    const list = [...placementData];

    // it's an array element, not a keyed map/object, 
    // so have to recreate the entire element
    list[index] = { ...list[index], value };
    setPlacementData(list);
  };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM