-
Notifications
You must be signed in to change notification settings - Fork 27.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
redux context in root layout not applying to sub page layouts #49557
Comments
Facing same issue when using useAppDispatch exported from redux store |
I see there has been discussions on this regarding context not showing up on #41994 as well since oct 2022. Hope this can get fixed soon! |
Have you placed your redux provider in a wrapper component with "use client" directive? Otherwise it won't work properly as Redux requires context, which is not available in RSC. |
yes-- I have a "providers" component that looks like this: https://github.com/wpcodevo/nextjs13-redux-toolkit/blob/main/src/redux/provider.tsx has use client in that wraps around all children in root. this results in issue with context |
I just ran into a similar problem where a theme provider in the root layout would have no effect on the server-rendered output of my page that is marked with The solution was to refactor my page component to be a Server Component again (removing
Note: Since the server component is not dynamic, it will only be run during the build and won't require a server. So static export should still work. |
Thanks for sharing this solution. Does it mean ‘use client’ is not working in page files |
I tried out @OhKai solution, though I get context errors in terminal when hard reloading / or reloading / loading the specific page with redux. For "The solution was to refactor my page component to be a Server Component again" Layout is already default as server so making page component server is kind of redundant I think. I ended up not using redux all together and went with zustand for the time being. Currently all refactored state code works due to zustand not needing a context wrapper |
To be clear, what worked for me was turning my page.tsx from a client component ("use client" at top) to a server component (no "use client" instead import with "use client"). I am not talking about layout.tsx here, that was always a server component. And I don't think it is redudant, in fact, in production on client navigations, your layout won't even be rendered on the server, just the page. They are pretty independent so that the layout can serve multiple pages without rerendering. |
Please what's the update on this issue as I'm currently encountering it. |
Refer to nextjs's example https://github.com/vercel/next.js/tree/canary/examples/with-redux Though I recommend just going with zustand to avoid headaches |
Verify canary release
Provide environment information
Which area(s) of Next.js are affected? (leave empty if unsure)
layouts / app router
Link to the code that reproduces this issue
https://github.com/wpcodevo/nextjs13-redux-toolkit/tree/main
To Reproduce
create sub page layouts that uses redux with redux context wrapping children in root layout.
Describe the Bug
for context, I am using this repo to get redux up and running my app router/ next 13 stable project:
https://github.com/wpcodevo/nextjs13-redux-toolkit/tree/main
issue: I have context wrapped in root layout, though have page layouts.
The root layout here contains the redux provider as seen above
When I try to have redux dispatch / selectors in page layouts, I get errors:
In this case, I have a plan-layout for the plan page with selectors.
I should not need a second provider in plan layout for this to work as I already have a provider in the root layout. If i add a temprorary provider in my plan-page layout, the error seems to go away.
Expected Behavior
context that is applied in root layout should be applied to all sub page layouts as described in the docs : "If you were to combine the two layouts above, the root layout (app/layout.js) would wrap the dashboard layout "
In this case, the redux context would wrap around the page layouts.
Which browser are you using? (if relevant)
No response
How are you deploying your application? (if relevant)
No response
The text was updated successfully, but these errors were encountered: