简体   繁体   English

插入值<input>进入 object -ReactJS

[英]Insert value of an <input> into an object -ReactJS

I am working on an app and in this section the admin creates categories for the website via a form.我正在开发一个应用程序,在本节中,管理员通过表单为网站创建类别。 On one section I need to get back the names of the categories in 3 different languages.在一个部分,我需要取回 3 种不同语言的类别名称。

I need to get the states of the <input> for each language here is the state:我需要获取每种语言的<input>状态,这里是 state:

export class Partner extends Component {
  state = {
    name: "",
    display_name:{
      en:"",
      fr:"",
      ar:""
    },        
    is_active: Boolean,
  };

As you can see I store the strings from the inputs into the display_name object (that I will need for some CRUD afterwards).如您所见,我将输入中的字符串存储到display_name object 中(之后我需要一些 CRUD)。

Here is the div from the form where I get the display name in English from the user这是我从用户那里获得英文显示名称的表单中的 div

<div className="row">
  <div className="input-field col s12">
    <input
      name="display_name"
      value={this.state.display_name.en} ///How do I set the state of display_name in "en" ?
      onChange={e => this.handleChange(e)}
      id="display_name"
      autoComplete="off"
    />
    <label className="active">
      Display_name of the category in English
    </label>
  </div>
</div>

How do I setState "en" in display_name?如何在 display_name 中设置状态“en”?

It would be nice to see what you've tried for your handleChange function, to see if you're close or not:)很高兴看到你为你的handleChange function 尝试了什么,看看你是否接近:)

However, here's how you can set the en property inside the display_name object in your state, using your current handleChange method:但是,您可以使用当前的handleChange方法在 state 中的display_name object 中设置en属性:

handleChange = (e) => {
    this.setState({
        display_name: {
            // es6 'spread' operator to copy the current properties of
            // -> this.state.display_name into a new object...
            ...this.state.display_name,
            // here, update the new value of 'en'
            en: e.target.value
        }
    })
}

Note : The above method is not very dynamic, so you may want to change the implementation to something like this:注意:上面的方法不是很动态,所以你可能想把实现改成这样:

handleLanguageChange = (e, language) => {
    this.setState({
        display_name: {
            // still copy old 'display_name' values like before
            ...this.state.display_name,
            // now update the language based off 'language' property
            [language]: e.target.value
        }
    })
}

This second option allows you to reuse handleLanguageChange with input elements of different languages.第二个选项允许您使用不同语言的input元素重用handleLanguageChange You would use it in your JSX like this:你可以像这样在你的 JSX 中使用它:

<input
    name="display_name_en"
    value={this.state.display_name.en}
    onChange={e => this.handleLanguageChange(e, 'en')}
    id="display_name_en"
    autoComplete="off"
/>
<input
    name="display_name_fr"
    value={this.state.display_name.fr}
    onChange={e => this.handleLanguageChange(e, 'fr')}
    id="display_name_fr"
    autoComplete="off"
/>
<!-- and so on... -->

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

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