[英]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.