Skip to content
Open
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
18 changes: 18 additions & 0 deletions packages/react-reconciler/src/ReactFiberHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -2917,6 +2917,15 @@ function mountMemo<T>(
nextCreate: () => T,
deps: Array<mixed> | void | null,
): T {
if (__DEV__) {
if (typeof nextCreate !== 'function') {
console.error(
'Expected useMemo() first argument to be a function that returns a value. ' +
'Instead received: %s.',
nextCreate !== null ? typeof nextCreate : 'null',
);
}
}
const hook = mountWorkInProgressHook();
const nextDeps = deps === undefined ? null : deps;
const nextValue = nextCreate();
Expand All @@ -2936,6 +2945,15 @@ function updateMemo<T>(
nextCreate: () => T,
deps: Array<mixed> | void | null,
): T {
if (__DEV__) {
if (typeof nextCreate !== 'function') {
console.error(
'Expected useMemo() first argument to be a function that returns a value. ' +
'Instead received: %s.',
nextCreate !== null ? typeof nextCreate : 'null',
);
}
}
const hook = updateWorkInProgressHook();
const nextDeps = deps === undefined ? null : deps;
const prevState = hook.memoizedState;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -858,6 +858,33 @@ describe('ReactHooks', () => {
]);
});

// https://github.com/facebook/react/issues/16589
it('warns when useMemo is called with a non-function first argument', async () => {
const {useMemo} = React;
function App() {
// $FlowExpectedError[incompatible-call] Testing runtime behaviour.
useMemo({value: 1}, []);
return null;
}
App.displayName = 'App';

await expect(async () => {
await act(() => {
ReactTestRenderer.create(<App />, {unstable_isConcurrent: true});
});
}).rejects.toThrow('nextCreate is not a function');
// The warning fires twice because concurrent mode retries the render
// after the TypeError thrown when `nextCreate` is invoked.
assertConsoleErrorDev([
'Expected useMemo() first argument to be a function that returns a value. ' +
'Instead received: object.\n' +
' in App (at **)',
'Expected useMemo() first argument to be a function that returns a value. ' +
'Instead received: object.\n' +
' in App (at **)',
]);
});

// https://github.com/facebook/react/issues/14022
it('works with ReactDOMServer calls inside a component', async () => {
const {useState} = React;
Expand Down