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

Use store revisions to ensure that stories re-render on HMR. #2588

Merged
merged 2 commits into from
Dec 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/react/src/client/preview/client_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export default class ClientApi {
if (m && m.hot) {
m.hot.dispose(() => {
this._storyStore.removeStoryKind(kind);
this._storyStore.incrementRevision();
});
}

Expand Down
11 changes: 10 additions & 1 deletion app/react/src/client/preview/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const isBrowser = typeof window !== 'undefined';
let rootEl = null;
let previousKind = '';
let previousStory = '';
let previousRevision = -1;

if (isBrowser) {
rootEl = document.getElementById('root');
Expand Down Expand Up @@ -45,6 +46,7 @@ export function renderMain(data, storyStore) {
const noPreview = <NoPreview />;
const { selectedKind, selectedStory } = data;

const revision = storyStore.getRevision();
const story = storyStore.getStory(selectedKind, selectedStory);
if (!story) {
ReactDOM.render(noPreview, rootEl);
Expand All @@ -55,14 +57,21 @@ export function renderMain(data, storyStore) {
// renderMain() gets executed after each action. Actions will cause the whole
// story to re-render without this check.
// https://github.com/storybooks/react-storybook/issues/116
if (selectedKind === previousKind && previousStory === selectedStory) {
// However, we do want the story to re-render if the store itself has changed
// (which happens at the moment when HMR occurs)
if (
revision === previousRevision &&
selectedKind === previousKind &&
previousStory === selectedStory
) {
return null;
}

// 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/storybooks/react-storybook/issues/81
previousRevision = revision;
previousKind = selectedKind;
previousStory = selectedStory;
ReactDOM.unmountComponentAtNode(rootEl);
Expand Down
12 changes: 12 additions & 0 deletions app/react/src/client/preview/story_store.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@ function getId() {
export default class StoryStore {
constructor() {
this._data = {};
// This number is incremented on every HMR.
// In theory it could also be incremented if stories were dynamically
// changed in the store
this._revision = 0;
}

getRevision() {
return this._revision;
}

incrementRevision() {
this._revision += 1;
}

addStory(kind, name, fn, fileName) {
Expand Down