react-rails icon indicating copy to clipboard operation
react-rails copied to clipboard

Suggestion to add functional option to component generator

Open CaitlinWeb-st opened this issue 4 years ago • 3 comments

Thank you for this wonderful library, it's helped me set up React in Rails when I'm new to Rails. It's especially nice to have rails g react:component for quickly creating new components. I'm using a lot of functional components in my project, though, so it's not as useful.

What about adding an option to make it a functional component? Something like rails g react:component --func or rails g react:component --function.

This would create:

import React from "react"
import PropTypes from "prop-types"
function SomeComponent() {
  return (
    <React.Fragment>
      Example: {this.props.example}
    </React.Fragment>
  );
}

SomeComponent.propTypes = {
  example: PropTypes.text
};
export default SomeComponent

CaitlinWeb-st avatar May 05 '20 14:05 CaitlinWeb-st

This would be a super helpful feature. Currently I manually change all class components to functional, since in our workflow we usually stick with functional components as starters.

yedhink avatar Oct 01 '20 05:10 yedhink

It's possible to override the template by copying it into your Rails project at this path lib/templates/react/component/component.es6.jsx and then modifying it. Took me a while to find the correct path, hope it helps someone!

wasabigeek avatar Nov 06 '20 16:11 wasabigeek

Worth noting that React now recommends using functional components. This should be the default with the class based generator as an option

paulodeon avatar Apr 24 '21 17:04 paulodeon

@justin808 Since officially it is recommended to use function components, isn't it better we use such components by default unless the developer wants to make a class based one?

If not, this issue is resolved by #1093.

ahangarha avatar Jan 05 '23 14:01 ahangarha

Hi just checking back in on this. Has there been any more work on adding functional components into react-rails?

harrison-broadbent avatar Mar 13 '23 01:03 harrison-broadbent

@justin808 Let's add this to the list for the version 3, to create a functional component unless the specific option for a class component is given.

ahangarha avatar Apr 25 '23 18:04 ahangarha

Please check the add-option-for-generating-functional-component branch and share your feedback.

cc: @CaitlinWeb-st @yedhink @wasabigeek @harrison-broadbent

ahangarha avatar May 01 '23 17:05 ahangarha