Skip to content

Commit

Permalink
Merge pull request preactjs#1584 from developit/fix/useState
Browse files Browse the repository at this point in the history
(fix) - useState can potentially add to the state queue and make the _dirty call neglect this
  • Loading branch information
marvinhagemeister committed Apr 30, 2019
2 parents 9d2fb10 + a056d8f commit 66f48d8
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
36 changes: 36 additions & 0 deletions hooks/test/browser/useState.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,40 @@ describe('useState', () => {
rerender();
expect(scratch.textContent).to.include('Count: 10');
});

it('should handle queued useState', () => {
function Message({ message, onClose }) {
const [isVisible, setVisible] = useState(Boolean(message));
const [prevMessage, setPrevMessage] = useState(message);

if (message !== prevMessage) {
setPrevMessage(message);
setVisible(Boolean(message));
}

if (!isVisible) {
return null;
}
return <p onClick={onClose}>{message}</p>;
}

function App() {
const [message, setMessage] = useState('Click Here!!');
return (
<Message
onClose={() => {
setMessage('');
}}
message={message}
/>
);
}

render(<App />, scratch);
expect(scratch.textContent).to.equal('Click Here!!');
const text = scratch.querySelector('p');
text.click();
rerender();
expect(scratch.innerHTML).to.equal('');
});
});
2 changes: 1 addition & 1 deletion src/diff/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@ export function diff(dom, parentDom, newVNode, oldVNode, context, isSvg, excessD
if (options.render) options.render(newVNode);

let prev = c._prevVNode || null;
let vnode = c._prevVNode = coerceToVNode(c.render(c.props, c.state, c.context));
c._dirty = false;
let vnode = c._prevVNode = coerceToVNode(c.render(c.props, c.state, c.context));

if (c.getChildContext!=null) {
context = assign(assign({}, context), c.getChildContext());
Expand Down

0 comments on commit 66f48d8

Please sign in to comment.