Search code examples
javascriptreactjsecmascript-6react-hookssetstate

Get consolidated data from all the child components in the form of an object inside a parent component : React JS


I am implementing a setting page for an application. For each setting I have implemented a slider that has enabled(green) or disabled(red) state. But parent's settings is read only and is calculated based on the values of its children.

Parent's setting is derived as follows: If all children are red, parent stays red ; If all are green parent stays green; If at-least one of child is green then parent stays grey(Pending).

These settings are grouped something like this:

Parent Feature 1 : (read-only-toggle)

 Setting 1   (Toggle)

 Setting 2   (Toggle)

Parent Feature 2: (read-only-toggle)

Setting 1   (Toggle)

Setting 2   (Toggle)

And in the end there is also a button, that gives me a consolidated values of all parent and children. But so far I was able to do only with one parent and 2 children.

Can someone help with an approach of getting consolidated values of all the settings in one place(Like a super parent component where all these settings are configured).

For this , I am using react-multi-toggle for this toggle switch.

Help would be really appreciated.

Code Sandbox: https://codesandbox.io/s/react-multi-toggle-solution-perfect-v9bi5

App


import React from "react";
import ChildSwitch from "./ChildSwitch";
import ParentSwitch from "./ParentSwitch";

export default class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      parentVal: "disabled",
      switch1Val: "enabled",
      switch2Val: "disabled"
    };
  }

  componentDidMount() {
    this.setParentSwitchValue();
  }

  onGetChildSwitchValues = () => {
    console.log(this.state);
  };

  setChildSwitchValue = (whichSwitch, selected) => {
    this.setState(
      prevState => ({ ...prevState, [whichSwitch]: selected }),
      this.setParentSwitchValue
    );
  };

  setParentSwitchValue = () => {
    const { switch1Val, switch2Val } = this.state;
    const switchStates = [switch1Val, switch2Val];
    let parent = "pending";
    if (switchStates.every(val => val === "enabled")) {
      parent = "enabled";
    }
    if (switchStates.every(val => val === "disabled")) {
      parent = "disabled";
    }
    this.setState(prevState => ({ ...prevState, parentVal: parent }));
  };

  render() {
    const { parentVal, switch1Val, switch2Val } = this.state;
    return (
      <>
        <div className="boxed">
          Parent Setting 1 :{" "}
          <ParentSwitch
            parentSwitch={parentVal}
            onSelect={this.setParentSwitchValue}
          />
          Setting 1:
          <ChildSwitch
            switchName={"switch1Val"}
            selected={switch1Val}
            onSelect={this.setChildSwitchValue}
          />
          Setting 2:
          <ChildSwitch
            switchName={"switch2Val"}
            selected={switch2Val}
            onSelect={this.setChildSwitchValue}
          />
        </div>
        <button onClick={this.onGetChildSwitchValues}>Get All Values</button>
      </>
    );
  }
}

ChildSetting


import MultiToggle from "react-multi-toggle";
import React from "react";

export default class ChildSwitch extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      options: [
        {
          displayName: "Disabled",
          value: "disabled"
        },
        {
          displayName: "Enabled",
          value: "enabled"
        }
      ]
    };
  }

  onSelectOption = selected => {
    this.props.onSelect(this.props.switchName, selected);
  };

  render() {
    const { options } = this.state;
    const { selected } = this.props;
    return (
      <MultiToggle
        options={options}
        selectedOption={selected}
        onSelectOption={this.onSelectOption}
      />
    );
  }
}

Parent Setting


import MultiToggle from "react-multi-toggle";
import React from "react";
import "react-multi-toggle/style.css";

export default class ParentSwitch extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      options: [
        {
          displayName: "Disabled",
          value: "disabled"
        },
        {
          displayName: "Pending",
          value: "pending"
        },
        {
          displayName: "Enabled",
          value: "enabled"
        }
      ]
    };
  }

  render() {
    const { options } = this.state;
    return (
      <MultiToggle
        options={options}
        selectedOption={this.props.parentSwitch}
        onSelectOption={() => {}}
      />
    );
  }
}




Solution

  • I will suggest that you group your child and parent under one component. Let say we name it Settings. Then, we create another component that will render a list of Settings and a button. This last component will hold the values of all Settings. Finally, each time the value of a Setting Component Change, we update the list. Checkout a sample working app here.

    App Component

    export default class App extends PureComponent {
      state = {};
    
      onSettingChange = (settingId, setting) => {
        this.setState(prevState => ({
          ...prevState,
          [settingId]: setting
        }));
      };
    
      onGetSettingValues = () => {
        console.log(this.state);
      };
    
      render() {
        return (
          <Fragment>
            <Setting id="setting1" onChange={this.onSettingChange} />
            <Setting id="setting2" onChange={this.onSettingChange} />
            <button onClick={this.onGetSettingValues}>Get All Values</button>
          </Fragment>
        );
      }
    }
    

    Setting Component

    import React, { PureComponent, Fragment } from "react";
    import ChildSwitch from "./ChildSwitch";
    import ParentSwitch from "./ParentSwitch";
    
    export default class Setting extends PureComponent {
      state = {
        parentVal: "disabled",
        switch1Val: "enabled",
        switch2Val: "disabled"
      };
    
      componentDidMount() {
        this.setParentSwitchValue();
      }
    
      setChildSwitchValue = (whichSwitch, selected) => {
        this.setState(
          prevState => ({ ...prevState, [whichSwitch]: selected }),
          this.setParentSwitchValue
        );
      };
    
      handleChange = () => {
        const { id, onChange } = this.props;
        onChange(id, this.state);
      };
    
      setParentSwitchValue = () => {
        const { switch1Val, switch2Val } = this.state;
        const switchStates = [switch1Val, switch2Val];
        let parent = "pending";
        if (switchStates.every(val => val === "enabled")) {
          parent = "enabled";
        }
        if (switchStates.every(val => val === "disabled")) {
          parent = "disabled";
        }
    
        this.setState(
          prevState => ({ ...prevState, parentVal: parent }),
          this.handleChange
        );
      };
    
      render() {
        const { parentVal, switch1Val, switch2Val } = this.state;
        return (
          <Fragment>
            <div className="boxed">
              Parent Setting 1
              <ParentSwitch
                parentSwitch={parentVal}
                onSelect={this.setParentSwitchValue}
              />
              Setting 1:
              <ChildSwitch
                switchName={"switch1Val"}
                selected={switch1Val}
                onSelect={this.setChildSwitchValue}
              />
              Setting 2:
              <ChildSwitch
                switchName={"switch2Val"}
                selected={switch2Val}
                onSelect={this.setChildSwitchValue}
              />
            </div>
          </Fragment>
        );
      }
    }