Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Codemod: Handle react hooks better for CSF3 #21115

Closed
Tracked by #25432
shilman opened this issue Feb 16, 2023 · 6 comments · Fixed by storybookjs/eslint-plugin-storybook#149
Closed
Tracked by #25432

Codemod: Handle react hooks better for CSF3 #21115

shilman opened this issue Feb 16, 2023 · 6 comments · Fixed by storybookjs/eslint-plugin-storybook#149

Comments

@shilman
Copy link
Member

shilman commented Feb 16, 2023

Is your feature request related to a problem? Please describe

Currently, the CSF 2-to-3 codemod would turn the following story:

export const Counter = (args) => {
  const [count, setCount]  = useState(0)
  return <Button {...args} label={count} onClick={setCount} />
}

Into:

export const Counter = {
  render: (args) => {
    const [count, setCount]  = useState(0)
    return <Button {...args} label={count} onClick={setCount} />
  },
}

This triggers a rules of hooks eslint error:

image

Describe the solution you'd like

Detect likely hooks (useX) and transform into:

export const Counter = {
  render: function Render (args) {
    const [count, setCount]  = useState(0)
    return <Button label={count} onClick={setCount} />
  },
}

Describe alternatives you've considered

Support Render alias:

export const Counter = {
  Render: (args) => {
    const [count, setCount]  = useState(0)
    return <Button {...args} label={count} onClick={setCount} />
  },
}

Are you able to assist to bring the feature to reality?

no

Additional context

No response

@shilman shilman added maintenance User-facing maintenance tasks csf3 labels Feb 16, 2023
@shilman shilman changed the title CSF3: Handle react hooks CSF3: Handle react hooks better Feb 16, 2023
@IanVS
Copy link
Member

IanVS commented Feb 16, 2023

Just curious, why not add an alias? That seems to be one thing that people try to do when writing their own stories too, not just when converting from csf2 to csf3.

@shilman shilman added react bug and removed maintenance User-facing maintenance tasks labels Feb 17, 2023
@shilman shilman moved this to Required for GA in Core Team Projects Feb 17, 2023
@tmeasday
Copy link
Member

tmeasday commented Mar 7, 2023

@shilman I think it'd probably make sense to alias Render => render. Would be easy to do I think. But we'd still have to figure out how to update the codemod.

@shilman shilman moved this from Required for GA to Nice to have in Core Team Projects Mar 21, 2023
@AntonNiklasson
Copy link

I just ran into this, and decided to disable the lint rule in *.stories.tsx as a temporary solution. I'm not sure what solution is best though, none of them seem ergonomic to me.

I agree the codemod should handle it better, but I'm probably going to run in to this again coding by hand.

{
  "files": ["**/*.stories.tsx"],
  "rules": {
    "react-hooks/rules-of-hooks": "off"
  }
}

@mctrafik
Copy link

mctrafik commented Jan 3, 2024

What's the point of wrapping stories that only have a single FC into an object with a function that isn't a react component? Feels like storybook syntax is wrong here.

@valentinpalkovic valentinpalkovic changed the title CSF3: Handle react hooks better Codemod: Handle react hooks better for CSF3 Feb 5, 2024
@vanessayuenn vanessayuenn removed this from the 8.0-RC milestone Feb 5, 2024
@shilman
Copy link
Member Author

shilman commented Feb 5, 2024

@mctrafik Storybook still supports older versions of CSF for precisely this reason, and will continue to support them in the foreseeable future.

@emlai
Copy link

emlai commented Feb 6, 2024

^ Why support multiple syntaxes that work for different use cases, instead of supporting a single syntax that works for all use cases?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants