4

I have a React component with an input field.

I want to update the value of the input field when a button is clicked, I can confirm that the value changes when I inspect element but it doesn't display in the input field. Below is a sample code to just to give an idea.

class InputField {
 constructor(props) {
  super(props)
 }
 state = {
  userInput: ''
 }
}

onClick = () => {
 this.setState({
  userInput: 'Test'
 })
}


render() {
 return ( <input value={this.state.userInput} name="sampleInput" />
  <button onClick={this.onClick}> Click me </button>  
 )
}
4
  • 1
    so many syntax error!! Commented Sep 18, 2019 at 10:13
  • 1
    @saurssaurav it's just an example to illustrate what he's trying to achieve Commented Sep 18, 2019 at 10:14
  • You have a wrong "} ". Commented Sep 18, 2019 at 10:14
  • It's just a sample code. Commented Sep 18, 2019 at 10:39

6 Answers 6

3

Fix syntax

your code is ok, just little order.

I add the whole component

import React, { Component } from 'react';

class InputField extends Component {
  constructor(props) {
    super(props)
  }

  state = {
    userInput: ''
  }

  onClick = () => {
    this.setState({
      userInput: 'Test'
    })
  }

  render() {
    return (
      <div>
        <input value={this.state.userInput} name="sampleInput" />
        <button onClick={this.onClick}>Click me</button>
      </div>
    )
  }
}

export default InputField;
Sign up to request clarification or add additional context in comments.

1 Comment

This doesn't explain what the actual issue was. Just the extra } after state = and the missing <div> in the return it seems.
1

I just removed syntax error in your example and it worked for me.

import React from 'react';

export default class InputField extends React.Component {
    constructor(props) {
        super(props)
        this.state = {
            userInput: ''
        }
    }

    onClick = () => {
        this.setState({
            userInput: 'Test'
        })
    }


    render() {
        return (
            <div>
                <input value={this.state.userInput} name="sampleInput"/>
                <button
                    onClick = {this.onClick} 
                >
                    Click me
                </button>
            </div>
        )
    }
}

Comments

1

One approach would be to implement this as a functional component via hooks. You could for instance use the state hook to store and render the userInput data as shown below:

import React from "react";

/* Declare functional InputField component */
function InputField () {

  /* Define local state hook to store the "user input" data */
  const [userInput, setUserInput] = React.useState("");

  const onClick = (e) => {
      /* Prevent button click's default behavior */
      e.preventDefault();
      /* Call the state's "setter" method to update "userInput" state */
      setUserInput('Test')
  }

   /* Render both input and button in a <> fragment */    
   return (<>
     <input value={this.state.userInput} name="sampleInput"/>
     <button onClick={onClick}>Click me</button>  
   </>)
} 

To use this component, simply render it as:

<InputField />

Comments

0

I just fix your syntax errors and it run no any error

class InputField extends React.Component {
  constructor(props) {
     super(props);
     this.state = {
         userInput: '',
     };
  }

  onClick = () => {
    this.setState({
      userInput: 'Test',
    });
  };

  render() {
    return (
      <div>
        <input value={this.state.userInput} name="sampleInput" />
        <button onClick={this.onClick}>Click me</button>
      </div>
    );
  }
}

Comments

0

Using defaultValue attribute

const [name, setName] = useState("")

<input defaultValue={name} placeholder='Enter Name' onChange={(e) => setName(e.target.value)} />

<button onClick={()=>setName("Umesh Bedi")}>Add Name</button>

I hope, It will help someone in functional component.

Comments

0
import React from 'react';

export default class InputField extends React.Component {
    constructor(props) {
        super(props)
        this.state = {
            userInput: ''
        }
    }

    onClick = () => {
        this.setState({
            userInput: 'Test'
        })
    }


    render() {
        return (
            <div>
                <input value={this.state.userInput} name="sampleInput"/>
                <button
                    onClick = {this.onClick} 
                >
                    Click me
                </button>
            </div>
        )
    }
}

1 Comment

I can see no difference between this code and that in another answer. Either highlight the difference(s) or delete the post.

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.