This fits conceptually very well with how we think about layouts as something thatās wrapping a page and contains any content thatās outside of the page itself.
And as an added bonus your layouts wonāt be re-rendered as soon as a page is re-rendered, or even when going from one page to another thatās in the same <Set>.
Wrapping with multiple components
In the example above the wrap prop took a single layout as its value. If you want to wrap more components around your routes, it will also accept an array: wrap={[BlogLayout, PageContext]}
I can migrate some of my pages to this new feature, but not all straight away: my layout has a breadcrumbs prop and an actions placeholder. Itās very cool to have those options though, and for my specific case I think I have no choice but to look for Context - which is cool.
Iāll try hook before context, great idea, Iām reluctant to play with contexts x).
My actions placeholder is a spot at the same level as the breadcrumbs.
Weāre building an application for school management, on the pages where we edit a Teacher, a Student, a Customer, a Session, we systematically have a button at the top to let us delete the entity weāre currently editing.
On some of those pages we have extra buttons, so we expect these to be dynamic :).
( thereās a preprod coming as soon as I manage to fix my netlify deploy, youād see in context how we use the actions placeholder )
@Tobbe
Eventually did it and migrated all appropriate routes to <Set />, also did what we discussed with a hook and context to handle my breadcrumbs and actions :), it high!
What ākindā of context can be used in the set attribute? (I donāt think itās the React hook useContext as I donāt see any mention of Providers/Consumers.)
Iāve searched through the docs and forums, but besides GQL related results, I didnāt find much about context and I donāt understand how to integrate the context needed for the breadcrumbs example above.
I think that could be extremely useful, but I actually ended up here after trying to display the current pageās title in the Layout component. Is there a way to add additional properties to a Route element in web/src/Routes.js that could be consumed in the parent layout? (I tried: useParams, useLocations, useRouterState.)
There are quite a few contexts to keep track of, but this one is React context. The Provider/Consumer is kind of implied, but to make things more explicit, in @Tobbeās breadcrumbs example, the Set component would have the BreadcrumbsProvider:
Youāve got to be careful using context like this though. Context is better for dependency managementājust passing something down the tree. Once you start mutating context, you get a lot of rerenders and might blow away your state further down the tree.
Passing information from the pages up to layouts is a very common thing to want to do. And Iāve been using context for that without issue in a lot of places. But if you do start noticing performance issues (I havenāt) Dom is right that contexts do often result in more re-renders than you might at first realize.
Thanks for asking. If more people start asking about this, maybe we should be thinking of a way to provide a built in solution for this.
Thank you both for the quick and informative replies. I have it wired up and Iām working on testing it out. I donāt think itāll get very complicated for this addition, but Iāll definitely keep an eye on the re-renders.