2
3 Comments

Class Component to Functional Component Converter?

When you use React for development, how often do you have to convert a class component to a functional component?

Recently I had this issue where I had to re-use some components from an old project and in order to match the code style of the current component, I had to convert those class components to the functional components.
I tried searching for an online tool that could help fasten this conversion process, but there wasn't any.

What tools do you use when you try to convert a react class based component to a functional component?

If there's none and anyone wants to build a tool for it, I will try to help with it.

on October 28, 2020
  1. 1

    I personally haven't hated doing this by hand previously. If your class component is complicated enough that it's not an easy by-hand change, then the problem might actually be that your component is trying to do too many things.

  2. 1

    Personally I don’t rewrite old components to hooks. Classes don’t go anywhere so I don’t see a problem in having class components with functional components inside one code base.

  3. 1

    I’m not sure it’s possible to automate this and catch all edge cases, although I suppose an automated tool could get you 80% of the way there by adding useState and converting lifecycle methods to useEffect. I generally do this by hand and haven’t found it to be a big deal. I have some older components as class components and I won’t bother refactoring unless I really need to take advantage of hooks.

Trending on Indie Hackers
Most founders don't have a product problem. They have a visibility problem User Avatar 105 comments Day 4: Why I Built a $199 Workspace Nobody Asked For User Avatar 54 comments Spent months building LazyEats AI. Spent 1 day realizing I have no idea how to get users. User Avatar 35 comments Hi IH — quick update. The MVP is live. User Avatar 27 comments I Built a Football Sentiment Platform in 18 Days. The World Cup Starts in 7 Days. Now I Need Distribution. User Avatar 17 comments I built a $5/1k-listing CRE data API because CoStar is overkill for first-pass scans User Avatar 14 comments