Skip to content
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

[Bug]: context providers are lost during refetchRouteData and return undefined #972

Closed
2 tasks done
apatrida opened this issue Aug 8, 2023 · 1 comment
Closed
2 tasks done
Labels
bug Something isn't working

Comments

@apatrida
Copy link

apatrida commented Aug 8, 2023

Duplicates

  • I have searched the existing issues

Latest version

  • I have tested the latest version

Current behavior 😯

If you add a context provider, say around all routes in root.tsx tree directly, or in a layout around all routes, then you can useContext to get the values provided just fine.

They work:

  • in routeData function
  • in body of component function
  • in rendering
  • in createEffect function
  • in createResource function
  • in onMount function

BUT, if you do something like:

<div onClick={() => { refetchRouteData() }}>
                CLICK ME TO REFETCH {data()}
</div>

Then during this refetch in the routeData call to useContext returns undefined during the lifecycle of the refetch.

Expected behavior 🤔

That if you can use useContext in route data at all, it should work for any time the route data function is called: first render, refetch, refetch by key

Steps to reproduce 🕹

Steps:

  1. add a context provider around the component
  2. add route data that useContext this context in a component, and log the value returned from useContext
  3. add a click handler on a div in that component to refetchRouteData
  4. click the div and watch the log message for useContext go from having a value previously to undefined during refetch

Context 🔦

If we want to inject context we have to be able to trust it everywhere, otherwise your code breaks. I.e. if I inject a API class that holds the configured API end points and want to use it everywhere, I need to reliably appear.

Your environment 🌎

MAC OS X
using solidjs create on a sample App with default settings and just doing steps above.
@apatrida apatrida added the bug Something isn't working label Aug 8, 2023
@apatrida
Copy link
Author

apatrida commented Aug 8, 2023

Update, this is only if you construct routeData with context incorrectly. The bad model:

export function routeData() {
    return createRouteData(() => {
        const thing = useContext(...);
        ...
    });
}

and correct is to capture context outside of the returned function:

export function routeData() {
    const thing = useContext(...);

    return createRouteData(() => {
        ...
    });
}

@apatrida apatrida closed this as completed Aug 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant