React's Context API #39061
Unanswered
andrii-khan
asked this question in
Help
React's Context API
#39061
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi community, when using the context as described here
I have some problems, for some reason my request for user data is sent twice.
here is my gatsby-browser and gatsby-ssr
import React from 'react';
import RootElement from './src/components/RootElement';
export const wrapRootElement = ({element}) => {
return {element};
};
root-element.js
import React from 'react';
import {UserProvider} from "./_contexts/UserContext";
import {PopupProvider} from "./_contexts/PopupContext";
import {LocationProvider} from "./_contexts/LocationContext";
const RootElement = ({children}) => {
return (
{children}
);
};
export default RootElement;
and my location provider
import React, { createContext, useState, useEffect, useMemo } from 'react';
import { useACFOptions } from "../../_hooks/useACFOptions";
export const LocationContext = createContext({
location: {
countryCode: null,
timezone: null
},
isInEU: false,
isInUSA: false,
loading: true
});
export const LocationProvider = ({ children }) => {
const [location, setLocation] = useState({
countryCode: null,
timezone: null
});
const [isInUSA, setIsInUSA] = useState(false);
const [isInEU, setIsInEU] = useState(false);
const [loading, setLoading] = useState(true);
const { quizSettings } = useACFOptions();
const EUCountryCodes = useMemo(() => {
return quizSettings?.euRegion.split('/').map(code => code.trim().toUpperCase()) || [];
}, [quizSettings]);
const USACountryCodes = useMemo(() => {
return quizSettings?.usaRegion.split('/').map(code => code.trim().toUpperCase()) || [];
}, [quizSettings]);
useEffect(() => {
const storedCountryCode = sessionStorage.getItem('countryCode') || null;
const storedTimezone = sessionStorage.getItem('timezone') || null;
}, []);
return (
<LocationContext.Provider value={{ location, isInEU, isInUSA, loading }}>
{children}
</LocationContext.Provider>
);
};
Maybe someone can help me or has already faced a similar problem?
Beta Was this translation helpful? Give feedback.
All reactions