使用TypeScript在create-react-app中的故事书

use*_*776 4 typescript reactjs storybook

我试图在带有TypeScript的create-react-app中使用Stotybook而不会弹出。我已经知道,即使文档中未对此进行说明,这也是可能的。这是我使用的资源:

https://storybook.js.org/basics/introduction/

https://storybook.js.org/configurations/typescript-config/

https://github.com/wmonk/create-react-app-typescript/issues/405

我可以建立故事书,但它显示:

No Preview
Sorry, but you either have no stories or none are selected somehow.
Run Code Online (Sandbox Code Playgroud)

这是我所做的:

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
Run Code Online (Sandbox Code Playgroud)

这是我从init storybook命令创建的内容中更改的代码:

tsconfig.js

...
"rootDirs": ["src", "stories"],
...
Run Code Online (Sandbox Code Playgroud)

.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);
Run Code Online (Sandbox Code Playgroud)

.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;
};
Run Code Online (Sandbox Code Playgroud)

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 } }
);
Run Code Online (Sandbox Code Playgroud)

我的设置有什么问题?

PS。我不知道我是否需要上述所有软件包,因此请告知我是否正在向我的项目中添加与我无关的东西,因为我正在运行create-react-app项目

use*_*776 5

src/stories/index.js 应该命名 src/stories/index.tsx