-
Notifications
You must be signed in to change notification settings - Fork 2
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
storybook-react-context packs value incorrectly #25
Comments
Hi @nirvdrum. Thanks for your feedback. I'm having trouble understanding the use case and the specific issue with 3rd party context. Would it be possible to provide an example to help me understand it? A PR would certainly be welcome as it could contain an example SB story that shows it in action. |
Hi @tyom. Thanks for taking a look. This is my first Storybook project, so apologies in advance if I've got something wrong. To make the example concrete, I'm using @auth0/auth0-react to handle authentication for my app. This library ships with an const { isAuthenticated, isLoading, loginWithRedirect } = useAuth0(); The definition of const useAuth0 = <TUser extends User = User>(): Auth0ContextInterface<TUser> =>
useContext(Auth0Context) as Auth0ContextInterface<TUser>; As you can see, My attempt at using the add-on looked like: export const Default = Template.bind({});
Default.decorators = [withReactContext({
context: Auth0Context,
initialState: {
isAuthenticated: true,
isLoading: false,
getAccessTokenSilently: () => new Promise((resolve) => resolve('My JWT')),
user: {
sub: "auth0|some_auth0_id",
name: "John Doe"
},
}
})]; So, I set the const { isAuthenticated, isLoading, loginWithRedirect } = useAuth0()[0]; While I could wrap the To solve the problem in the meanwhile, I took the spirit of this add-on and wrote my own decorator, added to preview.tsx: export const decorators = [
(Component: React.ComponentType<unknown>) => {
const initialValue = useAuth0();
return (
<Auth0Context.Provider
value={{
...initialValue,
isAuthenticated: true,
isLoading: false,
getAccessTokenSilently: () => new Promise((resolve) => resolve('My JWT')),
user: {
sub: 'auth0|some_auth0_id',
name: 'John Doe'
}
}}
>
<Component />
</Auth0Context.Provider>
);
}
]; Please let me know if you need any additional information. |
Hi @nirvdrum. Apologies for the radio silence. I've just done some housekeeping in the library and also looked into the use case you describe here. There is now a new option in the Try to upgrade to version 0.5.0 to get this functionality. Let me know if that helps or if I misinterpreted your use case. |
This only helps with the initial value. However, if context is more than just a single object of primitive values, it doesn't help. For example, many providers export a useReducer: false will allow the |
Could you help setting up a test case for this, @LongLiveCHIEF? |
Simple example: const ColorContext = React.createContext()
interface ColorThemeContext {
background: Color;
border: Color;
}
const ColorProvider({children}){
const [context, setContext] = useState<ColorThemeContext>()
return <ColorContext.Provider value={{context, setContext}}>{children}</ColorContext.Provider>
} The idea would be that inside the story we'd want to use a export const SomeStory = () => {
const { context, setContext } = useContext(ColorContext)
//.... some story jsx
} The problem is that currently when i decorate with this addon, the granted, that may be outside the use case of this addon. |
Maybe we could add a |
The use case for this addon when I initially created was to tap into the component's context in Storybook and change it at will. Now I realise that the use case is not even properly presented in the examples I have. I need to review those use cases and present them better while looking into making sure that the provider value is not assumed to be a tuple from |
I've refactored the Please note that this is a breaking change. The Let me know if it solves your issue. |
Hi @nirvdrum. It's been a long time, and I neglected this package. I have just rewritten it and am planning to release a new version. If you want to learn more, please see this comment for details. Cheers. |
The storybook-react-context addon repacks the
initialState
value into a new type that doesn't necessarily match the original context provider value. The addon unconditionally changes the provider value to aReact.useReducer
result. It looks like the implementation may overfit to a particular use case. I'm working with a provider where the value is just a plain object. What I pass as theinitialState
value should be what the component gets with aReact.useContext
call. In this particular case, it's a 3rd party context so I don't have the liberty of adjusting it to conform to this addon's expectations.I'm happy to pull together a PR, but I'd like to get agreement on what, if anything, should change.
The text was updated successfully, but these errors were encountered: