reactjs - Hot reloading u Storybooku uzrokuje grešku kod hookova
Dobivam sljedeću pogrešku kada se moj Storybook ponovno učitava uživo. Na prvom učitavanju radi savršeno:
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
Koristim redux provider, iako nisam siguran je li to povezano. Kod i priče izgledaju ovako bez obzira:
// 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 (unutra u .storybook/ - normalna aplikacija koristi zadani 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;
};
Odgovor #1
Ovo mi se dogodilo kada sam uređivao svoju .storybook/config.js datoteku i nisam ponovno pokrenuo Storybook server. Kada sam ga zaustavio i ponovno pokrenuo, ova je pogreška nestala.
Odgovor #2
Ispostavilo se da je pogreška nastala zbog toga što sam koristio dva različita formata pisanja priča, dok sam trebao koristiti jedan ili drugi.
Da citiram korisnika Hypnosphi,
@wgpsutherland Izgleda da miješate Component Story Format i storiesOf API u svojim pričama:
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>)Moraš odabrati jedan. Dakle, trebao bi biti ili
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>ili
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>)
Pogledajte cijeli problem na githubu ovdje: https://github.com/storybookjs/storybook/issues/8306
