Skip to content

Commit

Permalink
Dont call memo comparison function on initial render
Browse files Browse the repository at this point in the history
  • Loading branch information
Brandon Dail authored and gaearon committed Mar 15, 2019
1 parent 75dcd59 commit 0dbe1fd
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/react-test-renderer/src/ReactShallowRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,8 @@ class ReactShallowRenderer {
let shouldRender = true;
if (
isMemo(element.type) &&
elementType === this._previousComponentIdentity
elementType === this._previousComponentIdentity &&
previousElement !== null
) {
// This is a Memo component that is being re-rendered.
const compare = element.type.compare || shallowEqual;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1510,6 +1510,17 @@ describe('ReactShallowRenderer', () => {
expect(renderCount).toBe(2);
});

it('should not call the comparison function with React.memo on the initial render', () => {
const areEqual = jest.fn(() => false);
const SomeComponent = React.memo(({foo}) => {
return <div>{foo}</div>;
}, areEqual);
const shallowRenderer = createRenderer();
shallowRenderer.render(<SomeComponent foo={1} />);
expect(areEqual).not.toHaveBeenCalled();
expect(shallowRenderer.getRenderOutput()).toEqual(<div>1</div>);
});

it('should handle memo(forwardRef())', () => {
const testRef = React.createRef();
const SomeComponent = React.forwardRef((props, ref) => {
Expand Down

0 comments on commit 0dbe1fd

Please sign in to comment.