Storybook 热重载导致 hooks 错误
Storybook hot reloading causes hooks error
我的故事书热重载时出现以下错误。它在第一次加载时完美运行:
TypeError: Cannot read property 'hooks' of undefined
at StoryStore.cleanHooks (http://localhost:9009/vendors~main.0e777466815571a0333e.bundle.js:6197:22)
at renderMain (http://localhost:9009/vendors~main.0e777466815571a0333e.bundle.js:8181:22)
at StoryStore.renderUI (http://localhost:9009/vendors~main.0e777466815571a0333e.bundle.js:8296:9)
at StoryStore.emit (http://localhost:9009/vendors~main.0e777466815571a0333e.bundle.js:6474:35)
at http://localhost:9009/vendors~main.0e777466815571a0333e.bundle.js:5943:16
我正在使用 redux 提供程序,虽然我不确定它是否相关。它的代码和故事无论如何都是这样的:
// provider.tsx
const Provider: React.FC = ({ children }) => (
<ThemeProvider theme={theme}>
<Provider store={store}>
<Router>
{children}
</Router>
</Provider>
</ThemeProvider>
)
export default Provider
// decorators.tsx
export const withProvider = (storyFn: any) => <Provider>
{storyFn()}
</Provider>
// 2-TodoCard.stories.tsx
const complete = () => <TodoCard todo={completeTodo} /> // the TodoCard is just a React.FC
const incomplete = () => <TodoCard todo={incompleteTodo} />
storiesOf('TodoCard', module)
.addDecorator(withProvider)
.add('complete', complete)
.add('incomplete', incomplete)
webpack.config.js(在 .storybook/ 中 - 普通应用程序使用默认的 create-react-app webpack):
module.exports = ({ config }) => {
config.module.rules.push({
test: /\.(ts|tsx)$/,
use: [
{
loader: require.resolve('awesome-typescript-loader'),
},
// Optional
{
loader: require.resolve('react-docgen-typescript-loader'),
},
],
});
config.resolve.extensions.push('.ts', '.tsx');
// config.entry = config.entry.filter(singleEntry => !singleEntry.includes('/webpack-hot-middleware/')); // hot reloading causing a hooks bug
return config;
};
我在编辑我的 .storybook/config.js 文件并且没有重新启动故事书服务器时发生了这种情况。一旦我停止并重新启动它,这个错误就消失了。
事实证明,错误源于我使用了两种不同的故事写作格式,而我本应使用其中一种格式。
引用用户Hypnosphi,
@wgpsutherland Looks like you're mixing Component Story Format and
storiesOf API in your stories:
export default {
title: 'Button',
};
storiesOf('Button', module)
.add('gray', () => <Button color='gray' fluid size='large' type='submit'>Add</Button>)
.add('blue', () => <Button color='blue' fluid size='large' type='submit'>Add</Button>)
You have to pick one. So it should be either
export default {
title: 'Button',
};
export const gray = () => <Button color='gray' fluid size='large' type='submit'>Add</Button>
export const blue = () => <Button color='blue' fluid size='large' type='submit'>Add</Button>
or
storiesOf('Button', module)
.add('gray', () => <Button color='gray' fluid size='large' type='submit'>Add</Button>)
.add('blue', () => <Button color='blue' fluid size='large' type='submit'>Add</Button>)
请在此处查看 github 上的完整问题:https://github.com/storybookjs/storybook/issues/8306
我的故事书热重载时出现以下错误。它在第一次加载时完美运行:
TypeError: Cannot read property 'hooks' of undefined
at StoryStore.cleanHooks (http://localhost:9009/vendors~main.0e777466815571a0333e.bundle.js:6197:22)
at renderMain (http://localhost:9009/vendors~main.0e777466815571a0333e.bundle.js:8181:22)
at StoryStore.renderUI (http://localhost:9009/vendors~main.0e777466815571a0333e.bundle.js:8296:9)
at StoryStore.emit (http://localhost:9009/vendors~main.0e777466815571a0333e.bundle.js:6474:35)
at http://localhost:9009/vendors~main.0e777466815571a0333e.bundle.js:5943:16
我正在使用 redux 提供程序,虽然我不确定它是否相关。它的代码和故事无论如何都是这样的:
// provider.tsx
const Provider: React.FC = ({ children }) => (
<ThemeProvider theme={theme}>
<Provider store={store}>
<Router>
{children}
</Router>
</Provider>
</ThemeProvider>
)
export default Provider
// decorators.tsx
export const withProvider = (storyFn: any) => <Provider>
{storyFn()}
</Provider>
// 2-TodoCard.stories.tsx
const complete = () => <TodoCard todo={completeTodo} /> // the TodoCard is just a React.FC
const incomplete = () => <TodoCard todo={incompleteTodo} />
storiesOf('TodoCard', module)
.addDecorator(withProvider)
.add('complete', complete)
.add('incomplete', incomplete)
webpack.config.js(在 .storybook/ 中 - 普通应用程序使用默认的 create-react-app webpack):
module.exports = ({ config }) => {
config.module.rules.push({
test: /\.(ts|tsx)$/,
use: [
{
loader: require.resolve('awesome-typescript-loader'),
},
// Optional
{
loader: require.resolve('react-docgen-typescript-loader'),
},
],
});
config.resolve.extensions.push('.ts', '.tsx');
// config.entry = config.entry.filter(singleEntry => !singleEntry.includes('/webpack-hot-middleware/')); // hot reloading causing a hooks bug
return config;
};
我在编辑我的 .storybook/config.js 文件并且没有重新启动故事书服务器时发生了这种情况。一旦我停止并重新启动它,这个错误就消失了。
事实证明,错误源于我使用了两种不同的故事写作格式,而我本应使用其中一种格式。
引用用户Hypnosphi,
@wgpsutherland Looks like you're mixing Component Story Format and storiesOf API in your stories:
export default { title: 'Button', }; storiesOf('Button', module) .add('gray', () => <Button color='gray' fluid size='large' type='submit'>Add</Button>) .add('blue', () => <Button color='blue' fluid size='large' type='submit'>Add</Button>)
You have to pick one. So it should be either
export default { title: 'Button', }; export const gray = () => <Button color='gray' fluid size='large' type='submit'>Add</Button> export const blue = () => <Button color='blue' fluid size='large' type='submit'>Add</Button>
or
storiesOf('Button', module) .add('gray', () => <Button color='gray' fluid size='large' type='submit'>Add</Button>) .add('blue', () => <Button color='blue' fluid size='large' type='submit'>Add</Button>)
请在此处查看 github 上的完整问题:https://github.com/storybookjs/storybook/issues/8306