添加到使用中的对象

时间:2019-11-20 08:48:16

标签: javascript reactjs

每次使用输入时,我想在listData中添加或替换元素(如果存在),但是此代码未添加元素:

 const [listInput, setlistInput] = useState();
 const [listData, setListData] = useState();

    useEffect(
      () => {
        requestInputData().then((data) => {   
            const lista = (data["input"].map((type) =>
            (
                <label>
                    {type}
                    <input type="text" name={type}  onChange={e => {
                       setListData({ ...listData, [e.target.name]: e.target.value })
                    }} /><br />
                </label>
            )
        ));
        setlistInput(lista)

        }).catch(error => {
            notification.error({
                message: APPLICATION_NAME,
                description: error.message || 'Sorry! Something went wrong. Please try again!'
            });
        });
      },

      []
    );
    console.log(listData)
    return (
      <div>
     {listInput}
      </div>
    );

1 个答案:

答案 0 :(得分:0)

好吧!!!谢谢.....

[['110, jkl'], ['777, 9kdf'], ['333, mnp1']]