
Research
/Security News
Toptal’s GitHub Organization Hijacked: 10 Malicious Packages Published
Threat actors hijacked Toptal’s GitHub org, publishing npm packages with malicious payloads that steal tokens and attempt to wipe victim systems.
generate-react-cli
Advanced tools
To help speed up productivity in React projects and stop copying, pasting, and renaming files each time you want to create a new component.
A few notes:
npm i -g generate-react-cli
generate-react component Box
npx generate-react-cli component Box
(npx is a package runner tool that comes with npm 5.2+ and higher)
When you run generate-react-cli within your project the first time, it will ask you a series of questions to customize the cli for your project needs (this will create a "generate-react-cli.json" config file).
{
"usesTypeScript": false,
"usesCssModule": true,
"cssPreprocessor": "scss",
"testLibrary": "Testing Library",
"component": {
"path": "src/components",
"withLazy": false,
"withStory": false,
"withStyle": true,
"withTest": true
}
}
By default, GRC comes with one component command out of the box.
What if you wanted to generate components with your own custom commands, like for example page or layout that have their own set of component config rules?
You can do so by extending the generate-react-cli.json config file like this.
{
"usesTypeScript": false,
"usesCssModule": true,
"cssPreprocessor": "scss",
"testLibrary": "Testing Library",
"component": {
"path": "src/components",
"withLazy": false,
"withStory": false,
"withStyle": true,
"withTest": true
},
"page": {
"path": "src/pages",
"withLazy": true,
"withStory": false,
"withStyle": true,
"withTest": true
},
"layout": {
"path": "src/layout",
"withLazy": false,
"withStory": false,
"withStyle": false,
"withTest": true
}
}
Make sure to include the required properties listed below when creating the custom component commands. Otherwise, GRC will not register them as a custom component command.
The required properties are as follows:
Options | Description | Value Type |
---|---|---|
--path | Value of the path where you want the component to be generated in (e.g. src/components). | String |
--withLazy | Creates a corresponding lazy file (a file that lazy-loads your component out of the box and enables code splitting) with this component. | Boolean |
--withStory | Creates a corresponding (storybook) story file with this component. | Boolean |
--withStyle | Creates a corresponding stylesheet file with this component. | Boolean |
--withTest | Creates a corresponding test file with this component. | Boolean |
Once you have done that, you should be able to run the custom component commands like this:
npx generate-react-cli page HomePage
npx generate-react-cli layout BoxLayout
You can also pass the same options to your custom component commands as you would for the default component command that comes with GRC.
You can now create custom templates that Generate React CLI can use instead of the built-in templates that come with it. We hope this will provide more flexibility for your components and pages you want to generate.
Both the component
and page
properties (within the generate-react-cli.json config file) can accept an optional customTemplates
object property.
customTemplates
object:"customTemplates": {
"component": "templates/component.js",
"lazy": "templates/lazy.js",
"story": "templates/story.js",
"style": "templates/style.scss",
"test": "templates/test.js"
},
The keys represent the type of template, and the values are the paths that point to where your custom template lives in your project/system.
customTemplates
property in the generate-react-cli.json config file:{
"usesTypeScript": false,
"usesCssModule": true,
"cssPreprocessor": "scss",
"testLibrary": "Testing Library",
"component": {
"customTemplates": {
"component": "templates/component/component.js",
"style": "templates/component/style.scss",
"test": "templates/component/test.js"
},
"path": "src/components",
"withStyle": true,
"withTest": true,
"withStory": true,
"withLazy": false
},
"page": {
"customTemplates": {
"test": "templates/page/test.js"
},
"path": "src/pages",
"withStyle": true,
"withTest": true,
"withStory": false,
"withLazy": true
}
}
Notice in the page.customTemplates
that we only specified the "test" custom template type. That's because all the custom template types are optional. If you don't set the other types, the CLI will default to using the built-in templates it comes with.
// templates/component/component.js
import React from 'react';
import styles from './TemplateName.module.css';
const TemplateName = () => (
<div className={styles.TemplateName} data-testid="TemplateName">
<h1>TemplateName component</h1>
</div>
);
export default TemplateName;
Important - Make sure to use the TemplateName
keyword in your templates. The CLI will use this keyword to replace it with your component name.
// templates/component/test.js
import React from 'react';
import ReactDOM from 'react-dom';
import TemplateName from './TemplateName';
it('It should mount', () => {
const div = document.createElement('div');
ReactDOM.render(<TemplateName />, div);
ReactDOM.unmountComponentAtNode(div);
});
npx generate-react-cli component Box
This command will create a folder with your component name within your default (e.g. src/components) directory, and its corresponding files.
|-- /src
|-- /components
|-- /Box
|-- Box.js
|-- Box.css
|-- Box.test.js
You can also override some of the generate-react-cli default component config options using one-off commands. So for example, let's say you have set withTest to be true
in the component config property. You can override it like this:
npx generate-react-cli component Box --withTest=false
Or vice versa, if you have set withTest to be false
you can do this:
npx generate-react-cli component Box --withTest=true
Otherwise, if you don't pass any options, it will just use the default values from the generate-react-cli config file you have set.
Options | Description | Value Type |
---|---|---|
--path | Value of the path where you want the component to be generated in (e.g. src/components). | String |
--withLazy | Creates a corresponding lazy file (a file that lazy-loads your component out of the box and enables code splitting) with this component. | Boolean |
--withStory | Creates a corresponding (storybook) story file with this component. | Boolean |
--withStyle | Creates a corresponding stylesheet file with this component. | Boolean |
--withTest | Creates a corresponding test file with this component. | Boolean |
Generate React CLI is open source software licensed as MIT.
FAQs
A simple React CLI to generate components instantly and more.
The npm package generate-react-cli receives a total of 5,499 weekly downloads. As such, generate-react-cli popularity was classified as popular.
We found that generate-react-cli demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
/Security News
Threat actors hijacked Toptal’s GitHub org, publishing npm packages with malicious payloads that steal tokens and attempt to wipe victim systems.
Research
/Security News
Socket researchers investigate 4 malicious npm and PyPI packages with 56,000+ downloads that install surveillance malware.
Security News
The ongoing npm phishing campaign escalates as attackers hijack the popular 'is' package, embedding malware in multiple versions.