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

React: Add strictMode option #12781

Merged
merged 6 commits into from
Oct 16, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 15 additions & 8 deletions app/react/src/client/preview/render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@ import ReactDOM from 'react-dom';

import { RenderContext } from './types';

// will be provided by the webpack define plugin
declare const FRAMEWORK_OPTIONS: {
shilman marked this conversation as resolved.
Show resolved Hide resolved
fastRefresh?: boolean;
strictMode?: boolean;
};

const rootEl = document ? document.getElementById('root') : null;

const render = (node: ReactElement, el: Element) =>
new Promise((resolve) => {
ReactDOM.render(
process.env.STORYBOOK_EXAMPLE_APP ? <StrictMode>{node}</StrictMode> : node,
el,
resolve
);
ReactDOM.render(node, el, resolve);
});

class ErrorBoundary extends Component<{
Expand Down Expand Up @@ -47,6 +49,9 @@ class ErrorBoundary extends Component<{
}
}

const isStrict = process.env.STORYBOOK_EXAMPLE_APP || FRAMEWORK_OPTIONS?.strictMode;
const Wrapper = isStrict ? React.StrictMode : React.Fragment;

export default async function renderMain({
storyFn,
showMain,
Expand All @@ -57,9 +62,11 @@ export default async function renderMain({
const StoryFn = storyFn as FunctionComponent;

const element = (
<ErrorBoundary showMain={showMain} showException={showException}>
<StoryFn />
</ErrorBoundary>
<Wrapper>
<ErrorBoundary showMain={showMain} showException={showException}>
<StoryFn />
</ErrorBoundary>
</Wrapper>
);

// We need to unmount the existing set of components in the DOM node.
Expand Down
1 change: 1 addition & 0 deletions app/react/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ import { StorybookConfig as BaseConfig } from '@storybook/core/types';
export interface StorybookConfig extends BaseConfig {
reactOptions?: {
fastRefresh?: boolean;
strictMode?: boolean;
};
}
1 change: 0 additions & 1 deletion examples/official-storybook/.env
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
DOTENV_DISPLAY_WARNING=none
STORYBOOK_EXAMPLE_APP=true
1 change: 1 addition & 0 deletions examples/official-storybook/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module.exports = {
],
reactOptions: {
fastRefresh: true,
strictMode: true,
},
addons: [
{
Expand Down
2 changes: 2 additions & 0 deletions lib/core/src/server/preview/iframe-webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export default async ({
}) => {
const dlls = await presets.apply('webpackDlls', []);
const logLevel = await presets.apply('logLevel', undefined);
const frameworkOptions = await presets.apply(`${framework}Options`, {}, {});
const { raw, stringified } = loadEnv({ production: true });
const babelLoader = createBabelLoader(babelOptions, framework);
const isProd = configType === 'PRODUCTION';
Expand Down Expand Up @@ -157,6 +158,7 @@ export default async ({
new DefinePlugin({
'process.env': stringified,
NODE_ENV: JSON.stringify(process.env.NODE_ENV),
FRAMEWORK_OPTIONS: frameworkOptions,
}),
isProd ? null : new WatchMissingNodeModulesPlugin(nodeModulesPaths),
isProd ? null : new HotModuleReplacementPlugin(),
Expand Down