3

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.

sunquan
  • 322
  • 3
  • 9
  • Welcome to StackOverflow! Please read [How to ask](http://stackoverflow.com/help/how-to-ask) – Lahiru Jayaratne Nov 27 '15 at 03:54
  • Hi @Lahiru Is that my question isn't related to a specific problem I encounter? – sunquan Nov 27 '15 at 04:36
  • Sorry, if my comment seems to be offensive. What I only meant was try to do some formatting with the provided markdown for others to understand it better :) – Lahiru Jayaratne Nov 27 '15 at 05:02
  • 1
    @Lahiru never mind, I'm just trying to figure out the right way. It's very nice of you to write the "suggested edits" for me. – sunquan Nov 27 '15 at 05:31
  • In Rails using `_` as a prefix denotes that the file is a partial. A partial is a small piece of code that you can reuse in several other parts of your application. – Henrik Andersson Nov 27 '15 at 05:57
  • 1
    @limelights Thanks! I've also been using partials in my rails views and call 'render' method to render the partial. But if they are all *.js.jsx files, likely that you can't call any rails method within those files. Can you provide an example of reusing the code in the [Sample React Rails App](https://github.com/bensmithett/sample-react-rails-app/tree/master/app/assets/javascripts/react_components)? – sunquan Nov 27 '15 at 06:26

1 Answers1

1

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.

sunquan
  • 322
  • 3
  • 9