I wrote component on React Fiber. This component receive 1 cond props, and that is true, render children.
"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"));
Before React Fiber, this code works. But now, React Fiber causes error.
Uncaught TypeError: Cannot read property 'length' of null
I guess that React Fiber render children before component is rendered. But this behavior break component.
Can I stop prerender children of component?
The problem in your case isn't that react doesn't accept a
nullreturn value. (Which it doesReactComponent.render) But your the children will get evaluated before they get handed over to theIfcomponent which means thatstr.lengththrows the error you're seeing.Basic example based on your code.