简体   繁体   English

在React组件内渲染React元素

[英]Render a React element inside a React component

OK I'm trying to pass a component inside an object I use as a parameters to an action I'm triggering. 确定,我正在尝试将用作参数的对象内的组件传递给我触发的动作。

this.context.alt.actions.notificationActions.logMessage({
  component: <ModalLayoutEditorComments subscription="pop-up" contextualClass="info" callback={this._submitCaisseChangeAction} />,
  subscription: 'pop-up',
});

Inside the target component I receive the object as props with the component which is now React Element. 在目标组件内部,我将对象与现在为React Element的组件作为道具接收。

反应对象内部的元素

Now I would to know how I can convert this React element inside another React component if possible? 现在我想知道如何在可能的情况下在另一个React组件中转换这个React元素?

  render() {
      const Component = this.props.notifications[0].component;

      return (
        <div>
          {Component}
        </div>
      );
  }

Your render logic is OK. 您的render逻辑还可以。 It looks like you are using the wrong prop: 看来您使用的是错误的道具:

render() {    
  return (
    <div>
      {this.props.notifications[0].component}
    </div>
  );
}

As an aside, component is quite a confusing property name. 顺便说一句, component是一个非常令人困惑的属性名称。 The object is a React element, which is a description of a Component instance . 该对象是一个React元素,它是Component实例描述

The error I was getting was the following 我得到的错误是以下

Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined 元素类型无效:需要一个字符串(对于内置组件)或一个类/函数(对于复合组件),但得到:未定义

I was getting undefined because the mistake I made was I was not exporting the component using export default ModalLayoutEditorComments . 我变得不确定,因为我犯的错误是我没有使用export default ModalLayoutEditorComments导出组件。

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

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