Skip to content

Instantly share code, notes, and snippets.

@Ami777
Last active March 2, 2017 13:50
Show Gist options
  • Save Ami777/b7e073ea6a32b605321cc6f9bc1cc160 to your computer and use it in GitHub Desktop.
Save Ami777/b7e073ea6a32b605321cc6f9bc1cc160 to your computer and use it in GitHub Desktop.
import React from 'react';
import ReactDOM from 'react-dom';
document.addEventListener('DOMContentLoaded', function(){
class ButtonToClick extends React.Component {
handleButtonClick = () => {
if ( typeof this.props.onClick === 'function' ){
this.props.onClick();
}
};
render(){
return <button onClick={this.handleButtonClick}>Click!</button>;
}
}
class ButtonCounter extends React.Component {
constructor(props){
super(props);
this.state = {
count : 0,
};
}
handleIncBtnClick = () => {
this.setState({
count : this.state.count + 1
});
};
render(){
return <div>
<h1>{ this.state.count }</h1>
<ButtonToClick onClick={this.handleIncBtnClick}/>
<ButtonToClick onClick={this.handleIncBtnClick}/>
<ButtonToClick onClick={this.handleIncBtnClick}/>
<ButtonToClick onClick={this.handleIncBtnClick}/>
</div>;
}
}
class App extends React.Component {
render(){
return <ButtonCounter />;
}
}
ReactDOM.render(
<App />,
document.getElementById('app')
);
});
@Ami777
Copy link
Author

Ami777 commented Mar 2, 2017

import React from 'react';
import ReactDOM from 'react-dom';

document.addEventListener('DOMContentLoaded', function(){
    class ButtonToClick extends React.Component {
        handleButtonClick = () => {
            if ( typeof this.props.onIncrement === 'function' ){
                this.props.onIncrement();
            }
        };

        render(){
            return <button onClick={this.handleButtonClick}>Click!</button>;
        }
    }

    class ButtonCounter extends React.Component {
        constructor(props){
            super(props);

            this.state = {
                count  : 0,
            };
        }

        handleIncBtnClick = () => {
            this.setState({
                count : this.state.count + 1
            });
        };

        render(){
            return <div>
                <h1>{ this.state.count }</h1>
                <ButtonToClick onIncrement={this.handleIncBtnClick}/>
                <ButtonToClick onIncrement={this.handleIncBtnClick}/>
                <ButtonToClick onIncrement={this.handleIncBtnClick}/>
                <ButtonToClick onIncrement={this.handleIncBtnClick}/>
            </div>;
        }
    }

    class App extends React.Component {
        render(){
            return <ButtonCounter  />;
        }
    }

    ReactDOM.render(
        <App />,
        document.getElementById('app')
    );
});```

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment