简体   繁体   English

如何从 React 中的动态形式在 state arrays 中设置 object

[英]How to set object in state arrays from dynamic form in React

I am building a react app.我正在构建一个反应应用程序。 I have created a dynamic form (generates X number of input fields (1-10) based on the number chosen by the user).That part works perfectly.我创建了一个动态表单(根据用户选择的数字生成 X 个输入字段(1-10))。这部分工作得很好。 However, I am wanting to add a second input field inside my mapping function.但是,我想在我的映射 function 中添加第二个输入字段。 For context, I am building a flash card app.对于上下文,我正在构建一个 flash 卡应用程序。 I want to add the term and the definition to the same state item.我想将术语和定义添加到同一个 state 项目中。 The idea is that each state array will hold an object for the term and an object for the definition in order to keep each definition with its proper term.这个想法是每个 state 数组将包含一个 object 用于该术语,一个 object 用于定义,以使每个定义都具有其正确的术语。 Notice that the state items are named after the index of my mapping function.请注意,state 项目以我的映射 function 的索引命名。 So for example, I want to send the term and definition to this.state.0 for the first term, etc... for each term and definition depending on how many the user wants to add (up to 10).因此,例如,我想将第一个术语的术语和定义发送到 this.state.0 等……每个术语和定义取决于用户想要添加的数量(最多 10 个)。 Right now I'm getting the following error:现在我收到以下错误:

TypeError: Cannot read property 'term' of undefined

So how do I set each term and its definition to the state array, each to their own object of the array?那么如何将每个术语及其定义设置为 state 数组,每个都设置为自己的 object 数组? Here is my component:这是我的组件:

import React, { Component } from 'react'

export class TermForm extends Component {
    state = {
        0: [],
        1: [],
        2: [],
        3: [],
        4: [],
        5: [],
        6: [],
        7: [],
        8: [],
        9: []
    }

    onChange = (event) => this.setState({ [event.target.name]: event.target.value});

    render() {
        console.log(this.props.numberOfTerms);
        return (
            this.props.numberOfTerms.map((i) => (
                <div key={i}>
                    <input type="text" name={i.term} placeholder="TERM" value={this.state.i.term} onChange={this.onChange}></input>
                    <input type="text" name={i.def} placeholder="TERM" value={this.state.i.def} onChange={this.onChange}></input>
                </div>
            ))
        )
    }
}

export default TermForm

Any ideas about how I could set the object of a state array through my mapping function?关于如何通过我的映射 function 设置 state 数组的 object 的任何想法? Thanks in advance for any assistance.提前感谢您的任何帮助。

Cannot read property 'term' of undefined coz there is no such field on that location Cannot read property 'term' of undefined因为该位置没有这样的字段

value={this.state.i.term}

As per your initial state, it should look like this:根据您最初的 state,它应该如下所示:

value={this.state.i[0]?.term}

The same change will be applicable for your def field also同样的更改也适用于您的def字段


Suggestion:建议:

You should change the state something like this:您应该像这样更改 state :

state = [
    {
        term : "" ,
        def : ""
    },
    {
        term : "" ,
        def : ""
    },
    ...
]

Then you can use it like this:然后你可以像这样使用它:

value={this.state[i].term}

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

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