Ricerca nel sito web

reactjs - Il hot reloading di Storybook provoca errori nei hook


Ricevo il seguente errore quando il mio storybook si aggiorna a caldo. Funziona perfettamente al primo caricamento:

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

Sto usando un provider Redux, anche se non sono sicuro se sia correlato. Il codice e le storie sembrano comunque così:

// 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 (all'interno di .storybook/ - l'app normale utilizza il webpack predefinito di create-react-app):

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

Risposta n. 1

Mi è successo questo quando ho modificato il mio file .storybook/config.js e non ho riavviato il server di Storybook. Una volta fermato e riavviato, questo errore è scomparso.

Risposta n. 2

Si è scoperto che l'errore derivava dal fatto che stavo usando due formati diversi per scrivere storie, quando invece avrei dovuto usarne uno o l'altro.

Per citare l'utente Hypnosphi,

@wgpsutherland Sembra che tu stia mescolando il Component Story Format e l'API storiesOf nelle tue storie:

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

Devi sceglierne uno. Quindi dovrebbe essere o

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>

o

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

Vedi il problema completo su GitHub qui: https://github.com/storybookjs/storybook/issues/8306