首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我可以在React Fiber上阻止预渲染的孩子吗?

我可以在React Fiber上阻止预渲染的孩子吗?
EN

Stack Overflow用户
提问于 2017-12-06 19:15:12
回答 0查看 139关注 0票数 0

我在React光纤上写了component。这个组件收到1个cond props,这是真的,渲染子组件。

代码语言:javascript
复制
"use strict";
import * as React from "react";
import * as ReactDOM from "react-dom";

interface IfProps {
  cond: boolean;
}

export class If extends React.Component<IfProps, {}> {
  render() {
    const {cond, children} = this.props;

    if (cond) {
      return children;
    } else {
      return null;
    }
  }
}

class TopComponent extends React.Component {
  render() {
    let str: any = null;

    return (
      <div>
        <If cond={str != null}>
          This part not showed.
          {str.length}
        </If>
      </div>
    );
  }
}

ReactDOM.render(<TopComponent />, document.getElementById("app"));

在React纤程之前,这段代码可以工作。但是现在,React纤程导致了错误。

代码语言:javascript
复制
Uncaught TypeError: Cannot read property 'length' of null

我猜React纤程会在组件渲染之前渲染子组件。但是这个行为破坏了组件。

我可以停止组件的预渲染子项吗?

EN

回答

页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47673003

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档