1- import useMounted from '@restart/hooks/useMounted' ;
2- import invariant from 'invariant' ;
3- import React , { useCallback , useContext , useState } from 'react' ;
1+ import React , {
2+ useCallback ,
3+ useContext ,
4+ useEffect ,
5+ useRef ,
6+ useState ,
7+ } from 'react' ;
48import { ReactRelayContext , commitMutation } from 'react-relay' ;
59import {
610 MutationConfig as BaseMutationConfig ,
@@ -40,7 +44,14 @@ export function useMutation<T extends MutationParameters>(
4044 error : null ,
4145 } ) ;
4246
43- const isMounted = useMounted ( ) ;
47+ const mounted = useRef ( true ) ;
48+
49+ useEffect (
50+ ( ) => ( ) => {
51+ mounted . current = false ;
52+ } ,
53+ [ ] ,
54+ ) ;
4455
4556 const relayContext = useContext ( ReactRelayContext ) ;
4657 const resolvedEnvironment = environment || relayContext ! . environment ;
@@ -69,7 +80,9 @@ export function useMutation<T extends MutationParameters>(
6980 ...config ,
7081 } ;
7182
72- invariant ( mergedConfig . variables , 'you must specify variables' ) ;
83+ if ( ! mergedConfig . variables ) {
84+ throw Error ( 'you must specify variables' ) ;
85+ }
7386
7487 setState ( {
7588 loading : true ,
@@ -79,7 +92,7 @@ export function useMutation<T extends MutationParameters>(
7992
8093 return new Promise ( ( resolve , reject ) => {
8194 function handleError ( error : any ) {
82- if ( isMounted ( ) ) {
95+ if ( mounted . current ) {
8396 setState ( {
8497 loading : false ,
8598 data : null ,
@@ -106,7 +119,7 @@ export function useMutation<T extends MutationParameters>(
106119 return ;
107120 }
108121
109- if ( isMounted ( ) ) {
122+ if ( mounted . current ) {
110123 setState ( {
111124 loading : false ,
112125 data : response ,
@@ -134,7 +147,6 @@ export function useMutation<T extends MutationParameters>(
134147 optimisticUpdater ,
135148 optimisticResponse ,
136149 updater ,
137- isMounted ,
138150 ] ,
139151 ) ;
140152
0 commit comments