Javascript 使用 create-react-app 创建新 React 组件的最佳方式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/51942009/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Best way to create new React component using create-react-app
提问by phantomCoder
I am using create-react-app. Is there a command to create a component to scaffold the files.
我正在使用create-react-app. 是否有一个命令来创建一个组件来构建文件。
Right now I am manually creating the files. Say if the component name is Button, I create a folder named Button inside the component folder and then create Button.jsand Button.cssfiles.
现在我正在手动创建文件。假设组件名称是Button,我在组件文件夹中创建一个名为 Button 的文件夹,然后创建Button.js和Button.css文件。
Is there any CLI commands to do this using create-react-app? I know there is a way to do this Angular CLI but don't know anything with React CLI.
是否有任何 CLI 命令可以使用 create-react-app 执行此操作?我知道有一种方法可以执行此 Angular CLI,但对 React CLI 一无所知。
回答by Viraj
you can use https://www.npmjs.com/package/generate-react-componentfor this,
您可以为此使用https://www.npmjs.com/package/generate-react-component,
this will help you to create state and stateless component also known as functional and class components.
这将帮助您创建有状态和无状态组件,也称为功能组件和类组件。
回答by Pouria
Another way is to create files manually and use snippets to generate the code.
另一种方法是手动创建文件并使用片段来生成代码。
For example, take a look at this JavaScript/React VS Code extension. You can also create your own snippets or find other extensions if you were interested.
例如,看看这个JavaScript/React VS Code 扩展。如果您有兴趣,您还可以创建自己的片段或查找其他扩展。
回答by Leo
You may use directly without installing the generate-react-cli
不用安装generate-react-cli就可以直接使用
npx generate-react-cli component MyComponent
Which will install a component based on your project preferences (ex: typescript, scss, etc..).
这将根据您的项目首选项(例如:typescript、scss 等)安装一个组件。
A file called generate-react-cli.jsonwill be created where all the settings will be stored for future use.
generate-react-cli.json将创建一个名为的文件,其中将存储所有设置以供将来使用。

