简体   繁体   English

在 ReactJS 待办事项应用程序中,我正在尝试将输入字段文本 state 设置为另一个 state 对象数组

[英]In ReactJS todo app I am trying to set input field text state into another state which is array of objects

I am doing simple todo app.我正在做简单的待办事项应用程序。 I have 1 input box and Add button.我有 1 个输入框和添加按钮。 On typing into input box and clicking Add button the text is displayed in a list below.在输入框中键入并单击添加按钮时,文本将显示在下面的列表中。 I am using below constructor:我正在使用以下构造函数:

    class Entry extends Component {
        constructor() {
            super()
            this.state = {
                st_search_field: '',
                st_lists: [
                    { 
                      list_id: new Date(`enter code here`), 
                      list_name: ''
                    }
                ]
            }
         }

Now I create list_handler = () => {...... } function to set search_field text into list_name value.现在我创建list_handler = () => {...... } function 将 search_field 文本设置为 list_name 值。 I use list_handler method during onClick for Add button.我在 onClick 期间使用 list_handler 方法添加按钮。 I have started by using const join = Object.assign({}, this.state) in list_handler function and tried using this.setState({st_lists.list_name: this.state.st_searh_field}) but st_lists.list_name is marked in red in VS editor. I have started by using const join = Object.assign({}, this.state) in list_handler function and tried using this.setState({st_lists.list_name: this.state.st_searh_field}) but st_lists.list_name is marked in red in VS 编辑器。 Tried this.state.st_lists.map(li => {}) above setState method but even that gives error.在 setState 方法上尝试了this.state.st_lists.map(li => {})但即使这样也会出错。

you can't set state on one of states sub objects do it like this:您不能在状态子对象之一上设置 state 这样做:

  const st_lists = this.state.st_lists;
  st_lists.list_name = this.state.st_search_field;
  this.setState({st_lists});

st_lists.list_name is not a key, you need to add merge the object st_lists.list_name不是键,需要添加合并 object

this.setState({
    st_lists: [
        {
            ...this.state.st_lists,
            list_name: this.state.st_searh_field,
        },
    ]
})

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

相关问题 我正在尝试设置 reactjs State 动态值 - I am trying to set reactjs State dynamic value ReactJS - 用另一个数组交换状态对象数组中的所有值 - ReactJS - Swap out all the values in an array of objects in state with another array React.js-对处于状态的对象数组进行排序 - Reactjs - Sorting an array of objects in state ReactJS | 防止 state 数组在输入字段更改时更新 - ReactJS | Prevent state array from updating on input field change 设置对象数组的 state - set state of array of objects UseState, UseEffect 不更新 reactjs 中的状态。 我正在尝试将随机数添加到状态 - UseState, UseEffect not updating state in reactjs. I am trying to add random number to state 试图在 Redux ToDo 应用程序中删除 state 并且不工作 - trying to Deleting a state in a Redux ToDo app and nots working reactJS 对象的状态数组 - 如何在 setState() 中引用状态数组对象 - reactJS state array of objects - how do I reference the state array object in setState() ReactJS & Javascript: Getting “Error: Objects are not valid as a React child” when trying to set JSON Objects into a state hook - ReactJS & Javascript: Getting “Error: Objects are not valid as a React child” when trying to set JSON Objects into a state hook 如何在状态ReactJS中声明带有对象的数组 - How to declare an array with objects in the state ReactJS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM