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 on mount when deps are not an array #15018

Merged
merged 3 commits into from
Mar 5, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 11 additions & 0 deletions packages/react-reconciler/src/ReactFiberHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -1265,6 +1265,17 @@ if (__DEV__) {
): void {
currentHookNameInDev = 'useEffect';
mountHookTypesDev();
if (deps !== undefined && deps !== null && !Array.isArray(deps)) {
// Verify deps, but only on mount to avoid extra checks.
// It's unlikely their type would change as usually you define them inline.
warning(
false,
'%s received a final argument that is not an array (instead, received `%s`). When ' +
'specified, the final argument must be an array.',
currentHookNameInDev,
typeof deps,
);
}
return mountEffect(create, deps);
},
useImperativeHandle<T>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,36 @@ describe('ReactHooks', () => {
]);
});

it('warns if deps is not an array', () => {
const {useEffect} = React;

function App(props) {
useEffect(() => {}, props.deps);
return null;
}
expect(() => {
ReactTestRenderer.create(<App deps={'hello'} />);
}).toWarnDev([
'Warning: useEffect received a final argument that is not an array (instead, received `string`). ' +
'When specified, the final argument must be an array.',
]);
expect(() => {
ReactTestRenderer.create(<App deps={100500} />);
}).toWarnDev([
'Warning: useEffect received a final argument that is not an array (instead, received `number`). ' +
'When specified, the final argument must be an array.',
]);
expect(() => {
ReactTestRenderer.create(<App deps={{}} />);
}).toWarnDev([
'Warning: useEffect received a final argument that is not an array (instead, received `object`). ' +
'When specified, the final argument must be an array.',
]);
ReactTestRenderer.create(<App deps={[]} />);
ReactTestRenderer.create(<App deps={null} />);
ReactTestRenderer.create(<App deps={undefined} />);
});

it('assumes useEffect clean-up function is either a function or undefined', () => {
const {useLayoutEffect} = React;

Expand Down