diff --git a/code/renderers/react/template/stories/teardown.stories.tsx b/code/renderers/react/template/stories/teardown.stories.tsx new file mode 100644 index 000000000000..0d5840509619 --- /dev/null +++ b/code/renderers/react/template/stories/teardown.stories.tsx @@ -0,0 +1,23 @@ +import { useEffect } from 'react'; + +const LoggingComponent = () => { + useEffect(() => { + console.log('mounted'); + return () => { + console.log('unmounted'); + }; + }, []); + + return 'Component'; +}; + +export default { + component: LoggingComponent, + tags: ['autodocs'], + parameters: { + storyshots: { disable: true }, + chromatic: { disable: true }, + }, +}; + +export const Default = {}; diff --git a/code/ui/blocks/src/components/Story.tsx b/code/ui/blocks/src/components/Story.tsx index ee8d56a4e5e1..05f14d352d52 100644 --- a/code/ui/blocks/src/components/Story.tsx +++ b/code/ui/blocks/src/components/Story.tsx @@ -58,7 +58,9 @@ const InlineStory: FunctionComponent = (props) => { ); setShowLoader(false); return () => { - cleanup(); + // It seems like you are supposed to unmount components outside of `useEffect`: + // https://github.com/facebook/react/issues/25675#issuecomment-1363957941 + setTimeout(() => cleanup(), 0); }; }, [autoplay, renderStoryToElement, story]);