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

Cannot change resourceContext between stories #76

Open
theKashey opened this issue Mar 29, 2021 · 5 comments
Open

Cannot change resourceContext between stories #76

theKashey opened this issue Mar 29, 2021 · 5 comments
Labels
bug Something isn't working

Comments

@theKashey
Copy link
Collaborator

Discovered in Storybook environment, when was not able to reset resourceContext between different stories.

Expectation: mounting/unmounting Router and especially MemoryRouter are not by previously used components.

@albertogasparin
Copy link
Collaborator

Yeah, currently only UniversalRouter sets up a ResourceContainer that can be set to local so works with storybooks and tests. Should be addressed by the cleanup of the multiple routers we export, where Router can just handle this stuff properly

@theKashey
Copy link
Collaborator Author

In my case, I was able to resolve the current situation by mocking useResource and calling .getData on the given object directly, which sound like a correct solution for storybooks, where "page" and the corresponding "route" does not exist and resource in normal terms should not be fetched.

@theKashey
Copy link
Collaborator Author

theKashey commented Jul 12, 2022

Still waiting for #145 as UniversalRouter is not exported.

@tpuric
Copy link
Collaborator

tpuric commented Aug 9, 2023

Hi @theKashey , I managed to create a workaround for this without needing to override any imports.

You can create a decorator utilising key (for router uniqueness to ensure unmounts are handled appropriately) and forcefully destroy any existing stores when mounting another Router.

export const withRouterDecoratorFn = ({ resources }: RouterDecoratorProps = {}): DecoratorFn =>
  function RouterDecorator(Story) {
    defaultRegistry.stores.clear(); // destroys existing react-sweet-state stores

    return (
      <Router
        key={key} // key for uniqueness
        routes={routes}
        history={history}
        resourceContext={routerContext} // context will be fresh for every mount (instantiating a new store)
      >
        <Story />
      </Router>
    );
  };

Codesandbox demo: https://codesandbox.io/s/react-resource-router-basic-routing-example-forked-qt67q6

@theKashey
Copy link
Collaborator Author

Please remove defaultRegistry.stores.clear(); there is a better place to put such destructive side effect into

As for the Router - this is the current recommendation.

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

3 participants