小编Mic*_*elH的帖子

React 仅在渲染 Child 时才渲染父级

我想知道是否有办法只在渲染子组件时渲染父组件,反之亦然。如果你用谷歌搜索标题,你会发现很多条目,但对我的情况没有任何作用,因为我的元素不是undefinednull,例如。想象一个家长:

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)

javascript reactjs

5
推荐指数
1
解决办法
3327
查看次数

Template.instance()和this之间的区别

Template.instance()和这个有什么区别?使用其中一个是否有优势?

Template.name.onRendered( function() {
 var template = Template.instance(); 
 var instance = this;
});
Run Code Online (Sandbox Code Playgroud)

meteor

2
推荐指数
1
解决办法
728
查看次数

标签 统计

javascript ×1

meteor ×1

reactjs ×1