Skip to content
This repository has been archived by the owner on Apr 13, 2023. It is now read-only.

Commit

Permalink
Skip null array items in getDataFromTree (#1355)
Browse files Browse the repository at this point in the history
* Check for element === null and skip traversal.

* Add test cases for null in an array.
  • Loading branch information
dan-lee authored and James Baxley committed Nov 29, 2017
1 parent 276a14c commit e659ff9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/getDataFromTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ export function walkTree<Cache>(

return;
}

if (element === null) return;

const Component = element.type;
// a stateless functional component or a class
if (typeof Component === 'function') {
Expand Down
24 changes: 24 additions & 0 deletions test/react-web/server/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,16 @@ describe('SSR', () => {
expect(elementCount).toEqual(3);
});

it('function stateless components that render with a null in array', () => {
let elementCount = 0;

const MyComponent = () => [null, <div />];
walkTree(<MyComponent />, {}, element => {
elementCount += 1;
});
expect(elementCount).toEqual(2);
});

it('basic classes', () => {
let elementCount = 0;
class MyComponent extends React.Component<any, any> {
Expand Down Expand Up @@ -186,6 +196,20 @@ describe('SSR', () => {
expect(elementCount).toEqual(3);
});

it('basic classes components that render with a null in array', () => {
let elementCount = 0;

class MyComponent extends React.Component<any, any> {
render() {
return [null, <div />];
}
}
walkTree(<MyComponent />, {}, element => {
elementCount += 1;
});
expect(elementCount).toEqual(2);
});

it('basic classes with incomplete constructors', () => {
let elementCount = 0;
class MyComponent extends React.Component<any, any> {
Expand Down

0 comments on commit e659ff9

Please sign in to comment.