Skip to content

Commit

Permalink
useDebugValue should throw if used in a class component (#14601)
Browse files Browse the repository at this point in the history
  • Loading branch information
bvaughn committed Jan 15, 2019
1 parent 153a0b5 commit 4392e38
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
3 changes: 3 additions & 0 deletions packages/react-reconciler/src/ReactFiberHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,9 @@ export function useDebugValue(
value: any,
formatterFn: ?(value: any) => any,
): void {
// This will trigger a warning if the hook is used in a non-Function component.
resolveCurrentlyRenderingFiber();

// This hook is normally a no-op.
// The react-debug-hooks package injects its own implementation
// so that e.g. DevTools can display custom hook values.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,23 @@ describe('ReactHooks', () => {
ReactDOMServer = require('react-dom/server');
});

if (__DEV__) {
// useDebugValue is a DEV-only hook
it('useDebugValue throws when used in a class component', () => {
class Example extends React.Component {
render() {
React.useDebugValue('abc');
return null;
}
}
expect(() => {
ReactTestRenderer.create(<Example />);
}).toThrow(
'Hooks can only be called inside the body of a function component.',
);
});
}

it('warns about variable number of dependencies', () => {
const {useLayoutEffect} = React;
function App(props) {
Expand Down

0 comments on commit 4392e38

Please sign in to comment.