Skip to content

Commit

Permalink
Fix React.lazy(forwardRef) (facebook#13446)
Browse files Browse the repository at this point in the history
* Fix React.lazy(forwardRef)

* Forbid bad typeof
  • Loading branch information
gaearon authored Aug 20, 2018
1 parent e8571c7 commit bf1abf4
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ module.exports = {
'quotes': [ERROR, 'single', {avoidEscape: true, allowTemplateLiterals: true }],
'space-before-blocks': ERROR,
'space-before-function-paren': OFF,
'valid-typeof': [ERROR, {requireStringLiterals: true}],

// React & JSX
// Our transforms set this automatically
Expand Down
2 changes: 1 addition & 1 deletion packages/react-reconciler/src/ReactFiberLazyComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export function readLazyComponentType<T>(thenable: Thenable<T>): T {
// resolved value itself.
const defaultExport = (resolvedValue: any).default;
resolvedValue =
typeof defaultExport !== undefined && defaultExport !== null
defaultExport !== undefined && defaultExport !== null
? defaultExport
: resolvedValue;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ describe('ReactSuspense', () => {

function Text(props) {
ReactNoop.yield(props.text);
return <span prop={props.text} />;
return <span prop={props.text} ref={props.hostRef} />;
}

function AsyncText(props) {
Expand Down Expand Up @@ -1600,6 +1600,41 @@ describe('ReactSuspense', () => {
}).toWarnDev(' in Text (at **)\n' + ' in Foo (at **)');
expect(ReactNoop.getChildren()).toEqual([div(span('A'), span('B'))]);
});

it('supports class and forwardRef components', async () => {
const LazyClass = lazy(() => {
class Foo extends React.Component {
render() {
return <Text text="Foo" />;
}
}
return Promise.resolve(Foo);
});

const LazyForwardRef = lazy(() => {
const Bar = React.forwardRef((props, ref) => (
<Text text="Bar" hostRef={ref} />
));
return Promise.resolve(Bar);
});

const ref = React.createRef();
ReactNoop.render(
<Placeholder fallback={<Text text="Loading..." />}>
<LazyClass />
<LazyForwardRef ref={ref} />
</Placeholder>,
);
expect(ReactNoop.flush()).toEqual(['Loading...']);
expect(ReactNoop.getChildren()).toEqual([]);
expect(ref.current).toBe(null);

await LazyClass;
await LazyForwardRef;
expect(ReactNoop.flush()).toEqual(['Foo', 'Bar']);
expect(ReactNoop.getChildren()).toEqual([span('Foo'), span('Bar')]);
expect(ref.current).not.toBe(null);
});
});

it('does not call lifecycles of a suspended component', async () => {
Expand Down

0 comments on commit bf1abf4

Please sign in to comment.