React defines and speculates about how to manage code files for components. There are so many documentation and tools available as per the best practices and perfect scenaios. However as per my experience no perfect scenario
ever worked for any individual or company.
There is difference between perfect scenario and working scenario.
While everyone should try to stick to the best practices and perfect scenarios, it is a perpetual job. It never ends. Cause may be anything, but it never gets complete. There exists a working scenario.
In my experience the working scenario is always a better place to be. It might not be exactly as per the best practices or suggested docs, but it gets the job done nicely in time and have a long term favourable result.
Managing the Component Code, should be as per the project.
Like everything else in software development, this one should also be as per the project. There should not be one size fits all solution. To start with, each Component
should have its own file. Which is good, but soon the project has thousands of files and sometimes, the team writes the same component in different files. That means, we have one component logic but there are 2 files. Whichever you use, you will get the same result. The working scenario
in this case is writing the supporting components in the same file as the main component. This way, there is only one main component file and the smaller components are all inside this file. Once you include the component, you have it running. While removing, just remove one component(file) and it is done.
It comes as one, goes as one.
This way, to maintain the component and project becomes much easier. If there is a need that the supporting components are required in other part of the project, there are 2 ways to deal with this.
- Export the supporting component from the same file
- Extract the code to a new component file
Depending upon the situation, one can decide which way to go.
The best way to write an application code, is to make it so beautiful that if some code file goes out of the project, it takes all of its traces out of it. Likewise, when included in the project, it should be self sufficient.
Happy Coding.