-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
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
Context provided through addDecorator is only available to child components of a story when using useContext
.
#9923
Comments
Thanks @threehams! This is a react hooks issue, and I'm not sure what's the right fix yet. As you've noted, using react context directly works: <ExampleContext.Consumer>
{value => (
<div>
Value in example: {value}
<ChildComponent />
</div>
)}
</ExampleContext.Consumer> Also, treating the story as a react component in your decorator makes the hooks work: const StoryDecorator = StoryFn => {
return (
<ExampleContext.Provider value="From Decorator">
<StoryFn />
</ExampleContext.Provider>
);
}; However, I'm hesitant to recommend this in general since it's react specific (and decorators are not a react-specific concept). cc @tmeasday |
That suddenly makes a ton of sense. Same reason we've been gradually getting rid of functions which return JSX, but are called like functions. |
I guess decorators are quite framework specific (like what does an angular decorator even look like? Certainly it doesn't have JSX in it right?). I think we probably should be showing a decorator for a specific framework in our docs; and for a react one we should render the story as a JSX component. |
Am also hitting this issue. Our library has a few providers and a number of
Using the long form isn't a workaround in this case because |
@tmeasday that makes sense. i think we need a guide for each framework as part of the post-6.0 documentation overhaul, and decorators can be a section in that. |
Hi everyone! Seems like there hasn't been much going on in this issue lately. If there are still questions, comments, or bugs, please feel free to continue the discussion. Unfortunately, we don't have time to get to every issue. We are always open to contributions so please send us a pull request if you would like to help. Inactive issues will be closed after 30 days. Thanks! |
I ran in the same issue today. This basic example logs undefined when ran import React from 'react';
const AContext = React.createContext();
function ADecorator(storyFn) {
return (
<AContext.Provider value="hello">
{storyFn()}
</AContext.Provider>
);
}
export function Component() {
const context = React.useContext(AContext);
console.log('context inside Component', { context })
return (
<div>This is my normal page </div>
);
}
Component.story = {
name: 'Component',
decorators: [ADecorator],
}; If |
Hi everyone! Seems like there hasn't been much going on in this issue lately. If there are still questions, comments, or bugs, please feel free to continue the discussion. Unfortunately, we don't have time to get to every issue. We are always open to contributions so please send us a pull request if you would like to help. Inactive issues will be closed after 30 days. Thanks! |
Hey there, it's me again! I am going close this issue to help our maintainers focus on the current development roadmap instead. If the issue mentioned is still a concern, please open a new ticket and mention this old one. Cheers and thanks for using Storybook! |
@jugglingcats - did you ever find a way to fix this? I seem to be having the same issue - want to provide example docs. Would like to showcase how to use |
Sorry @coreybruyere I can't quite remember. We ended up using Docusaurus because we felt it was better suited to end-user documentation. |
As mention in my post earlier for us it worked fine if we created decorator like function ADecorator(storyFn) {
return (
<AContext.Provider value="hello">
<StoryFn />
</AContext.Provider>
);
} |
How do you actually pass on function ADecorator(storyFn, context) {
return (
<AContext.Provider value="hello">
<StoryFn /> {/* works, but has no "upper" context */}
<StoryFn {...context} /> {/* still no "upper" context (also, does not make much sense to spread as props) */}
{StoryFn(context)} {/* Not possible as context won't be available directly in the story (only children) */}
</AContext.Provider>
);
}
I found these possible solutions:
export const MyStory = () => <MyStoryComponent />
const MyStoryComponent = () => {
const [_, updateArgs] = useArgs()
// ...
}
|
Describe the bug
When rendering a story within an MDX
Preview
block, context provided fromaddDecorator
(usinguseContext
only) is applied to child components in the story, but not to the story itself.To Reproduce
Steps to reproduce the behavior:
.stories.mdx
file, then.stories.js
file, and export a story which usesuseContext
.useContext
addDecorator
inpreview.js
, add a provider to all stories.Expected behavior
The provider value is available in
useContext
within the story.Actual behavior
The provider value is the default within the story, but the child component receives the provided value from the decorator. React DevTools show the hierarchy as story, then decorator provider, then child component.
This only happens with the
useContext
hook, not withContext.Consumer
.Code snippets
Minimal repo:
https://github.com/threehams/storybook-decorator-order
System:
The text was updated successfully, but these errors were encountered: