Skip to content

Instantly share code, notes, and snippets.

@xgqfrms-GitHub
Created June 28, 2017 16:19
Show Gist options
  • Save xgqfrms-GitHub/2000c356ad8cedd00f2c74d861eda215 to your computer and use it in GitHub Desktop.
Save xgqfrms-GitHub/2000c356ad8cedd00f2c74d861eda215 to your computer and use it in GitHub Desktop.
PureComponent
@xgqfrms-GitHub
Copy link
Author

// class props & this.props.onClickFunction

class PropsTest extends React.Component{
    render() {
        return (
            <div>
                <button onClick={props.onClickFunction} >
                    Add counter
                </button>
                <p>
                    this state is 
                    <span 
                        style={{color: "#0ff", fontSize: "32px"}}
                        >
                        {this.props.counter}
                    </span>
                </p>
            </div>
        );
    };
};

// function props & no need this

const Test = (props) =>{
    return (
        <div>
            <button onClick={props.onClickFunction} >
                Add `props` counter
            </button>
            <p
                style={{color: "#0ff", fontSize: "32px"}}
                >
                {props.counter}
            </p>
        </div>
    );
};



class App extends React.Component{
    state = {
        counter: 1
    };
    addCounter = (e) => {
        console.log(`e =`, e);
        this.setState(
          (prevState) => (
              {
                  counter: prevState.counter + 1
              }
          )
        );
    };
    render(){
        return(
            <div>
                <PropsTest 
                    onClickFunction={this.addCounter}
                    counter={this.state.counter}
                    />
                <Test 
                    onClickFunction={this.addCounter}
                    counter={this.state.counter}
                    />
            </div>
        );
    }
}


export default App;

const props = {
    name: "xgqfrms",
    age: 17
};

ReactDOM.render(
    <div>
        <App {...props}/>
    </div>
    , mountNode
);

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