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.
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"
}
}
The following command can be used to generate code:
npm run gen-react-code -- -n example-component -d src/example/dir -r
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. |
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.
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
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
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.
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
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 ReduxProvider
where you're rendering your main app, which is usually located insrc/index.js
A great example on how to use generate-react-code
can be found here (courtesy of ZuluCoda).