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

5

我正在尝试在不弹出的情况下,将Storybook与TypeScript结合使用create-react-app。我已经了解到这是可能的,尽管文档中没有描述。以下是我使用的资源:

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

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

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

我成功让storybook构建,但它显示如下:

No Preview
Sorry, but you either have no stories or none are selected somehow.

这是我所做的:

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

以下是我根据初始化storybook命令创建的代码所作出的更改:

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

我的设置有什么问题?

PS.我不知道是否需要上面列出的所有软件包,请告诉我如果我添加了对我的项目没有用处的东西,因为我正在运行create-react-app项目。

2个回答

6

src/stories/index.js 应该改名为 src/stories/index.tsx


0

只需直接在.storybook/config.js中加载您的故事即可。

function loadStories() {
  require('../src/stories')
}

编辑:

或者你可以将 src/stories/index.js 重命名为 src/stories/index.stories.tsx

仔细阅读注释

// automatically import all files ending in *.stories.js
const req = require.context('../', true, /.stories.tsx$/);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接