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. #2605

Merged
merged 1 commit into from
Dec 30, 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
27 changes: 19 additions & 8 deletions app/react/src/client/preview/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const logger = console;
let rootEl = null;
let previousKind = '';
let previousStory = '';
let previousRevision = -1;

if (isBrowser) {
rootEl = document.getElementById('root');
Expand Down Expand Up @@ -46,6 +47,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 @@ -56,16 +58,25 @@ 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) {
// 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
previousKind = selectedKind;
previousStory = selectedStory;
ReactDOM.unmountComponentAtNode(rootEl);
// 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);

const context = {
kind: selectedKind,
story: selectedStory,
Expand Down
1 change: 1 addition & 0 deletions lib/core/src/client/preview/client_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export default class ClientApi {
if (m && m.hot) {
m.hot.dispose(() => {
this._storyStore.removeStoryKind(kind);
this._storyStore.incrementRevision();
});
}

Expand Down
14 changes: 13 additions & 1 deletion lib/core/src/client/preview/story_store.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ export default class StoryStore extends EventEmitter {
constructor() {
super();
this._data = {};
this._revision = 0;
}

getRevision() {
return this._revision;
}

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

addStory(kind, name, fn, fileName) {
Expand Down Expand Up @@ -88,7 +97,10 @@ export default class StoryStore extends EventEmitter {
}

dumpStoryBook() {
const data = this.getStoryKinds().map(kind => ({ kind, stories: this.getStories(kind) }));
const data = this.getStoryKinds().map(kind => ({
kind,
stories: this.getStories(kind),
}));

return data;
}
Expand Down