Updating Next's Context/Provider before a page has loaded?
Unanswered
Bull Terrier posted this in #help-forum
Bull TerrierOP
I have wrapped my
And before this I do a simple fetch such as:
However, when I refresh the page, the state is updated after the page has loaded. Even though I am running the fetch server-sided I believe?
I am currently using Next.js's App Dir with a simple Context/Provider, and in the same
Below is my Provider:
layout.tsx in something like below:<ContentProvider data={data}>
{children}
</ContentProvider>And before this I do a simple fetch such as:
const res = await fetch(`/api/content`, {
method: "GET",
cache: "no-store",
headers: {
"Content-Type": "application/json"
}
});However, when I refresh the page, the state is updated after the page has loaded. Even though I am running the fetch server-sided I believe?
I am currently using Next.js's App Dir with a simple Context/Provider, and in the same
layout.tsx I am executing a fetch request. Even though the data is available, the context/provider is not ready until the page has loaded.Below is my Provider:
"use client"
export default function ContentProvider ({ data }) {
const [state, dispatch] = useReducer(...);
useEffect(() => {
console.log("State has updated:", state);
}, [state])
}20 Replies
Bull TerrierOP
Updated code above, still looking for a solution. Somehow seems to work in a regular component, the data is instantly ready. But not in a Provider?
Bull TerrierOP
Here is a CodePen of my example:
https://codesandbox.io/p/sandbox/objective-nova-2r4qx3
https://codesandbox.io/p/sandbox/objective-nova-2r4qx3
@Bull Terrier I have wrapped my `layout.tsx` in something like below:
js
<ContentProvider data={data}>
{children}
</ContentProvider>
And before this I do a simple fetch such as:
js
const res = await fetch(`/api/content`, {
method: "GET",
cache: "no-store",
headers: {
"Content-Type": "application/json"
}
});
However, when I refresh the page, the state is updated after the page has loaded. Even though I am running the fetch server-sided I believe?
I am currently using Next.js's App Dir with a simple Context/Provider, and in the same `layout.tsx` I am executing a `fetch` request. Even though the data is available, the context/provider is not ready until the page has loaded.
Below is my Provider:
js
"use client"
export default function ContentProvider ({ data }) {
const [state, dispatch] = useReducer(...);
useEffect(() => {
console.log("State has updated:", state);
}, [state])
}
Even though I am running the fetch server-sided I believe?No, you are running the fetch from the browser
if you fetch from the server,
/api/content doesn't work@joulev > Even though I am running the fetch server-sided I believe?
No, you are running the fetch from the browser
Bull TerrierOP
Ah ok, do you know what is causing the issue I explained above?
you have to use a full url like
https://domain.tld/api/contentBull TerrierOP
Even though every component is ready before page render, for some reason the context/provider does not update quick enough as the useEffect only loads once the page has fully rendered
@Bull Terrier Here is a CodePen of my example:
https://codesandbox.io/p/sandbox/objective-nova-2r4qx3
in this example though the fetch is run on the server yeah?
Bull TerrierOP
That's the same example that reflects my current code
But my provider/context useEffect is not updating quick enough
the useEffect is triggered on mount, that is normal, expected behaviour
useEffect is triggered when the dep array changes, but also on mountBull TerrierOP
Even if I put the console.log at the top of my provider
It is still delayed
const Provider ... => console.log("hi")
all i see is expected behaviour, nothing wrong with anything, what exactly is your question
it is not delayed,
useEffect only runs in the browser after react has been loaded so it will never be instantlyif you log the
state from the provider in the first render from the server it is already set from the initial argument so what exactly is the issue?Bull TerrierOP
Thanks solved
@joulev the useEffect is triggered on mount, that is normal, expected behaviour
Bull TerrierOP
This was the issue, thanks joulev