I’m starting learning about reactjs. But when i watch tutorial on youtube i often see that when definite component in sperate file and export, people always name the file name with component name defining in the file is similar.
For example:I see that to define a component name Book the video always names the file of Book component is** Book.js**
And i wonder that this is official rule of reactjs or may be convention of Reactjs community.
Can i name the file name and component in the file is difference?
2
Answers
React doesn’t care about your filenames at all (and in fact, if you use a bundler, it’s going to combine source files into a small number of scripts for distribution). You can put one component in each file, or all of your components in a single file, and either way, it makes no difference to React. Components are just functions (either function components using hooks, or constructor functions for
class
components). Organize them as you see fit.Here’s an example with three components in the same script tag (loosely, "file"):
These rules are generally accepted by developers. Even the eslint rules will show an error. But the react will work with other names as well