I am trying to use Stotybook in a create-react-app with TypeScript without ejecting. I have understood that this should be possible, even though it is not described in the docs. Here are the resources I have used:
https://storybook.js.org/basics/introduction/
https://storybook.js.org/configurations/typescript-config/
https://github.com/wmonk/create-react-app-typescript/issues/405
I get storybook to build but it shows:
No Preview
Sorry, but you either have no stories or none are selected somehow.
This is what I have done:
npx -p @storybook/cli sb init
npm install --save-dev react-docgen-typescript-webpack-plugin @storybook/addon-info @types/storybook__react awesome-typescript-loader
npm run storybook
Here is code I have changed from what the init storybook command created:
tsconfig.js
...
"rootDirs": ["src", "stories"],
...
.storybook/config.js
import { configure } from '@storybook/react';
// automatically import all files ending in *.stories.js
const req = require.context('../', true, /.stories.tsx$/);
function loadStories() {
req.keys().forEach(filename => req(filename));
}
configure(loadStories, module);
.storybook/webpack.config.js
const path = require("path");
const TSDocgenPlugin = require("react-docgen-typescript-webpack-plugin"); // Optional
module.exports = (baseConfig, env, config) => {
config.module.rules.push({
test: /\.(ts|tsx)$/,
loader: require.resolve("ts-loader")
});
config.plugins.push(new TSDocgenPlugin()); // optional
config.resolve.extensions.push(".ts", ".tsx");
return config;
};
src/stories/index.js
import * as React from 'react';
import { storiesOf } from '@storybook/react';
import { action } from '@storybook/addon-actions';
import TicTacToeCell from './TicTacToeCell';
const stories = storiesOf('Components', module);
stories.add(
'TicTacToeCell',
() => <TicTacToeCell value="X" position={{ x: 0, y: 0 }} onClick={action('onClick')} />,
{ info: { inline: true } }
);
What is wrong with my setup?
PS. I don't know if I need all of the packages above, so please let me know if I am adding things to my project that won't be relevant because I am running a create-react-app project
src/stories/index.js
应命名为src/stories/index.tsx
just load your stories directly in .storybook/config.js
function loadStories() {
require('../src/stories')
}
or you can rename src/stories/index.js
into src/stories/index.stories.tsx
read the comments carefully
// automatically import all files ending in *.stories.js
const req = require.context('../', true, /.stories.tsx$/);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.