javascript - 以父组件内对象的形式从所有子组件获取合并数据 : React JS

标签 javascript reactjs ecmascript-6 react-hooks setstate

我正在为应用程序实现一个设置页面。对于每个设置,我都实现了一个具有启用(绿色)或禁用(红色)状态的 slider 。但父级的设置是只读的,并且是根据其子级的值计算的。

父级的设置推导如下:如果所有子级都是红色,则父级保持红色;如果全部都是绿色,则父级保持绿色;如果至少一个 child 是绿色的,那么 parent 保持灰色(待定)。

这些设置按如下方式分组:

父功能 1:(只读切换)

 Setting 1   (Toggle)

 Setting 2   (Toggle)

父功能 2:(只读切换)

Setting 1   (Toggle)

Setting 2   (Toggle)

最后还有一个按钮,可以为我提供所有 parent 和 child 的综合值。但到目前为止,我只能与一位 parent 和 2 个 child 一起做。

有人可以帮助提供一种方法,将所有设置的综合值放在一个位置(例如配置所有这些设置的 super 父组件)。

为此,我使用react-multi-toggle作为此切换开关。

非常感谢您的帮助。

代码沙箱:https://codesandbox.io/s/react-multi-toggle-solution-perfect-v9bi5

应用程序


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>
      </>
    );
  }
}

子设置


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}
      />
    );
  }
}

家长设置


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={() => {}}
      />
    );
  }
}



最佳答案

我建议您将您的 child 和家长分组在一个组件下。假设我们将其命名为Settings。然后,我们创建另一个组件,它将呈现设置列表和一个按钮。最后一个组件将保存所有设置的值。最后,每次设置组件的值发生变化时,我们都会更新列表。查看 sample working app here .

应用程序组件

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>
    );
  }
}

设置组件

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>
    );
  }
}

关于javascript - 以父组件内对象的形式从所有子组件获取合并数据 : React JS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57546099/

相关文章:

javascript - AngularJS : Can't figure out why nested angular directive does not get rendered when transcluding with transclude :'element'

javascript - 从外部 JSON 创建饼图不起作用

javascript - Webpack 无法解析 svg

javascript - 如何处理 JavaScript 中未定义的函数参数的解构?

javascript - NodeJS 生成器永远不会到达某一行?

javascript - 密码学家类型错误: First argument must be a string

javascript - 在异步函数reactjs之后调用另一个组件的方法

reactjs - 如何使用鼠标滚轮进行 react 水平滚动

reactjs - createRef 在 react 无状态组件中无法正常工作

reactjs - 基本构造函数必须具有相同的返回类型