@@ -3915,6 +3915,60 @@ describe('ReactHooksWithNoopRenderer', () => {
3915
3915
expect ( ReactNoop ) . toMatchRenderedOutput ( '2' ) ;
3916
3916
} ) ;
3917
3917
3918
+ it ( 'useReducer does not replay previous no-op actions when other state changes' , ( ) => {
3919
+ let increment ;
3920
+ let setDisabled ;
3921
+
3922
+ function Counter ( ) {
3923
+ const [ disabled , _setDisabled ] = useState ( true ) ;
3924
+ const [ count , dispatch ] = useReducer ( ( state , action ) => {
3925
+ if ( disabled ) {
3926
+ return state ;
3927
+ }
3928
+ if ( action . type === 'increment' ) {
3929
+ return state + 1 ;
3930
+ }
3931
+ return state ;
3932
+ } , 0 ) ;
3933
+
3934
+ increment = ( ) => dispatch ( { type : 'increment' } ) ;
3935
+ setDisabled = _setDisabled ;
3936
+
3937
+ Scheduler . unstable_yieldValue ( 'Render disabled: ' + disabled ) ;
3938
+ Scheduler . unstable_yieldValue ( 'Render count: ' + count ) ;
3939
+ return count ;
3940
+ }
3941
+
3942
+ ReactNoop . render ( < Counter /> ) ;
3943
+ expect ( Scheduler ) . toFlushAndYield ( [
3944
+ 'Render disabled: true' ,
3945
+ 'Render count: 0' ,
3946
+ ] ) ;
3947
+ expect ( ReactNoop ) . toMatchRenderedOutput ( '0' ) ;
3948
+
3949
+ act ( ( ) => {
3950
+ // These increments should have no effect, since disabled=true
3951
+ increment ( ) ;
3952
+ increment ( ) ;
3953
+ increment ( ) ;
3954
+ } ) ;
3955
+ expect ( Scheduler ) . toHaveYielded ( [
3956
+ 'Render disabled: true' ,
3957
+ 'Render count: 0' ,
3958
+ ] ) ;
3959
+ expect ( ReactNoop ) . toMatchRenderedOutput ( '0' ) ;
3960
+
3961
+ act ( ( ) => {
3962
+ // Enabling the updater should *not* replay the previous increment() actions
3963
+ setDisabled ( false ) ;
3964
+ } ) ;
3965
+ expect ( Scheduler ) . toHaveYielded ( [
3966
+ 'Render disabled: false' ,
3967
+ 'Render count: 0' ,
3968
+ ] ) ;
3969
+ expect ( ReactNoop ) . toMatchRenderedOutput ( '0' ) ;
3970
+ } ) ;
3971
+
3918
3972
it ( 'useReducer does not replay previous no-op actions when props change' , ( ) => {
3919
3973
let setDisabled ;
3920
3974
let increment ;
0 commit comments