简体   繁体   English

具有本地状态的组件未在React Redux上更新

[英]Component with local state not updating on React Redux

I'm trying to create a standalone component MultiSelect (I know there are components out there but I want to learn how to do one). 我正在尝试创建一个独立的组件MultiSelect (我知道那里有组件,但是我想学习如何做)。

I created it in a way it will store the selected values in its state. 我以某种方式创建了它,它将以其状态存储所选值。

I call it like this from my container: 我从容器中这样称呼它:

<MultiSelect
  items={ this.props.boards }
  label="Boards"
  value={ this.props.selectedBoards }
  onChange={ e => this.onChange(e) } />

When I dispatch an action I get the correct values for the container's props. 当我分派动作时,我会得到容器道具的正确值。 My MultiSelect even display the correct options. 我的MultiSelect甚至显示正确的选项。 However for some reason it doesn't update the selected values. 但是由于某种原因,它不会更新所选的值。 In the container this.props.selectedBoards has the correct value, but the component doesn't get this update. 在容器中, this.props.selectedBoards具有正确的值,但组件未获得此更新。

export default class MultiSelect extends React.Component {
  constructor(props) {
    super(props); // props.items = all options to select

    this.state = {
      selectedItems: props.value || [], // selected options (problem)
      search: '',
      hidden: true,
    };
  }

  render() {
    return (<div>
      ...
      this.state.selectedItems

The options to select is on the props (which works) and the selected options is on the state (which doesn't get updated). 选择的选项在props (起作用),所选的选项在state (不会更新)。

How can I make the MultiSelect component update with a new value for selectedItems which is in the state? 如何使处于处于状态的selectedItems的新值更新MultiSelect组件? Or what would be the recommended way to create a component like this? 或推荐使用哪种方式创建这样的组件?

I end up moving selectedItems from the state to the props , and then on the onChange event where elements get selected I'm getting the value from the props , adding/removing the changed item and calling this.props.onChange with the modified array. 我最终将selectedItemsstate移动到props ,然后在其中元素被选中的onChange事件上,我从props获取值,添加/删除更改的项目,并使用修改后的数组调用this.props.onChange

Then on the container I'm dispatching an action with the new array so I can get a new state. 然后在容器上,我使用新数组调度一个动作,以便获得新状态。

I solved it by delegating the task to keep track of selected items to the container. 我通过委派任务来跟踪选定项目到容器中来解决它。

MultiSelect: 多选:

export default class MultiSelect extends React.Component {
  constructor(props) {
    super(props);

    this.state = {
      search: '',
      hidden: true,
    };
  }

  onChange(item) {
    const selectedItems = [ ...this.props.selectedItems ];

    const index = selectedItems.findIndex(element => element.value === item.value);

    if (index !== -1) {
      selectedItems.splice(index, 1);
    }
    else {
      selectedItems.push(item);
    }

    this.props.onChange(selectedItems); // container
  }

  render() {
    return (<div>
      ...
      this.props.selectedItems

Container: 容器:

onChange(data) {
  this.props.dispatch({ type: 'SetSelectedBoards', data });
}

<MultiSelect
  items={ this.props.boards }
  label="Boards"
  selectedItems={ this.props.selectedBoards }
  onChange={ e => this.onChange(e) } />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM