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

[SIGNIFICANT] Remove redundant render in react #2503

Merged
merged 7 commits into from
Dec 21, 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
18 changes: 10 additions & 8 deletions app/react/src/client/preview/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,18 @@ 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);
if (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
previousKind = selectedKind;
previousStory = selectedStory;
ReactDOM.unmountComponentAtNode(rootEl);

const context = {
kind: selectedKind,
story: selectedStory,
Expand Down
1 change: 1 addition & 0 deletions examples/cra-kitchen-sink/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"@storybook/addon-storyshots": "^3.3.0-alpha.4",
"@storybook/addon-viewport": "^3.3.0-alpha.4",
"@storybook/addons": "^3.3.0-alpha.4",
"@storybook/client-logger": "^3.3.0-alpha.4",
"@storybook/components": "^3.3.0-alpha.4",
"@storybook/react": "^3.3.0-alpha.4",
"babel-jest": "^21.2.0",
Expand Down
40 changes: 40 additions & 0 deletions examples/cra-kitchen-sink/src/components/LifecycleLogger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import React, { Component } from 'react';
import { logger } from '@storybook/client-logger';

function log(name) {
logger.info(`LifecycleLogger: ${name}`);
}

// A component that logs its lifecycle so we can check that things happen
// the right number of times (i.e. we are using React properly)
export default class LifecycleLogger extends Component {
constructor() {
super();
log('contructor');
}
componentWillMount() {
log('componentWillMount');
}
componentDidMount() {
log('componentDidMount');
}
componentWillReceiveProps() {
log('componentWillReceiveProps');
}
componentWillUpdate() {
log('componentWillUpdate');
}
componentDidUpdate() {
log('componentDidUpdate');
}
componentWillUnmount() {
log('componentWillUnmount');
}
componentDidCatch() {
log('componentDidCatch');
}
render() {
log('render');
return <div>Lifecycle methods are logged to the console</div>;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1030,6 +1030,12 @@ exports[`Storyshots Button with text 1`] = `
</button>
`;

exports[`Storyshots Lifecycle logging 1`] = `
<div>
Lifecycle methods are logged to the console
</div>
`;

exports[`Storyshots Some really long story kind description with text 1`] = `
<div
style="position:fixed;top:0;left:0;bottom:0;right:0;display:flex;align-items:center;justify-content:center;overflow:auto"
Expand Down
4 changes: 4 additions & 0 deletions examples/cra-kitchen-sink/src/stories/index.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { Button } from '@storybook/react/demo';

import App from '../App';
import Container from './Container';
import LifecycleLogger from '../components/LifecycleLogger';

const InfoButton = () => (
<span
Expand Down Expand Up @@ -88,3 +89,6 @@ storiesOf('App', module).add('full app', () => <App />);
storiesOf('Some really long story kind description', module)
.addDecorator(centered)
.add('with text', () => <Button onClick={action('clicked')}>Hello Button</Button>);

storiesOf('Lifecycle', module).add('logging', () => <LifecycleLogger />);