Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storybook in create-react-app with TypeScript

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

like image 759
user1283776 Avatar asked Nov 06 '18 11:11

user1283776


1 Answers

src/stories/index.js should be named src/stories/index.tsx

like image 62
user1283776 Avatar answered Oct 15 '22 01:10

user1283776