diff --git a/package.json b/package.json index 92e772b..c1a7fdf 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,7 @@ "homepage": "https://reactjs.org/", "dependencies": { "object-assign": "^4.1.1", - "react-is": "^16.12.0 || ^17.0.0" + "react-is": "^16.12.0 || ^17.0.0 || ^18.0.0" }, "devDependencies": { "@babel/cli": "^7.8.4", @@ -45,14 +45,14 @@ "jest-diff": "^25.1.0", "lint-staged": "^10.0.8", "prettier": "1.19.1", - "react": "^16.12.0", + "react": "^18.0.0", "rimraf": "^3.0.1", "rollup": "^1.30.1", "rollup-plugin-babel": "^4.3.3", "rollup-plugin-strip-banner": "^1.0.0" }, "peerDependencies": { - "react": "^16.0.0 || ^17.0.0" + "react": "^16.0.0 || ^17.0.0 || ^18.0.0" }, "files": [ "LICENSE", diff --git a/src/ReactShallowRenderer.js b/src/ReactShallowRenderer.js index 5c59539..4f33ca5 100644 --- a/src/ReactShallowRenderer.js +++ b/src/ReactShallowRenderer.js @@ -169,6 +169,7 @@ class ReactShallowRenderer { this._didScheduleRenderPhaseUpdate = false; this._renderPhaseUpdates = null; this._numberOfReRenders = 0; + this._idCounter = 0; } _validateCurrentlyRenderingComponent() { @@ -311,7 +312,6 @@ See https://fb.me/react-invalid-hook-call for tips about how to debug and fix th responder, }); - // TODO: implement if we decide to keep the shallow renderer const useTransition = config => { this._validateCurrentlyRenderingComponent(); const startTransition = callback => { @@ -320,12 +320,22 @@ See https://fb.me/react-invalid-hook-call for tips about how to debug and fix th return [startTransition, false]; }; - // TODO: implement if we decide to keep the shallow renderer const useDeferredValue = (value, config) => { this._validateCurrentlyRenderingComponent(); return value; }; + const useId = () => { + this._validateCurrentlyRenderingComponent(); + const nextId = ++this._idCounter; + return ':r' + nextId + ':'; + }; + + const useSyncExternalStore = (subscribe, getSnapshot) => { + this._validateCurrentlyRenderingComponent(); + return getSnapshot(); + }; + return { readContext, useCallback: identity, @@ -337,13 +347,16 @@ See https://fb.me/react-invalid-hook-call for tips about how to debug and fix th useEffect: noOp, useImperativeHandle: noOp, useLayoutEffect: noOp, + useInsertionEffect: noOp, useMemo, useReducer, useRef, useState, useResponder, + useId, useTransition, useDeferredValue, + useSyncExternalStore, }; } @@ -437,11 +450,13 @@ See https://fb.me/react-invalid-hook-call for tips about how to debug and fix th // Start over from the beginning of the list this._workInProgressHook = null; this._rendering = false; + this._idCounter = 0; this.render(element, context); } else { this._workInProgressHook = null; this._renderPhaseUpdates = null; this._numberOfReRenders = 0; + this._idCounter = 0; } } diff --git a/src/__tests__/ReactShallowRenderer-test.js b/src/__tests__/ReactShallowRenderer-test.js index 84b52ad..bdca2d1 100644 --- a/src/__tests__/ReactShallowRenderer-test.js +++ b/src/__tests__/ReactShallowRenderer-test.js @@ -1210,8 +1210,8 @@ describe('ReactShallowRenderer', () => { shallowRenderer.render(React.createElement(SimpleComponent, {name: 123})), ).toErrorDev( 'Warning: Failed prop type: Invalid prop `name` of type `number` ' + - 'supplied to `SimpleComponent`, expected `string`.\n' + - ' in SimpleComponent', + 'supplied to `SimpleComponent`, expected `string`.', + {withoutStack: true}, ); }); diff --git a/src/__tests__/ReactShallowRendererHooks-test.js b/src/__tests__/ReactShallowRendererHooks-test.js index dabeb51..dbeceb4 100644 --- a/src/__tests__/ReactShallowRendererHooks-test.js +++ b/src/__tests__/ReactShallowRendererHooks-test.js @@ -233,8 +233,12 @@ describe('ReactShallowRenderer with hooks', () => { effectsCalled.push('useEffect'); }); + React.useInsertionEffect(() => { + effectsCalled.push('useInsertionEffect'); + }); + React.useLayoutEffect(() => { - effectsCalled.push('useEffect'); + effectsCalled.push('useLayoutEffect'); }); return