how to set value on button click in react js?

user944513

I want to change the state value on button click using react js.I am able to get click event .But the set value not updated why .I used this

btnClick(){
    alert('---')
  //  this.setState({data: 'nannsd'});
     this.state ={data: 'sds'};
  }

here is my code http://codepen.io/naveennsit/pen/MydPJM

class App extends React.Component{
  constructor(){
     super();
    this.state ={data: 'test'};
  }
  btnClick(){
    alert('---')
  //  this.setState({data: 'nannsd'});
     this.state ={data: 'sds'};
  }

  render(){
    return <div>
      hello {this.state.data}
      <button onClick={this.btnClick}>click</button>
    </div>
  }

}

React.render(<App/>,document.getElementById('app'))
J Pagano

Two things:

1) Outside the constructor, you should call setState (instead of directly setting state). It looks like you probably tried this since it's commented out.

2) You need to bind this, so that you have the right value inside btnClick.

Here's a quick fork of your codepen with this fixed up. http://codepen.io/juliepagano/pen/xVNyrO

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

  btnClick(){
    alert('---')
   this.setState({data: 'nannsd'});
     // this.state ={data: 'sds'};
  }

  render(){
    return <div>
      hello {this.state.data}
      <button onClick={this.btnClick.bind(this)}>click</button>
    </div>
  }

}

React.render(<App/>,document.getElementById('app'))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

how to set value on button click in react js?

From Dev

How to set a TextBox value with a button click in ReactiveUI

From Dev

How to set BOOL value TRUE on button click

From Dev

JQuery Set value on button click

From Dev

how to set value in text box on button click on other page in uwp?

From Dev

how to set value in text box on button click on other page in uwp?

From Dev

How to set value of multiple parameters on a single button click?

From Dev

React native:how to get Text value on a button click?

From Dev

How to filter an table by a value in row Angular JS button click

From Dev

How to set click listener on list item in material ui in react js

From Dev

How to change the value of button on click?

From Dev

Set ng-model value to button click

From Dev

button click to set an optionsCaption value as default in knockout

From Dev

How to call directive on radio button to set value in Angular Js

From Dev

How to set the "checked" value of a React switch to a reference in React Js?

From Dev

how to set value list item in adapter class and get value on Button click in android

From Dev

how to set value list item in adapter class and get value on Button click in android

From Dev

jQuery: how to set a specific value selection in dropdown when click yes in radio button

From Dev

How to set button value in javascript

From Dev

How to set first letter to upperCase on button click

From Dev

How to set click event for button in onsen ui?

From Dev

How to set up a prompt after the click of a button?

From Dev

React Button Click Event Update State Value But Not Reflected on Button Text

From Dev

Set checkbox value in React JS

From Dev

How to react to a button click in pyqt5

From Dev

How to print React component on click of a button?

From Dev

How to react on a button click in a Templated Control

From Dev

how to get the value from button on click

From Dev

How to click a button based on a linked id value

Related Related

  1. 1

    how to set value on button click in react js?

  2. 2

    How to set a TextBox value with a button click in ReactiveUI

  3. 3

    How to set BOOL value TRUE on button click

  4. 4

    JQuery Set value on button click

  5. 5

    how to set value in text box on button click on other page in uwp?

  6. 6

    how to set value in text box on button click on other page in uwp?

  7. 7

    How to set value of multiple parameters on a single button click?

  8. 8

    React native:how to get Text value on a button click?

  9. 9

    How to filter an table by a value in row Angular JS button click

  10. 10

    How to set click listener on list item in material ui in react js

  11. 11

    How to change the value of button on click?

  12. 12

    Set ng-model value to button click

  13. 13

    button click to set an optionsCaption value as default in knockout

  14. 14

    How to call directive on radio button to set value in Angular Js

  15. 15

    How to set the "checked" value of a React switch to a reference in React Js?

  16. 16

    how to set value list item in adapter class and get value on Button click in android

  17. 17

    how to set value list item in adapter class and get value on Button click in android

  18. 18

    jQuery: how to set a specific value selection in dropdown when click yes in radio button

  19. 19

    How to set button value in javascript

  20. 20

    How to set first letter to upperCase on button click

  21. 21

    How to set click event for button in onsen ui?

  22. 22

    How to set up a prompt after the click of a button?

  23. 23

    React Button Click Event Update State Value But Not Reflected on Button Text

  24. 24

    Set checkbox value in React JS

  25. 25

    How to react to a button click in pyqt5

  26. 26

    How to print React component on click of a button?

  27. 27

    How to react on a button click in a Templated Control

  28. 28

    how to get the value from button on click

  29. 29

    How to click a button based on a linked id value

HotTag

Archive