You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
import{gql,useApolloClient,useQuery}from"@apollo/client"import{useCallback}from"react"exportconstmodalQuery=gql` query GetModal { modal @client { name props } }`constuseModal=()=>{constclient=useApolloClient()const{ data }=useQuery(modalQuery)constopen=useCallback(({ name, props ={}}: {name: string;props?: any})=>{// Warning: Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in a useEffect cleanup function.client.writeQuery({query: modalQuery,data: {modal: { name, props }},})},[client])constclose=useCallback(()=>{client.writeQuery({query: modalQuery,data: {modal: null},})},[client])return{modal: data?.modal,
close,
open,}}exportdefaultuseModal
I use this hook in two components on different routes.
I use open function to write data to cache in first component it shows modal and close modal(write to cache)
Then I go to second route, and first component unmount, and when I use open it shows warning on first component that I display in useModal code chunk
First Component
import{Box,Button}from"@material-ui/core"importReactfrom"react"import{useModal,useProjectsQuery}from"../../app"importProjectsListfrom"../components/projects-list"constProjectsView=()=>{const{ data }=useProjectsQuery()const{ open }=useModal()return(<Box><Buttonvariant="contained"size="small"color="primary"onClick={()=>open({name: "project-form"})}>CreateProject</Button><ProjectsListprojects={data?.projects}/></Box>)}exportdefaultProjectsView
import{ModalasMuModal}from"@material-ui/core"importReactfrom"react"import{useModal}from"../../lib"import{modalMap}from"./constants"import{Container}from"./styles"constModal=()=>{const{ modal, close }=useModal()constModalComponent=modalMap[modal?.name]return(<MuModalopen={Boolean(ModalComponent)}onClose={()=>close()}><Container>{ModalComponent &&(<ModalComponent{...modal?.props}onClose={()=>{close()if(modal?.props?.onClose)modal?.props?.onClose()}}/>)}</Container></MuModal>)}exportdefaultModal
As I understand useApolloClient don't unsubscribe on unmount. How I can fix it ?
Version: "@apollo/client": "^3.0.0-beta.43"
The text was updated successfully, but these errors were encountered:
I have custom hook that use
useApolloClient
Hook
I use this hook in two components on different routes.
I use
open
function to write data to cache in first component it shows modal andclose
modal(write to cache)Then I go to second route, and first component unmount, and when I use
open
it shows warning on first component that I display inuseModal
code chunkFirst Component
Second Component
Modal Component
As I understand
useApolloClient
don't unsubscribe on unmount. How I can fix it ?Version:
"@apollo/client": "^3.0.0-beta.43"
The text was updated successfully, but these errors were encountered: