简体   繁体   English

与父组件进行通讯,而无需使用状态/道具

[英]Communicate with parent component without using state/props

I need a parent component to know when it's child components have changed from collapsed to expanded and viceversa. 我需要一个父组件才能知道其子组件何时从折叠变为展开,反之亦然。

I don't want to keep that in the app state (Redux). 我不想将其保持在应用状态(Redux)。 I can't move the logic up to the parent component. 我不能将逻辑上移到父组件。

My components are: 我的组件是:

  • I have component C which is an item that can be expanded/collapsed. 我有组件C,它是可以扩展/折叠的项目。
  • Then I have Component B which is a wrapper for component C in a specific case (gives it the draggable behaviour). 然后,我有了组件B,它在特定情况下是组件C的包装(赋予它可拖动的行为)。
  • And finally component A which lists components C in a loop. 最后是组件A,它在一个循环中列出了组件C。 A, sometimes wraps Cs in B, and sometimes not (when items shouldnt be draggable). A,有时将Cs包装在B中,有时不包装(当物品不应该被拖动时)。

I need B to know C is expanded, so that it isn't draggable while expanded. 我需要B知道C已展开,因此它在展开时不可拖动。

I can't put the expanded/collapsed logic in B, because C should always be collapsible/expandable independently of if draggable. 我不能将展开/折叠逻辑放在B中,因为C应该始终是可折叠/可展开的,而与是否可拖动无关。

Is there any simple way to accomplish this without needing to have the expand/collapse state of each item in the app state? 有什么简单的方法可以完成此操作,而无需使每个项目的展开/折叠状态都处于应用状态?

I have read about https://facebook.github.io/react/docs/context.html but seems still in experimental phase... 我已经阅读过有关https://facebook.github.io/react/docs/context.html的信息,但似乎仍处于实验阶段...

You can keep the expanded state inside the C component and update the draggable state of the B component with a callback passed with the props of the C component. 您可以将扩展状态保留在C组件内,并通过与C组件的props一起传递的回调来更新B组件的可拖动状态。 In this way both components keep their own state, no need to add the state on the A component or in you App state (the Redux one). 这样,两个组件都保持自己的状态,无需在A组件上或您的App状态(Redux一个)中添加状态。

Here is an example : https://jsfiddle.net/snahedis/69z2wepo/28567/ 这是一个例子https : //jsfiddle.net/snahedis/69z2wepo/28567/

var sampleList = [{id: 1, draggable: true},{id: 2, draggable: false}];

var Acomponent = React.createClass({
  render: function() {
    return (
      <div>
        {sampleList.map(function(component) {
          if (component.draggable) {
            return <Bcomponent key={component.id} />;
          } else {
            return <Ccomponent key={component.id} />;
          }
        })}
      </div>
    );
  }
});

var Bcomponent = React.createClass({
  getInitialState: function() {
    return {
        draggable: true
    }
  },
  hasBeenToggled: function() {
    this.setState({
        draggable: !this.state.draggable
    });
  },
  render: function() {
    return <Ccomponent draggable={this.state.draggable} toggleExpandableCallback={this.hasBeenToggled} />;
  }
});

var Ccomponent = React.createClass({
  getInitialState: function() {
    return {
        expanded: false
    }
  },
  toggleExpandable: function() {
    this.setState({
        expanded: !this.state.expanded
    });

    if (typeof this.props.toggleExpandableCallback === "function") {
        this.props.toggleExpandableCallback();
    }
  },
  render: function() {
    return (
      <div>
        <div>I'm the C component and my expanded state is : {this.state.expanded.toString()}</div>
        <div>{(this.props.draggable) ? "oh, and I'm draggable !" : "and I'm saddly not draggable"}</div>
        <button onClick={this.toggleExpandable}>Change expandable state</button>
      </div>
    );
  }
});

ReactDOM.render(
  <Acomponent />,
  document.getElementById('container')
);

A few ideas: 一些想法:

1) You can ALWAYS render B (A renders B's, each which wraps a C), but just disable the draggable behavior in B when it shouldn't be draggable (pass it in something like a isDraggable to B). 1)您可以始终渲染B(A渲染B,每个都包裹一个C),但是只要它不应该被拖动(只要将它以isDraggable给B),就可以禁用B中的可拖动行为。 Then, C's collapsible state can be stored in B, and passed a prop to C, as B will always be there. 然后,C的可折叠状态可以存储在B中,并向B传递一个支持,因为B始终会存在。

2) You can move all the state to A (not sure if this is what you mean by "app state") 2)您可以将所有状态移到A(不确定这是否是“应用程序状态”的意思)

It's hard to suggest without knowing more context. 不知道更多上下文很难提出建议。 But in cases like this, there's almost a better way to break up your components / app state so that it doesn't feel "wrong." 但是在这种情况下,几乎有更好的方法来分解组件/应用程序状态,以免出现“错误”的感觉。

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

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