DEV Community

Junichi Takahashi
Junichi Takahashi

Posted on • Updated on

How to set up Next.js + Storybook

Storybook is a UI development, testing, and documentation tool.
This article describes how to set up Next.js + Storybook.

Next.js

How to set up TypeScript + Next.js
https://dev.to/jtakahashi64/how-to-set-up-nextjs-with-typescript-20in

Install

npx storybook init
Enter fullscreen mode Exit fullscreen mode
npm i --save-dev tsconfig-paths-webpack-plugin
Enter fullscreen mode Exit fullscreen mode

Settings

// .storybook/main.js

const path = require("path");
const TsconfigPathsPlugin = require("tsconfig-paths-webpack-plugin");

// ...

module.exports = {
  // ...
  typescript: { reactDocgen: false },
  webpackFinal(config) {
    config.resolve.modules = [
      ...(config.resolve.modules || []),
      path.resolve(__dirname, "../src"),
    ];

    config.resolve.plugins = [
      ...(config.resolve.plugins || []),
      new TsconfigPathsPlugin(),
    ];
    return config;
  },
}
Enter fullscreen mode Exit fullscreen mode

Write

// src/components/example.tsx

interface Props {
  text: string;
}

const Example = (props: Props) => {
  return <p data-testid="text">{props.text}</p>;
};

export default Example;
Enter fullscreen mode Exit fullscreen mode
// src/pages/example.tsx

import type { NextPage } from "next";

import Example from "@/components/example";

const Home: NextPage = () => {
  return <Example text={"Hello, World."} />;
};

export default Home;
Enter fullscreen mode Exit fullscreen mode
// src/components/example.stories.tsx

import Example from "@/components/example";

export default { component: Example };

export const Default = {
  args: {
    text: "Hello, World! from Storybook.",
  },
};
Enter fullscreen mode Exit fullscreen mode

Run

npm run storybook
Enter fullscreen mode Exit fullscreen mode

http://localhost:6006/

ESLint

# .eslintrs.json

{
  // ...

  "overrides": [
    {
      "files": ["src/**/*.stories.tsx"],
      "rules": {
        "import/no-anonymous-default-export": "off"
      }
    }
  ],

  // ...
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)