故事书热重装导致挂钩错误

时间:2019-10-05 22:55:33

标签: reactjs typescript react-redux storybook

重新加载故事书时出现以下错误。它在第一次加载时效果很好:

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;
};

2 个答案:

答案 0 :(得分:0)

当我编辑.storybook / config.js文件并且没有重新启动故事书服务器时,发生了这种情况。一旦我停止并重新启动它,此错误就消失了。

答案 1 :(得分:0)

事实证明,错误是由我使用两种不同的写作故事格式引起的,而我本该一直使用。

要引用用户Hypnosphi,

  

@wgpsutherland好像您在混合Component Story Format和   您故事中的storyOfAPI:

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>)
     

您必须选择一个。所以应该是

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>
     

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