0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

[Storybook] インタラクションテストでhooksを使ったらreact-hooks/rules-of-hooksのエラーが出た

Posted at

環境

  • Storybook 7.5.3
  • React 18.2.0
  • eslint 8.53.0

エラー内容

Storybookでplay関数を使用してインタラクションテストを書く際に、render内でuseStateを使用するコードを書いたら、ESLintのreact-hooks/rules-of-hooksに関するエラーが出た。

React Hook "React.useState" is called in function "render" that is neither a React function component nor a custom React Hook function. 
React component names must start with an uppercase letter. React Hook names must start with the word "use".eslint

発生したときのStorybookのコードは以下の通り。


export const ClickTest: Story = {
    render: () => {
        // ESLintのエラー
        const [open, setOpen] = React.useState(false)
        ...
    },
    play: async ({ canvasElement }) => {
    ....
    },
};

解決方法

以下のGitHubのissueを参考に、アロー関数からfunctionに書き換えるとESLintのエラーが解消。

export const ClickTest: Story = {
    render: function Render() {
        const [open, setOpen] = React.useState(false)
        ...
    },
    play: async ({ canvasElement }) => {
    ....
    },
};

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?