Make load hooks more versatile
See original GitHub issueOn the page I’m building with kit I need to display categories for a navigation component and want to highlight the active category (the currently viewed category or the category of the currently viewed entry). I see 3 possible approaches:
- I could just retrieve the categories on each page where I need them, extract somehow the active category and give those as props to the
Navigation
component. This would mean much duplicated code and I think I would need to find an unique name for each query, right? Also it would be not good for caching, right? - Retrieve the categories in
__layout.svelte
, put them into a context and extract them where needed. - Retrieve the categories in
__layout.svelte
and pass them to further pages via thestuff
property. This would mean that theafterLoad
hook may return anLoadOutput
object.
I think the third option would be the best (at least for kit, I did not look into how the kit code is wrapped for sapper) and implemented a PoC which works alright. Basically my modified code generates this as the load
return:
const _houdini_props = {
_GetCategoriesPublic,
_GetCategoriesPublic_Input: {},
_GetCategoriesPublic_Source
};
if ("props" in _houdini_context.returnValue)
return {
..._houdini_context.returnValue,
props: {
..._houdini_context.returnValue.props,
..._houdini_props
}
};
return {
props: {
..._houdini_context.returnValue,
..._houdini_props
}
};
The fallback means I have to return something for the props
key in __layout.svelte
s afterLoad
:
return {
props: {},
stuff: {
categories: data.GetCategoriesPublic.categories,
},
};
Would you be willing to implement something like this/accept a PR for this?
Then I would polish the code a bit and also handle returns from both beforeLoad
and afterLoad
.
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (1 by maintainers)
Top Results From Across the Web
Patterns to allow for versatile data-flow hooks in Javascript
Would there be a better pattern approach that I am not thinking about that would fulfill my requirements? Specifically: (1) hooking into parts ......
Read more >Use React hook to implement a self-increment counter
A more versatile approach would be to create a new custom hook that stores the function in a ref and only creates a...
Read more >Decoy Versatile Keeper Review - Wired2Fish
It works great for securing a trailer hook, which we'll talk more about in a minute. But as the Versatile in its name...
Read more >Lightweight but Heavy-Duty S-Hooks – Versatile – Comes ...
Use S-hooks to hang coffee mugs, pots and pans, kitchen utensils, grill utensils, docking, tools, towels, for towing purposes, lifting, hoisting, docking plants ......
Read more >Tune-Up Tuesday: J Hook or a Circle Hook - Tips and ...
The first hook we'll talk about is the J Hook. As I mentioned above, this hook is shaped like a J. This is...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Glad you like it and that I can contribute to this great project 😃
I will prepare a PR but will probably need your help for adding some test cases 😅
Hey @fehnomenal! I’m a little distracted at the moment with some other work for Houdini so I haven’t found the time to look at your proposal but I will get to it as soon as I have some time. I didn’t want you to think I was ignoring you. Thanks for being patient!