0

I am trying to create a counter button but I can't. I know its something to do with binding but I can't find a solution. I tried by using .bind(this) but its does not work.

class Button extends React.Component{ render(){ return( <button onClick={this.props.localHandleClick}>+1</button> ) } } class Result extends React.Component{ render(){ return ( <div>{this.props.localCounter}</div> ) } } class Main extends React.Component{ constructor(props){ super(props); this.state={ counter:0 } } clickHandler(){ this.setState({counter: this.state.counter+1}); } render(){ return( <div> <Button localHandleClick={this.handleClick}/> <Result localCounter={this.state.counter} /> </div> ) } } ReactDOM.render( <Main />, document.getElementById("app") )
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> <div id="app"></div>

9
  • 1
    Add this to your constructor - this.clickHandler = this.clickHandler.bind(this). Commented Jul 12, 2017 at 19:53
  • Possible duplicate of OnClick Event binding in React.js Commented Jul 12, 2017 at 19:53
  • I have tried this. This does not work. Commented Jul 12, 2017 at 19:55
  • @KrishnaRana What is the error you get? Commented Jul 12, 2017 at 19:56
  • I don't get any error, but the screen is blank Commented Jul 12, 2017 at 19:58

2 Answers 2

1

Changes:

1. Bind handleClick method in the constructor of main component.

2. There is a name mismatch, you are passing the handleClick method but you defined the clickHandler. Replace clickHandler by handleClick.

Check the working snippet:

class Button extends React.Component{ render(){ return( <button onClick={this.props.localHandleClick}>+1</button> ) } } class Result extends React.Component{ render(){ return ( <div>{this.props.localCounter}</div> ) } } class Main extends React.Component{ constructor(props){ super(props); this.state={ counter:0 } this.handleClick = this.handleClick.bind(this); } handleClick(){ this.setState({counter: this.state.counter+1}); } render(){ return( <div> <Button localHandleClick={this.handleClick}/> <Result localCounter={this.state.counter} /> </div> ) } } ReactDOM.render( <Main />, document.getElementById("app") )
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> <div id='app'/>

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

1 Comment

let me know if you need any help in this.
0

Just use .bind(this) on your handler so that this refers to the component instead of being undefined.

Note this is not specific to React, but the default behaviour of JavaScript class methods, which are not automatically bound.

Also, you are using different names in the handler definition and when you use it in the template.

class Button extends React.Component { render() { return( <button onClick={this.props.localHandleClick}>+1</button> ); } } class Result extends React.Component { render() { return( <div>{this.props.localCounter}</div> ); } } class Main extends React.Component { constructor(props) { super(props); this.state = { counter: 0, }; this.handleClick = this.handleClick.bind(this) } handleClick() { this.setState({ counter: this.state.counter + 1, }); } render() { return( <div> <Button localHandleClick={ this.handleClick }/> <Result localCounter={ this.state.counter } /> </div> ); } } ReactDOM.render(<Main />, document.getElementById('app'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> <div id="app"></div>

If you are using Babel's class property transform, you can declare your handler as follows, and it will be automatically bound:

class Main extends React.Component { handleClick = () => { this.setState({ counter: this.state.counter + 1, }); }; constructor(props) { ... } render() { ... } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.