简体   繁体   English

使用动态子组件对组件进行反应

[英]React Component with dynamic Sub-Components

I`m creating a react dynamic dialog where you can add funtionality to the Dialog. 我正在创建一个反应动态对话框,您可以在其中向对话框添加功能。

One way of doing this was with Composition, but i did not managed to do this via composition. 一种实现方法是使用Composition,但是我没有设法通过composition做到这一点。

I`m not very experienced on React, so this was my frist approach 我对React不太有经验,所以这是我的第一手方法

I got my Modal component, and the modal has 我有模态组件,模态有

export default class KneatModal extends React.Component {
constructor() {
  super();
  this.state = {
     open: false
  }
  this.components = [];

I would add components like this 我会添加这样的组件

import CommentField from '../../../Modal/ModalContents/CommentField.jsx'

export default class DoApprove extends React.Component {
constructor() {
  super();
}

componentDidMount() {
 this._buildDialog();
}

_buildDialog() {
  console.log("Building the Dialog"); 
  this.modal.components.push(CommentField);
}

In that modal renderer, i have 在那个模式渲染器中,我有

<ModalContent components={ this.components } />

Then i the final renderer in ModalContent i try to render all attached components 然后,我在ModalContent的最终渲染器中尝试渲染所有附加组件

 render() {
  var list = this.props.components.map((Component, key) => <Component/> ); 
  return (
     <div className='modal-contents'>
        {list}
     </div>
 )

} }

But the render method does not seems to work, i`ve tryed callin Component.render() instead of the component tag, but still could not make the sub-components render. 但是render方法似乎不起作用,我已经尝试调用Component.render()而不是component标签,但是仍然无法使子组件渲染。 =( =(

Would apreciate any help. 会感激任何帮助。 Thanks 谢谢

To be even more specific, this resumes on what im attempting 更具体地说,这会在我尝试

import PropTypes from 'prop-types';
import React from 'react';
import MyComponent1 from './MyComponent1.jsx'
import MyComponent2 from './MyComponent2.jsx'

export default class KneatModalContent extends React.Component {

   constructor() {
      super();
      this.components = [MyComponent1, MyComponent2];
   }

   render() {
     return (
     <div className='modal-contents'>
        {this.components.map(function (component, i) {
           return <{ component } key= { i } />
        })}
     </div>
 )
}

}

暂无
暂无

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

相关问题 Angular 2,不渲染动态组件的子组件 - Angular 2, sub-components of dynamic component are not rendered 使用带有 props 的子组件动态反应 - React dynamically using sub-components with props 在使用Jest单元测试React组件时如何模拟子组件 - How to mock out sub-components when unit testing a React component with Jest 当React / Flux中有多个小的可重复子组件时,如何管理组件渲染 - How to manage component rendering when there are several small, repeatable sub-components in React/Flux 如何让react-table的第一个子组件默认打开并手动打开其他子组件 - How to make first sub-component of react-table open by default and open other sub-components manually 如何正确地将一个组件的代码拆分为多个子组件? - How to properly split the code of a component into sub-components? Material UI如何使用整个组件的一种主要通用样式设置子组件的外部样式 - Material UI How to set external styles for sub-components with one main common style for the entire component 改变React状态的最佳方法 <App /> 基于子组件的级别 - Best way to change React state at <App /> level based on sub-components 反应在其中一个子组件中生成图像时如何在整个屏幕顶部显示图像 - React how to display an image on top of the entire screen when the image is generated in one of the sub-components 聚合物2.0:在Chrome的测试中未呈现的子组件 - Polymer 2.0: Sub-components not rendered in tests on Chrome
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM