diff --git a/src/client/ui/preview.js b/src/client/ui/preview.js index b7d2e97943f8..d33039928d7b 100644 --- a/src/client/ui/preview.js +++ b/src/client/ui/preview.js @@ -6,6 +6,8 @@ import { } from '../'; const rootEl = document.getElementById('root'); +let previousKind = ''; +let previousStory = ''; export function renderError(data, error) { // We always need to render redbox in the mainPage if we get an error. @@ -26,11 +28,19 @@ export function renderMain(data) { return ReactDOM.render(noPreview, rootEl); } - // We need to unmount the existing set of components in the DOM node. - // Otherwise, React may not recrease instances for every story run. - // This could leads to issues like below: - // https://github.com/kadirahq/react-storybook/issues/81 - ReactDOM.unmountComponentAtNode(rootEl); + // Unmount the previous story only if selectedKind or selectedStory has changed. + // renderMain() gets executed after each action. Actions will cause the whole + // story to re-render without this check. + // https://github.com/kadirahq/react-storybook/issues/116 + if (selectedKind !== previousKind || previousStory !== selectedStory) { + // We need to unmount the existing set of components in the DOM node. + // Otherwise, React may not recrease instances for every story run. + // This could leads to issues like below: + // https://github.com/kadirahq/react-storybook/issues/81 + previousKind = selectedKind; + previousStory = selectedStory; + ReactDOM.unmountComponentAtNode(rootEl); + } return ReactDOM.render(story(), rootEl); }