I'm just getting started with React
in Rails
.
When I come across other's Rails
apps with React
, I find some of them add a leading underscore when naming *.js.*
files in /app/assets/javascripts/components/
.
e.g. Sample React Rails App
Component files are like:
_comment.js.jsx
While some don't, e.g. Account React Rails App
Component files are like:
record.js.coffee
So what difference does it make to add the leading underscore?
I know in Rails
view, naming a *.html.erb
file with leading underscore means it's a partial which you can reuse and we call render
method to render it. But here the *.js.*
files are require by components.js
with //= require_tree ./components
. So even you remove the underscore, it makes no difference. And I believe this is the only part which "reuse" the component.
As there's //= require_tree ./components
in application.js
. I think it really doesn't make any difference to name them with leading underscore.
Then I think there may be something to do with the Rails asset pipeline, because actually the component
files are required duplicately in the sample-react-rails-app. The underscores might be for avoid duplication when precomiling.
I've tried remove the leading underscores in those file and run the app locally (also I make asset pipeline precompile in dev env). It turns out that even if you remove the leading underscores, precompilation for those files still doesn't get duplicated and the app works just fine. As a matter of fact, asset pipeline will only include the same file once even if you require
the file duplicately.
In conclusion, the leading underscore doesn't make any difference in how your app works. It's probably a naming convention following partial naming like in Rails views or SASS compilation which was also mentioned in Rails Issues #3094. And Generally I think the convention is worth following to tell yourself and your team those files work as incomplete parts.