Can I use functional component and class component together?

5.2k views Asked by At

I am working on a project with Micro Front End architecture. There will be several Micro Front Ends and different team works with different MFE.

Are there any technical difficulties to use the class component and functional component together in a react project? Or it is just bad practice?

2

There are 2 answers

0
Lazar Nikolic On

This is purely opinion based question, so I will give opinion based answer.

There are no technical difficulties in using both of these types of components together. You can use them at the same time in single project. Is it a bad practice? I do not think so. I guess it depends on what those components ought to do. My dummy, representational components are often functional while components that are connected to redux are class components. But, I guess other people would disagree with me.

1
TLadd On

You can use functional and class components in the same project. Prior to hooks being introduced, this was nearly a requirement since class components were the only ones that could hold component state and hook into life cycles.

Since hooks have been introduced and largely removed the differences in terms of what can be accomplished with class and function components (although there are still a few holdouts), it is generally recommended to use function components and hooks.

So to summarize, there are no technical problems with using class and functional components together, but it is generally recommended for new development to use function components.