question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Make load hooks more versatile

See original GitHub issue

On 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:

  1. 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?
  2. Retrieve the categories in __layout.svelte, put them into a context and extract them where needed.
  3. Retrieve the categories in __layout.svelte and pass them to further pages via the stuff property. This would mean that the afterLoad hook may return an LoadOutput 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.sveltes 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:closed
  • Created 2 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
fehnomenalcommented, Mar 21, 2022

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 😅

1reaction
AlecAivaziscommented, Mar 15, 2022

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!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found