【发布时间】:2019-02-26 18:28:02
【问题描述】:
我希望所有通知消息都具有相同的外观。我有一个父组件,两个子组件为其呈现不同的消息...一个普通消息,另一个组件根据上下文显示消息。
下面是代码,
class Parent extends React.Component {
constructor(props) {
super(props);
this.element = document.createElement('div');
this.element.className= 'wrapper';
}
componentDidMount() {
root.appendChild(this.element);
}}
class Child1 extends React.Component {
render = () => {
return (
<div onClick={this.close} className="notification_info">
soemthing
</div>);}}
class Child2 extends React.Component {
render = () => {
<div>
<h2>Debug</h2>
</div>}
let content;
if (content) {
return (
<div className="container">
<div className="main">
<div className="notification">
<div className="notification_info">{content}
</div>
</div>
</div>
<div className="close"><SvgClose width="28" /></div>
</div>);}}}
我想为 child1 和 child2 组件创建三个带有 className 容器、main、通知的 div。我想将与 child2 中相同的 div 添加到 child1 以及在具有类名 notification_info 的 div 之前。
<div className="container">
<div className="main">
<div className="notification">
我尝试做的是创建三个带有类名容器的 div,main,以及 child1 组件的通知。这也将为正常和上下文消息创建容器。但后来我正在寻找其他方法来做到这一点。重构它或干净的方式来做它。
有人可以帮我解决这个问题吗?谢谢。
【问题讨论】:
-
您能否减少您的示例并提供一个不包含无关部分的代码 sn-p。您可以在这里找到提示:How to create a Minimal, Complete, and Verifiable example 提前致谢。
-
有人可以帮忙吗?