0

I'm just starting with React and it seems just beautifull to create user interfaces, however, I do have problem with communication between components.

I do have a very simple react component, representing a button:

import React from 'react';

export default class OfficeUIButton extends React.Component {

   constructor(props) {
     super(props);
     this.state = {
       active: false,
     };

     // Bind the event handlers.
     this.onMouseDownEventHandler = this.onMouseDownEventHandler.bind(this);
     this.onMouseUpEventHandler = this.onMouseUpEventHandler.bind(this);
   }

  onMouseDownEventHandler() {
    this.setState({
      active: true
    });
  }

  onMouseUpEventHandler() {
    this.setState({
      active: false
    });
  }

  render() {
    return  <div className={"officeui-button" + (this.state.active ? ' active': '')} onMouseDown={this.onMouseDownEventHandler} 
                 onMouseUp={this.onMouseUpEventHandler}>
              <span className="officeui-button-label">{this.props.text}</span>
            </div>
  }
} 

This component has a state property named active. Based on the state, an additional class is added to the button.

Let's assume that I have 2 of these OfficeUIButton components on my page, how can I, by clicking the first button, make the second button active?

This is just a dummy example, but I need this for example for a modal popup, or for disabling buttons based on a certain action.

Thanks for the help.

Complexity
  • 5,682
  • 6
  • 41
  • 84
  • 1
    [Lift the state up](https://facebook.github.io/react/docs/lifting-state-up.html). If multiple components somehow depend on each other, the state should be owned by one of their common ancestors. – Felix Kling Dec 02 '16 at 08:12
  • Possible duplicate: [ReactJS Two components communicating](https://stackoverflow.com/questions/21285923/reactjs-two-components-communicating) – oklas Dec 21 '17 at 14:37

2 Answers2

1

You need then the state in a parent component which will control your buttons. You can do something like :

Button component, where you pass onClick prop and active prop

class Button extends React.Component {
    render(){
    let disabled = this.props.active ? "disabled" : "";
    return(
        <div><button onClick={this.props.onClick} disabled={disabled}>Button</button></div>
    )
  }
}

Then in your parent you need to have a state which you will pass to the button components and onClick function:

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

      this.state = {
        active: false
      }
    }

    handleClick(event){ 
      this.setState({active: !this.state.active});
    }

    render(){
      return (
        <div>
            <Button onClick={this.handleClick.bind(this)}/>
            <Button active={this.state.active}/>
        </div>
      )
    }
}

React.render(<Test />, document.getElementById('container'));

Here is a fiddle.

Hope this helps.

Boky
  • 11,554
  • 28
  • 93
  • 163
1

You should in the higher component (let's asume for easy explanation that it is a page), where you will store a local store and where you will pass to each UIOFficeButton a function that would set this higher component state e.g. Button1Clicked on true, and also send that state to the other component.

class Test extends React.Component {
    constructor(props){
      super(props);
        this.state = {
          buttonOneClicked: false
        }
      }

handleClickButtonOne = () => this.setState({buttonOneClicked: true});

render(){
        return (
            <div>
               <OfficeUIButton onClick={this.handleClickButtonOne} />
               <OfficeUIButton disabled={this.state.buttonOneClicked} />
           </div>
       )
    }
}

Do not forget to actually handle that disabled and onClick props in your OfficeUIButton like in button

<button disabled={props.disabled} />

and do not use Span as a button.

Shiroo
  • 666
  • 4
  • 11