Skip to content

Commit

Permalink
Merge pull request #2463 from storybooks/(#1736)-Ability-to-force-red…
Browse files Browse the repository at this point in the history
…ender-a-story

[WIP] (#1736) ability to force re-render a story
  • Loading branch information
dangreenisrael authored Dec 28, 2017
2 parents 47dc12e + 82699d3 commit 4b5622a
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 12 deletions.
2 changes: 2 additions & 0 deletions app/angular/src/client/preview/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,5 @@ const renderUI = () => {
};

reduxStore.subscribe(renderUI);

export const forceReRender = () => render(context, true);
8 changes: 4 additions & 4 deletions app/angular/src/client/preview/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export function renderException(error) {
logger.error(error.stack);
}

export function renderMain(data, storyStore) {
export function renderMain(data, storyStore, forceRender) {
if (storyStore.size() === 0) return null;

const { selectedKind, selectedStory } = data;
Expand All @@ -39,7 +39,7 @@ export function renderMain(data, storyStore) {
// https://github.com/storybooks/react-storybook/issues/116

const reRender = selectedKind !== previousKind || previousStory !== selectedStory;
if (reRender) {
if (reRender || forceRender) {
// 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:
Expand All @@ -54,14 +54,14 @@ export function renderMain(data, storyStore) {
return renderNgApp(story, context, reRender);
}

export default function renderPreview({ reduxStore, storyStore }) {
export default function renderPreview({ reduxStore, storyStore }, forceRender = false) {
const state = reduxStore.getState();
if (state.error) {
return renderException(state.error);
}

try {
return renderMain(state, storyStore);
return renderMain(state, storyStore, forceRender);
} catch (ex) {
return renderException(ex);
}
Expand Down
9 changes: 8 additions & 1 deletion app/react/src/client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@ import deprecate from 'util-deprecate';
import { action as deprecatedAction } from '@storybook/addon-actions';
import { linkTo as deprecatedLinkTo } from '@storybook/addon-links';

export { storiesOf, setAddon, addDecorator, configure, getStorybook } from './preview';
export {
storiesOf,
setAddon,
addDecorator,
configure,
getStorybook,
forceReRender,
} from './preview';

export const action = deprecate(
deprecatedAction,
Expand Down
4 changes: 4 additions & 0 deletions app/react/src/client/preview/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ const isBrowser =
!(navigator.userAgent.indexOf('jsdom') > -1);

const storyStore = new StoryStore();
/* eslint-disable no-underscore-dangle */
const reduxStore = createStore(reducer);
/* eslint-enable */
const context = { storyStore, reduxStore };

if (isBrowser) {
Expand Down Expand Up @@ -54,3 +56,5 @@ const renderUI = () => {
};

reduxStore.subscribe(renderUI);

export const forceReRender = () => render(context, true);
7 changes: 4 additions & 3 deletions app/react/src/client/preview/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export function renderException(error) {
logger.error(error.stack);
}

export function renderMain(data, storyStore) {
export function renderMain(data, storyStore, forceRender) {
if (storyStore.size() === 0) return null;

const NoPreview = () => <p>No Preview Available!</p>;
Expand All @@ -60,6 +60,7 @@ export function renderMain(data, storyStore) {
// However, we do want the story to re-render if the store itself has changed
// (which happens at the moment when HMR occurs)
if (
!forceRender &&
revision === previousRevision &&
selectedKind === previousKind &&
previousStory === selectedStory
Expand Down Expand Up @@ -109,14 +110,14 @@ export function renderMain(data, storyStore) {
return null;
}

export default function renderPreview({ reduxStore, storyStore }) {
export default function renderPreview({ reduxStore, storyStore }, forceRender = false) {
const state = reduxStore.getState();
if (state.error) {
return renderException(state.error);
}

try {
return renderMain(state, storyStore);
return renderMain(state, storyStore, forceRender);
} catch (ex) {
return renderException(ex);
}
Expand Down
2 changes: 2 additions & 0 deletions app/vue/src/client/preview/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,5 @@ const renderUI = () => {
};

reduxStore.subscribe(renderUI);

export const forceReRender = () => render(context, true);
8 changes: 4 additions & 4 deletions app/vue/src/client/preview/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ function renderRoot(options) {
app = new Vue(options);
}

export function renderMain(data, storyStore) {
export function renderMain(data, storyStore, forceRender) {
if (storyStore.size() === 0) return;

const { selectedKind, selectedStory } = data;
Expand All @@ -60,7 +60,7 @@ 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) {
if (forceRender || 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:
Expand Down Expand Up @@ -97,14 +97,14 @@ export function renderMain(data, storyStore) {
});
}

export default function renderPreview({ reduxStore, storyStore }) {
export default function renderPreview({ reduxStore, storyStore }, forceRender = false) {
const state = reduxStore.getState();
if (state.error) {
return renderException(state.error);
}

try {
return renderMain(state, storyStore);
return renderMain(state, storyStore, forceRender);
} catch (ex) {
return renderException(ex);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Storyshots Force ReRender button 1`] = `
<button>
Click me to increment: 0
</button>
`;
12 changes: 12 additions & 0 deletions examples/cra-kitchen-sink/src/stories/force-rerender.stories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import React from 'react';
import { storiesOf, forceReRender } from '@storybook/react';

let count = 0;
const increment = () => {
count += 1;
forceReRender();
};

storiesOf('Force ReRender', module).add('button', () => (
<button onClick={increment}> Click me to increment: {count} </button>
));

0 comments on commit 4b5622a

Please sign in to comment.