File tree Expand file tree Collapse file tree 1 file changed +21
-0
lines changed
packages/react-reconciler/src/__tests__ Expand file tree Collapse file tree 1 file changed +21
-0
lines changed Original file line number Diff line number Diff line change @@ -690,6 +690,27 @@ describe('ReactHooks', () => {
690
690
) ;
691
691
} ) ;
692
692
693
+ it ( 'throws when reading context inside useMemo after outside it' , ( ) => {
694
+ const { useMemo, createContext} = React ;
695
+ const ReactCurrentDispatcher =
696
+ React . __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED
697
+ . ReactCurrentDispatcher ;
698
+
699
+ const ThemeContext = createContext ( 'light' ) ;
700
+ let firstRead ;
701
+ function App ( ) {
702
+ firstRead = ReactCurrentDispatcher . current . readContext ( ThemeContext ) ;
703
+ return useMemo ( ( ) => {
704
+ return ReactCurrentDispatcher . current . readContext ( ThemeContext ) ;
705
+ } , [ ] ) ;
706
+ }
707
+
708
+ expect ( ( ) => ReactTestRenderer . create ( < App /> ) ) . toThrow (
709
+ 'Context can only be read inside the body of a component' ,
710
+ ) ;
711
+ expect ( firstRead ) . toBe ( 'light' ) ;
712
+ } ) ;
713
+
693
714
it ( 'throws when reading context inside useEffect' , ( ) => {
694
715
const { useEffect, createContext} = React ;
695
716
const ReactCurrentDispatcher =
You can’t perform that action at this time.
0 commit comments