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

[WIP] (#1736) ability to force re-render a story #2463

Merged
merged 15 commits into from
Dec 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
98fa5cd
(#1736) Adds Ability to re-render a story programatically in react
dangreenisrael Dec 11, 2017
6cb8d21
Merge branch 'release/3.3' into (#1736)-Ability-to-force-redender-a-s…
dangreenisrael Dec 11, 2017
45f3a7f
(#1736) Add cra example for force re-render
dangreenisrael Dec 11, 2017
50774cb
Merge branch 'release/3.3' into (#1736)-Ability-to-force-redender-a-s…
ndelangen Dec 14, 2017
6d80f69
Merge branch 'release/3.3' into (#1736)-Ability-to-force-redender-a-s…
ndelangen Dec 21, 2017
d4609e4
(#1736) Update forceReRender to not rely on a bug
dangreenisrael Dec 27, 2017
ce935e6
Merge branch 'master' into (#1736)-Ability-to-force-redender-a-story
ndelangen Dec 27, 2017
597387c
(#1736) Add forceReRender support for Angular and Vue
dangreenisrael Dec 27, 2017
1fb6e3a
Merge branch 'master' of https://github.com/storybooks/storybook into…
dangreenisrael Dec 27, 2017
31a68d7
(#1736) Update Force ReRender storyshot
dangreenisrael Dec 27, 2017
8e1caac
Merge branch 'master' into (#1736)-Ability-to-force-redender-a-story
dangreenisrael Dec 27, 2017
293bb7a
Merge branch 'master' of https://github.com/storybooks/storybook into…
dangreenisrael Dec 28, 2017
2be6ff2
Merge branch 'master' of https://github.com/storybooks/storybook into…
dangreenisrael Dec 28, 2017
a27b4bd
Merge branch 'master' into (#1736)-Ability-to-force-redender-a-story
dangreenisrael Dec 28, 2017
82699d3
Merge branch 'master' into (#1736)-Ability-to-force-redender-a-story
dangreenisrael Dec 28, 2017
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
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>
));