Next13 unable to trigger state in parent layout from child component
Answered
Tufted Flycatcher posted this in #help-forum
Tufted FlycatcherOP
I'm unable to trigger state changes in my parent layout component. I have a side menu for my dashboard pages, and the side menu is a layout file. In one of my pages, i need to be able to hit the
TV mode button, which should shut the side menu bar (boolean value) from the child component button, but there's no easy intuitive way to do this. Seems like all i found is something hacky20 Replies
@Tufted Flycatcher I'm unable to trigger state changes in my parent layout component. I have a side menu for my dashboard pages, and the side menu is a layout file. In one of my pages, i need to be able to hit the `TV mode` button, which should shut the side menu bar (boolean value) from the child component button, but there's no easy intuitive way to do this. Seems like all i found is something hacky
Can you give us some minimal code to illustrate current behaviour and what you want to have?
Tufted FlycatcherOP
in my layout.jsx, I have this:
const [sidebarOpen, setSidebarOpen] = useState(false) <Dialog
as="div"
className="relative z-50 lg:hidden"
onClose={setSidebarOpen}
> <main className="py-10">
<div className="px-4 sm:px-6 lg:px-8">{children}</div>
</main>and in my page.jsx, i have a button when i click it, it should be able to set setSidebarOpen to false. It's simple what i want to do
Please provide samples of solutions please
Can you make a minimal reproduction repository so I can clone and try? If you just give snippets then I can only guess and I’d rather not do that
Tufted FlycatcherOP
Yes, let me make a minimal reproduction. codesandbox link work?
Yes
Tufted FlycatcherOP
@joulev will this work: https://codesandbox.io/p/sandbox/inspiring-brattain-tlgq32?file=%2Fapp%2Flayout.tsx%3A94%2C1
also, i have a search form. I would like to type in that search form to filter out items in my page.jsx
@Tufted Flycatcher <@484037068239142956> will this work: https://codesandbox.io/p/sandbox/inspiring-brattain-tlgq32?file=%2Fapp%2Flayout.tsx%3A94%2C1
Can you simplify the layout? Since this problem doesn’t concern css it would be best to remove any styling and animation. Helpers here might not have the time and willpower to parse 300+ lines in a question
Basically, a reproduction repository means a very minimal nextjs without any unrelated bits that shows the problem this post concerns, so that other people can try to fix that problem from the code
Tufted FlycatcherOP
okay, look now: https://codesandbox.io/p/sandbox/inspiring-brattain-tlgq32?file=%2Fapp%2Flayout.tsx%3A17%2C8. All i want to do, it to toggle the value for sidebarOpen, in the page.tsx file with the button tag
for this you can use a context provider
Answer
Tufted FlycatcherOP
can you provide sample
Tufted FlycatcherOP
where do you use the provider? in the layout?
thank you