1

Somewhat new to react and having trouble passing multiple state values to an input.

Specifically, state from drop-down menus. I can accomplish this in a p tag by chaining the various state values together.

This works

   <p> 
    {this.state.one}_
    {this.state.two}_
    {this.state.three}_
    {this.state.four}_
    {this.state.five}
    </p>

This will return

one_two_three_four_five

Whenever I try to pass these same values into a single input I get an error.

This does not work

<label>
  Title:
  <input 
value=
{this.state.one}_
{this.state.two}_
{this.state.three}_
{this.state.four}_
{this.state.five}
 type="text" /> 
</label>

How can one pass multiple state values into a single input?

2 Answers 2

2

Template literals with variables :

<input 
        value={ `${this.state.one}_${this.state.two}` }
/>
Sign up to request clarification or add additional context in comments.

Comments

1

You have to pass a single expression to the value attribute. Change to this:

<label>
  Title:
  <input value={`${this.state.one}_
    ${this.state.two}_
    ${this.state.three}_
    ${this.state.four}_
    ${this.state.five}`
 }
 type="text" /> 
</label>

Note: this is ES6 string interpolation. The ES5 alternative is the regular string concatenation with "+"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.