Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warn when second callback is passed to setState/dispatch in Hooks #14625

Merged
merged 1 commit into from
Jan 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions packages/react-reconciler/src/ReactFiberHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -782,6 +782,15 @@ function dispatchAction<S, A>(
'an infinite loop.',
);

if (__DEV__) {
warning(
arguments.length <= 3,
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could've been === 3 but I figured I don't want to punish users for our bugs.

"State updates from the useState() and useReducer() Hooks don't support the " +
'second callback argument. To execute a side effect after ' +
'rendering, declare it in the component body with useEffect().',
);
}

const alternate = fiber.alternate;
if (
fiber === currentlyRenderingFiber ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,70 @@ describe('ReactHooks', () => {
expect(root).toFlushAndYield(['Parent: 1, 2 (dark)']);
});

it('warns about setState second argument', () => {
const {useState} = React;

let setCounter;
function Counter() {
const [counter, _setCounter] = useState(0);
setCounter = _setCounter;

ReactTestRenderer.unstable_yield(`Count: ${counter}`);
return counter;
}

const root = ReactTestRenderer.create(null, {unstable_isConcurrent: true});
root.update(<Counter />);
expect(root).toFlushAndYield(['Count: 0']);
expect(root).toMatchRenderedOutput('0');

expect(() => {
setCounter(1, () => {
throw new Error('Expected to ignore the callback.');
});
}).toWarnDev(
'State updates from the useState() and useReducer() Hooks ' +
"don't support the second callback argument. " +
'To execute a side effect after rendering, ' +
'declare it in the component body with useEffect().',
{withoutStack: true},
);
expect(root).toFlushAndYield(['Count: 1']);
expect(root).toMatchRenderedOutput('1');
});

it('warns about dispatch second argument', () => {
const {useReducer} = React;

let dispatch;
function Counter() {
const [counter, _dispatch] = useReducer((s, a) => a, 0);
dispatch = _dispatch;

ReactTestRenderer.unstable_yield(`Count: ${counter}`);
return counter;
}

const root = ReactTestRenderer.create(null, {unstable_isConcurrent: true});
root.update(<Counter />);
expect(root).toFlushAndYield(['Count: 0']);
expect(root).toMatchRenderedOutput('0');

expect(() => {
dispatch(1, () => {
throw new Error('Expected to ignore the callback.');
});
}).toWarnDev(
'State updates from the useState() and useReducer() Hooks ' +
"don't support the second callback argument. " +
'To execute a side effect after rendering, ' +
'declare it in the component body with useEffect().',
{withoutStack: true},
);
expect(root).toFlushAndYield(['Count: 1']);
expect(root).toMatchRenderedOutput('1');
});

it('never bails out if context has changed', () => {
const {useState, useLayoutEffect, useContext} = React;

Expand Down