React Component Generator is a CLI tool for automating the creation of React component structures with TypeScript and BEM. This package helps you quickly generate files for new components, including index.tsx, typings, constants, and styles.
- Generates a folder structure for a React component with:
index.tsx— the main component file.ComponentName.typings.ts— for TypeScript typings.ComponentName.const.ts— constants and BEM selectors.ComponentName.scss— styling file.
- Fully TypeScript-supported.
- Configurable CLI interface.
- Clone the repository or download the source code.
- Install dependencies:
npm install
- Install dependencies:
npm run build
Install the package globally using npm:
npm install -g @sysoevandrey/generate-react-componentAfter a global installation, you can run the generate-react-component command in your terminal:
generate-react-componentThe CLI will prompt you to provide:
- The target directory for the component (e.g., ./src/components).
- The component name (e.g., MyComponent).
It will generate the following structure:
src/components/MyComponent/
├── MyComponent.const.ts
├── MyComponent.scss
├── MyComponent.typings.ts
├── index.tsx
With the following templated content:
import type { FC } from 'react';
import { cnMyComponent } from './MyComponent.const';
import type { IMyComponentProps } from './MyComponent.typings';
import './MyComponent.scss';
export const MyComponent: FC<IMyComponentProps> = () => {
return <p className={cnMyComponent}>MyComponent</p>;
};
If the package is installed locally, use:
npx generate-react-componentYou can customize the file templates to suit your project. Templates are located in src/utils/templates.ts and return strings for file generation.
For example, to customize the index.tsx template:
export const getIndexTemplate = (componentName: string): string =>
`import React from 'react';\n\nexport const ${componentName} = () => {\n return <div>${componentName}</div>;\n};\n`;MIT
If you have any questions or suggestions, please create an issue in the project repository or contact me.