Skip to content

Commit

Permalink
backport #3739
Browse files Browse the repository at this point in the history
  • Loading branch information
JoviDeCroock committed Oct 26, 2022
1 parent 0d21c42 commit 1660ed1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion hooks/src/index.js
Expand Up @@ -216,7 +216,7 @@ export function useReducer(reducer, initialState, init) {
}
});

return shouldUpdate
return shouldUpdate || hookState._internal.props !== p
? prevScu
? prevScu.call(this, p, s, c)
: true
Expand Down
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 1660ed1

Please sign in to comment.