我想知道是否有办法只在渲染子组件时渲染父组件,反之亦然。如果你用谷歌搜索标题,你会发现很多条目,但对我的情况没有任何作用,因为我的元素不是undefined或null,例如。想象一个家长:
class Parent extends React.Component {
constructor(props){
super(props);
}
render() {
return (
<div>
<div className="container">
{this.props.children}
</div>
</div>
);
}
}
Run Code Online (Sandbox Code Playgroud)
以下子项可能取决于某些设置。例如,仅显示布尔值是否为真。
class Child extends React.Component {
constructor(props){
super(props);
const showChild = Settings.get(...);
this.state = {
visible:showChild
}
}
render() {
const showHello = this.state.visible ? <div>Hello</div> : "";
return (
<div>{showHello}</div>
);
}
}
Run Code Online (Sandbox Code Playgroud)
在我的布局中,我像这样搭建脚手架:
class Layout extends React.Component {
constructor(props){
super(props);
}
render() {
return (
<Parent>
<Child />
</Parent>
);
}
} …Run Code Online (Sandbox Code Playgroud) Template.instance()和这个有什么区别?使用其中一个是否有优势?
Template.name.onRendered( function() {
var template = Template.instance();
var instance = this;
});
Run Code Online (Sandbox Code Playgroud)