Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

React Context calling function indefinitely

I have this profile which I load from my context, but it is calling the function over and over. How do I make it be called only once or only when needed.
For example I have 2 Modals, where one be called if no profile exists and one where a profile exists. But when the second modal is called, I see profile called over and over, but I want it to be only called onLoad and when changes to the profile happen (update, delete Profile).

interface ProfileContext {
  openProfile: boolean
  setOpenProfile: React.Dispatch<React.SetStateAction<boolean>>
  profile: Profil | undefined
  setProfile: React.Dispatch<React.SetStateAction<Profil | undefined>>
}
const ProfileContext = React.createContext<ProfileContext>({
  openProfile: false,
  setOpenProfile: () => {
    return true
  },
  profile: undefined,
  setProfile: () => {
    return { profile: undefined }
  },
})

export const useProfileContext = (): ProfileContext => React.useContext(ProfileContext)

interface ProfileContextProviderProps {
  children: React.ReactNode
}
export const ProfileContextProvider = ({ children }: ProfileContextProviderProps): React.ReactElement => {
  const [openProfile, setOpenProfile] = React.useState<boolean>(false)
  const [profile, setProfile] = React.useState<Profil | undefined>(undefined)
  const { loadProfile, createProfile, deleteProfile } = useProfile()

  const loadProfileIntoContext = () => {
    const userProfile = loadProfile()
    userProfile.then((values) => {
      setProfile(values)
      console.log(profile)
    })
  }

  loadProfileIntoContext()

  return (
    <>
      <ProfileContext.Provider value={{ openProfile, setOpenProfile, profile, setProfile }}>
        {children}
      </ProfileContext.Provider>
    </>
  )
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

For these cases you should use useEffect with an empty array of deps

interface ProfileContext {
  openProfile: boolean
  setOpenProfile: React.Dispatch<React.SetStateAction<boolean>>
  profile: Profil | undefined
  setProfile: React.Dispatch<React.SetStateAction<Profil | undefined>>
}
const ProfileContext = React.createContext<ProfileContext>({
  openProfile: false,
  setOpenProfile: () => {
    return true
  },
  profile: undefined,
  setProfile: () => {
    return { profile: undefined }
  },
})

export const useProfileContext = (): ProfileContext => React.useContext(ProfileContext)

interface ProfileContextProviderProps {
  children: React.ReactNode
}
export const ProfileContextProvider = ({ children }: ProfileContextProviderProps): React.ReactElement => {
  const [openProfile, setOpenProfile] = React.useState<boolean>(false)
  const [profile, setProfile] = React.useState<Profil | undefined>(undefined)
  const { loadProfile, createProfile, deleteProfile } = useProfile()

  const loadProfileIntoContext = () => {
    const userProfile = loadProfile()
    userProfile.then((values) => {
      setProfile(values)
      console.log(profile)
    })
  }
  
  useEffect(() => {
     loadProfileIntoContext()
  }, [])

  return (
    <>
      <ProfileContext.Provider value={{ openProfile, setOpenProfile, profile, setProfile }}>
        {children}
      </ProfileContext.Provider>
    </>
  )
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading