如何将 props 及其方法动态传递给 React 中的组件?

我有一个动态加载子组件的父组件:

父组件:

// ... lifecycle methods et al

   setOverviewRef(ref) {
    this.OverviewHeader = ref;
  }

  setBowSizeCompareRef(ref) {
    this.bowSizeCompare = ref;
  }

  setDualVideoRef(ref) {
    this.dualVideoRef = ref;
  }

render() {

    const showComponent = (componentName, {refToPass, refMethod}) => {
      if (typeof this.state[`${componentName}`] !== undefined && this.state[`${componentName}`] !== null) {
        const Component = this.state[`${componentName}`].default;
        console.log('{refToPass, refMethod}: ', {refToPass, refMethod});
        return (
          <Component {...{ refToPass: this[`${refMethod}`] }} />
        );
      } else {
        return null;
      }
    }

    return (
      <section>
        <OverviewHeader overviewRef={this.setOverviewRef} />
        { showComponent('BowSizeCompareComponent', {refToPass: 'bowSizeCompareRef', refMethod: 'setBowSizeCompareRef' }) }
        { showComponent('DualVideoComponent', {refToPass: 'dualVideoRef', refMethod: 'setDualVideoRef' }) }
      </section>
    );
  }

假设我想要 BowSizeCompareComponent,我如何获取它以便Component返回showComponent的形式为:

<Component bowSizeCompareRef={this.setBowSizeCompareRef} />

如果是DualVideoComponent,则应采用以下形式:

<Component dualVideoRef={this.setDualVideoRef} />

我需要修复传递的结构refObjshowComponent()

render() {

    const showComponent = (componentName, refObj) => {
      if (typeof this.state[componentName] !== undefined && this.state[componentName] !== null) {
    const Component = this.state[componentName].default;
    return (
      <Component {...refObj} />
    );
  } else {
    return null;
  }
};

    return (
      <section>
        <OverviewHeader overviewRef={this.setOverviewRef} />
        { showComponent('BowSizeCompareComponent', {bowSizeCompareRef: this.setBowSizeCompareRef }) }
        { showComponent('DualVideoComponent', {dualVideoRef: this.setDualVideoRef }) }
      </section>
    );
  }

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何将props传递给在React中作为props传递的组件?

来自分类Dev

如何将组件 props 从 React 传递到 Redux?

来自分类Dev

将函数传递给 props 中的 React 组件

来自分类Dev

React js将props传递给组件

来自分类Dev

React-router:如何将props传递给子组件?

来自分类Dev

如何通过React中的props将函数传递给不同组件中的函数?

来自分类Dev

将 props 传递给动态加载的组件

来自分类Dev

(react-window) 如何将 props 传递给 <FixedSizeList>{Row}</FixedSizeList> 中的 {Row}

来自分类Dev

如何将变量中的值传递给React组件

来自分类Dev

如何将 props 传递给 ReactJS 中 const 中描述的组件

来自分类Dev

将 props 传递给包装在变量中的 react-redux 组件

来自分类Dev

将 props 传递给高阶无状态功能 React 组件

来自分类Dev

如何在React中不使用props的情况下将函数传递给组件的层次结构?

来自分类Dev

如何在React中引用组件的props?

来自分类Dev

如何将值附加到React组件子代的props中?

来自分类Dev

React Router和this.props.children-如何将状态传递给this.props.children

来自分类Dev

在ES6中编写时,使用React中的props将复杂对象传递给子组件的正确方法是什么?

来自分类Dev

将 props 的函数传递给 react 中的 render 函数

来自分类Dev

如何将 props(从 Redux 获取的)从一个组件传递到另一个(React/Redux)?

来自分类Dev

通过 props 动态传递 React 组件?

来自分类Dev

如何将React Hook传递给子组件

来自分类Dev

如何将函数作为属性传递给React组件?

来自分类Dev

如何将道具传递给子React组件?

来自分类Dev

如何将属性传递给 React 函数组件?

来自分类Dev

React - 如何将道具传递给孙子组件?

来自分类Dev

如何将状态className变量传递给React中的另一个组件

来自分类Dev

如何将节点作为道具传递给React中的组件?

来自分类Dev

如何将组件回调传递给react-router中的children元素?

来自分类Dev

如何将值传递给 React 中的其他组件

Related 相关文章

  1. 1

    如何将props传递给在React中作为props传递的组件?

  2. 2

    如何将组件 props 从 React 传递到 Redux?

  3. 3

    将函数传递给 props 中的 React 组件

  4. 4

    React js将props传递给组件

  5. 5

    React-router:如何将props传递给子组件?

  6. 6

    如何通过React中的props将函数传递给不同组件中的函数?

  7. 7

    将 props 传递给动态加载的组件

  8. 8

    (react-window) 如何将 props 传递给 <FixedSizeList>{Row}</FixedSizeList> 中的 {Row}

  9. 9

    如何将变量中的值传递给React组件

  10. 10

    如何将 props 传递给 ReactJS 中 const 中描述的组件

  11. 11

    将 props 传递给包装在变量中的 react-redux 组件

  12. 12

    将 props 传递给高阶无状态功能 React 组件

  13. 13

    如何在React中不使用props的情况下将函数传递给组件的层次结构?

  14. 14

    如何在React中引用组件的props?

  15. 15

    如何将值附加到React组件子代的props中?

  16. 16

    React Router和this.props.children-如何将状态传递给this.props.children

  17. 17

    在ES6中编写时,使用React中的props将复杂对象传递给子组件的正确方法是什么?

  18. 18

    将 props 的函数传递给 react 中的 render 函数

  19. 19

    如何将 props(从 Redux 获取的)从一个组件传递到另一个(React/Redux)?

  20. 20

    通过 props 动态传递 React 组件?

  21. 21

    如何将React Hook传递给子组件

  22. 22

    如何将函数作为属性传递给React组件?

  23. 23

    如何将道具传递给子React组件?

  24. 24

    如何将属性传递给 React 函数组件?

  25. 25

    React - 如何将道具传递给孙子组件?

  26. 26

    如何将状态className变量传递给React中的另一个组件

  27. 27

    如何将节点作为道具传递给React中的组件?

  28. 28

    如何将组件回调传递给react-router中的children元素?

  29. 29

    如何将值传递给 React 中的其他组件

热门标签

归档