Skip to content

Commit

Permalink
fix equality for non root children (#3739)
Browse files Browse the repository at this point in the history
  • Loading branch information
JoviDeCroock committed Sep 23, 2022
1 parent 2cafede commit a868b02
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
2 changes: 1 addition & 1 deletion hooks/src/index.js
Expand Up @@ -233,7 +233,7 @@ export function useReducer(reducer, initialState, init) {
}
});

return shouldUpdate
return shouldUpdate || hookState._component.props !== p
? prevScu
? prevScu.call(this, p, s, c)
: true
Expand Down
2 changes: 1 addition & 1 deletion hooks/test/browser/errorBoundary.test.js
Expand Up @@ -102,9 +102,9 @@ describe('errorBoundary', () => {
resetErr();
render(<App onError={spy2} />, scratch);
rerender();
expect(scratch.innerHTML).to.equal('<p>Error</p>');
expect(spy).to.be.calledOnce;
expect(spy2).to.be.calledOnce;
expect(spy2).to.be.calledWith(error);
expect(scratch.innerHTML).to.equal('<p>Error</p>');
});
});
25 changes: 25 additions & 0 deletions hooks/test/browser/useState.test.js
Expand Up @@ -346,4 +346,29 @@ describe('useState', () => {

expect(renderSpy).to.be.calledTwice;
});

// see preactjs/preact#3731
it('respects updates initiated from the parent', () => {
let setChild, setParent;
const Child = props => {
const [, setState] = useState(false);
setChild = setState;
return <p>{props.text}</p>;
};

const Parent = () => {
const [state, setState] = useState('hello world');
setParent = setState;
return <Child text={state} />;
};

render(<Parent />, scratch);
expect(scratch.innerHTML).to.equal('<p>hello world</p>');

setParent('hello world!!!');
setChild(true);
setChild(false);
rerender();
expect(scratch.innerHTML).to.equal('<p>hello world!!!</p>');
});
});

0 comments on commit a868b02

Please sign in to comment.