2

class App extends React.Component  {
  constructor (props) {
    super(props);
    this.state ={val:'test'}
  }

  change(e){
    let valueOfInput = e.target.value;
  }
  submit(ev){
    ev.preventDefault();
    alert(valueOfInput)
  }
  render() {
    return(
      <div>
        <form action="">
          <input onChange={this.change.bind(this)} type="text" value={this.state.val}/>
          <input onClick={this.submit.bind(this)} type="submit"  value='submit'/>
        </form>
      </div>

    )
  }
}
ReactDOM.render(
<App />,
document.getElementById('root')
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.1/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.1/umd/react-dom.production.min.js"></script>
<div id="root"> </div>
With this code i want to send the value of the input in alert from sumbit() function. So, i want to take the value from input, to save it in variable valueOfInput, and after that to send that value in alert from submit function. How to realize that in ReactJs?

3 Answers 3

1

How about using state!!

setState() enqueues changes to the component state and tells React that this component and its children need to be re-rendered with the updated state. This is the primary method you use to update the user interface in response to event handlers and server responses.

class App extends React.Component  {
  constructor (props) {
    super(props);
    this.state ={valueOfInput:''}
  }
  change(e){
    valueOfInput = e.target.value;
    this.setState({valueOfInput});
  }
  submit(ev){
    ev.preventDefault();
    alert(this.state.valueOfInput)
  }
  render() {
    return(
      <div>
        <form action="">
          <input onChange={this.change.bind(this)} type="text" value={this.state.valueOfInput}/>
          <input onClick={this.submit.bind(this)} type="submit"  value='submit'/>
        </form>
      </div>
    )
  }
}
ReactDOM.render(
  <App />,
  document.getElementById('root')
);

Happy coding!!! Hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

1

Storing value of input in valueOfInput variable can be done by declaring it into class level using this.

constructor(props) {
super(props);
this.state = { val: "test" };
this.valueOfInput = null;
  }


change(e) {
    this.valueOfInput = e.target.value;
  }
  submit(ev) {
    ev.preventDefault();
    alert(this.valueOfInput);
  }

But it won't work as expected as we're not updating value of input with new value. So to solve this we have to store new input value into state and use that value in input.

change(e) {
this.valueOfInput = e.target.value;
this.setState({
  val: e.target.value
});

}

Comments

0

Your valueOfInput seems to be defined in its block space of change(), declare that variable within the class state and you should be able to reference it in the submit() callback.

class App extends React.Component  {
  constructor (props) {
    super(props);
    this.state = {
     valueOfInput: null,
    }
  }

  change(e){
    this.setState({
      valueOfInput: e.target.value,
  val:e.target.value

    });
  }
  submit(ev){
    ev.preventDefault();
    alert(this.state.valueOfInput)
  }
  render() {
    return(
      <div>
        <form action="">
          <input onChange={this.change.bind(this)} type="text" value={this.state.valueOfInput}/>
          <input onClick={this.submit.bind(this)} type="submit"  value='submit'/>
        </form>
      </div>
    )
  }
}
ReactDOM.render(
  <App />,
  document.getElementById('root')
);

2 Comments

Sorry about that, I changed it to use state now
,thanks. It was an issue, the value of input wasn't dinamic, i mean you can't change the value of input, because you didn't add val:e.target.value in change function.

Your Answer

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