npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

generate-react-code

v2.0.0

Published

An automated React code generation tool for React web, React-Native, and/or Redux in the ducks modular pattern.

Downloads

85

Readme

Generate React Code

Build Status Coverage Status code style: prettier

js-standard-style

This project utilises a scaffolding framework which generates React or React-Native code along with all the essential test code... Because who likes writing that themselves?!

Additionally, it can be used to generate Redux code conforming to the Redux ducks pattern - and it can also be used to generate the Redux core files needed for React-Redux projects (store, root-reducer, and action-utilities).

This generated code conforms to the Air BnB style guide's naming and coding-style conventions, and it is thus highly recommended to make use of this tool when creating new React or React-Redux components.

This package also allows users to add a configuration file containing default parameters. That way users would not have to specify these parameters every time they wish to generate code.

IMPORTANT NOTE:

  • This package assumes the use of sass for React web projects.
  • This package assumes the use of Redux-Thunk as a Redux middleware.
  • This package assumes the use of enzyme and jest for React testing.

Installation

To install and save this npm package, navigate to your project's root directory in console and execute the following command:

npm install generate-react-code --save-dev

Then add the following script to your packages.json file:

{
  "scripts": {
    "gen-react-code": "generate-react-code"
  }
}

Generation Command

The following command can be used to generate code:

npm run gen-react-code -- -n example-component -d src/example/dir -r

Command Parameter Description:

|Parameter|Description|Default Value| |---------|-----------|-------| | -n OR--name | This is the lower kebab case name of the feature/component you would like to generate (e.g. kebab-example-name). | kebab-example-name | | -d OR--directory | This is the relative directory where the generated component will be placed (e.g src/components). | src/components | | -N OR--native | If you wish to generate code for React-Native, add this parameter - else React web code will be generated. | false | | -r OR--redux | If you wish to generate Redux code in the duck pattern, add this parameter - else regular React code will be generated. | false | | -o OR--omit-comments | If you wish to hide the comments within the generated files, add this parameter - else descriptive comments will be left in the generated code. | false | | -R OR--redux-core | If you would like to generate the Redux core files (store, root-reducer, and action-utilities), add this parameter. These files are used to connect your application with Redux. | false | | -D OR--redux-core-directory | This is the relative directory where the generated Redux core file will be placed (e.g src/redux). It is recommended to leave this as the default. | src/redux | | -h OR--help | Output help usage information. | |

Configuration File

If you wish to store default parameters for your project, you'll have to add an optional grcc.json (generate react code config) file to your project's root directory. The file must have the following structure:

{
  "native": true,
  "redux": true,
  "omitComments": true
}

IMPORTANT NOTE:

  • All these parameters are false by default. The parameters you wish to remain so, may be omitted from the file.
  • If you specify any of these parameters in your generation command, the generation command parameters will take priority over the ones in the grcc.json file.
  • The grcc.json file is completely optional and does not have to be added.

Generated Output Examples

React Example

Given the following example code generation command:

npm run gen-react-code -- -n example-component -d src/components

The following file/folder structure will be generated (take note that the example-component directory is generated without you having to specify it explicitly):

project
└───src
    └───components
        └───example-component
            │   example-component.view.js
            │   _example-component.styles.scss
            └───test
                │   example-component.view.spec.js

Within these files the majority of the React code will be completed for you - which contains detailed comments on how to add your functionality and general best practices.

IMPORTANT NOTE:

  • Remember to add generated style sheets to the main style sheet, which is usually located in src/index.scss

Demo

React Example With Config

Given the following grcc.json config file:

{
 "native": true,
 "redux": true
}

And given the following example code generation command:

npm run gen-react-code -- -n example-component -d src/components

The following file/folder structure will be generated (take note that the example-component directory is generated without you having to specify it explicitly):

project
└───src
    └───components
        └───example-component
            │   example-component.view.js
            │   _example-component.styles.scss
            └───test
                │   example-component.view.spec.js

Within these files the majority of the React code will be completed for you - which contains detailed comments on how to add your functionality and general best practices.

IMPORTANT NOTE:

  • Remember to add generated style sheets to the main style sheet, which is usually located in src/index.scss

Demo

React Native Example

Given the following example code generation command:

npm run gen-react-code -- -n example-component -d src/components -N

The following file/folder structure will be generated (take note that the example-component directory is generated without you having to specify it explicitly):

project
└───src
    └───components
        └───example-component
            │   example-component.view.js
            └───test
                │   example-component.view.spec.js

Within these files the majority of the React-Native code will be completed for you - which contains detailed comments on how to add your functionality and general best practices.

Demo

React With Redux Example

Given the following example code generation command:

npm run gen-react-code -- -n example-component -d src/components -r

The following file/folder structure will be generated (take note that the example-component directory is generated without you having to specify it explicitly):

project
└───src
    └───components
        └───example-component
            │   example-component.container.js
            │   example-component.reducer.js
            │   example-component.view.js
            │   _example-component.styles.scss
            └───test
                │   example-component.container.spec.js
                │   example-component.reducer.spec.js
                │   example-component.view.spec.js

Within these files the majority of the React web code will be completed for you - which contains detailed comments on how to add your functionality and general best practices.

IMPORTANT NOTE:

  • Remember to add generated reducers to the root reducer, which is usually located in src/redux/root-reducer.js
  • Remember to add generated style sheets to the main style sheet, which is usually located in src/index.scss

Demo

Redux Core Files Example

Given the following example code generation command:

npm run gen-react-code -- -R

The following file/folder structure will be generated (take note that adding the -R parameter will cause only the core files to be generated, irrespective of the other parameters):

project
└───src
    └───redux
        │   store.js
        │   root-reducer.js
        └───action-creator
            │   build-action-type.js
            │   create-action.js
            └───test
                │   build-action-type.spec.js
                │   create-action.spec.js

Within these files the majority of the React-Redux core code will be completed for you - which contains detailed comments on how to add your reducers and general best practices.

IMPORTANT NOTE:

  • Remember to add your store to your Redux Provider where you're rendering your main app, which is usually located in src/index.js

Demo

Extra Material

A great example on how to use generate-react-code can be found here (courtesy of ZuluCoda).