简体   繁体   English

setState of address array inside data object

[英]setState of address array inside data object

I am new to React, and I am currently working on an app using the MERN stack and was looking for a bit of advice.我是 React 的新手,我目前正在使用 MERN 堆栈开发一个应用程序,并且正在寻找一些建议。

I have React User component that calls & renders a user object with embedded documents from mongodb.我有 React User 组件,它使用来自 mongodb 的嵌入式文档调用并呈现用户 object。 When I return the data from the database, it does so in the following format:当我从数据库返回数据时,它采用以下格式:

{
  firstName: "joe",
  lastName: "smith",
  address: [
      {
        street: "123 Street",
        city: "UpCity",
      },
   ],
};

I have set the address up as an embedded document/array of addresses in mongo, and need to keep this format.我已将地址设置为 mongo 中的嵌入式文档/地址数组,并且需要保持这种格式。

On my user component, I am displaying the returned values text controls, which I want the user to be able to edit, update and submit back to the DB.在我的用户组件上,我正在显示返回值文本控件,我希望用户能够编辑、更新并提交回数据库。 I have been successful in getting the object data to update, but editing the array, within the object has left me stumped for days now.我已经成功地更新了 object 数据,但是在 object 中编辑数组已经让我难过了好几天。 Ideally wanted to pass the user object back in the same format so I didn't have to deconstruct the "req.body" and map this to the corresponding fields - unless this is the only way this can be done.理想情况下,希望以相同的格式将用户 object 传递回,因此我不必将“req.body”和 map 解构到相应的字段 -除非这是唯一可以完成的方法。

I have no issue with updating the state of the "user" fields but can't seem to figure out how to update the state of the address data, held in the embedded array.我对更新“用户”字段的 state 没有问题,但似乎无法弄清楚如何更新保存在嵌入式数组中的地址数据的 state。 Below is an excerpt of my code下面是我的代码的摘录

  const [data, setData] = useState({
    firstName: "",
    lastName: "",
    address: [
      {
        street: "",
        city: "",
      },
    ],
  });

  const onChange = (e) => {
    setData({
      ...data,
      [e.target.name]: e.target.value,
    });
  };

return ( 
         <div> 
                        <TextField
                          type="text"
                          name="firstName"
                          value={data.firstName}
                          onChange={onChange}
                        />
                        <TextField
                          type="text"
                          name="lastName"
                          value={data.lastName}
                          onChange={onChange}
                        />
                      </div>
                       <TextField
                        type="text"
                        name="street"
                        value={data.address[0].street}
                        onChange={onChange}
                      />
                      <TextField
                        type="text"
                        name="city"
                        value={data.address[0].city}
                        onChange={onChange}
                      />
)

Would you have any advice?你有什么建议吗?

Should I simply manage the data as a simple object and map the embedding to an array, before submitting it to the DB, or am I missing something really simple?在将数据提交给数据库之前,我是否应该简单地将数据作为简单的 object 和 map 嵌入到数组中,还是我错过了一些非常简单的东西?

note: I'm using mongoose & axios also注意:我也在使用 mongoose & axios

here is the simplest solution这是最简单的解决方案

    const onChange = (e, type=false) => {
    if(type){
        data.address[0][e.target.name] = e.target.value
        setData({...data})
    }else{
        setData({
            ...data,
            [e.target.name]: e.target.value,
        });
    }
};

return (
    <div>
        <TextField
            type="text"
            name="firstName"
            value={data.firstName}
            onChange={(e)=>onChange(e)}
        />
        <TextField
            type="text"
            name="lastName"
            value={data.lastName}
            onChange={(e)=>onChange(e)}
        />

        <TextField
            type="text"
            name="street"
            value={data.address[0].street}
            onChange={(e)=>onChange(e, true)}
        />
        <TextField
            type="text"
            name="city"
            value={data.address[0].city}
            onChange={(e)=>onChange(e, true)}
        />
    </div>
)

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

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