【问题标题】:How to use React DnD with styled component if the dragSource is also the dropTarget?如果dragSource也是dropTarget,如何使用带有样式组件的React DnD?
【发布时间】:2018-07-12 18:02:06
【问题描述】:

我已经找到了这个问题 How to use React DnD with styled component? 但在我的特殊情况下,它对我没有帮助,因为我的 dragSource 也是我的 dropTarget。

像这样:

class MyComponent extends Component {
...
  render() {
    const { connectDragSource, connectDropTarget, ... } = this.props;
    return (
      connectDragSource &&
      connectDropTarget &&
      connectDragSource(
        connectDropTarget(
          <MyStyledComponent>
            <h1>foo</h1>
          </MyStyledComponent>
        )
      )
    );
  }
}

所以问题是:如何使用 innerRef 来调用我的 connectDragSource AND 我的 connectDropTarget。

【问题讨论】:

    标签: styled-components react-dnd


    【解决方案1】:

    你可以使用组件的innerRef来获取DOM节点。

    class MyComponent extends Component {
      render() {
        const { connectDragSource, connectDropTarget } = this.props;
        return (
          connectDragSource &&
          connectDropTarget 
            <MyStyledComponent
              innerRef={ref => {
                this.props.connectDragSource(ref);
                this.props.connectDropTarget(ref);
              }}>
              <h1>foo</h1>
            </MyStyledComponent>
        );
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-06-12
      • 2018-10-17
      • 1970-01-01
      • 1970-01-01
      • 2021-03-21
      • 1970-01-01
      • 2023-01-26
      • 2021-10-07
      相关资源
      最近更新 更多