React - how to pass state to another component React - how to pass state to another component reactjs reactjs

React - how to pass state to another component


Move all of your state and your handleClick function from Header to your MainWrapper component.

Then pass values as props to all components that need to share this functionality.

class MainWrapper extends React.Component {    constructor() {        super();        this.state = {            sidbarPushCollapsed: false,            profileCollapsed: false        };        this.handleClick = this.handleClick.bind(this);    }    handleClick() {        this.setState({            sidbarPushCollapsed: !this.state.sidbarPushCollapsed,            profileCollapsed: !this.state.profileCollapsed        });    }    render() {        return (           //...           <Header                handleClick={this.handleClick}                sidbarPushCollapsed={this.state.sidbarPushCollapsed}               profileCollapsed={this.state.profileCollapsed} />        );

Then in your Header's render() method, you'd use this.props:

<button type="button" id="sidbarPush" onClick={this.props.handleClick} profile={this.props.profileCollapsed}>