DEV Community

Cover image for Create react components at the speed of light with Plop.js

Create react components at the speed of light with Plop.js

Ivan on January 09, 2022

So you are working in your React app, and it comes the time to create a new component, you already have your ritual down. Go to the components fold...
Collapse
 
amirsohel007 profile image
Amir Sohel

this is great ! can we add import same route in router file also?

Collapse
 
ivanms1 profile image
Ivan

Yeah you can append code to existing file with append action. Something like this

      {
        // Action type 'append' injects a template into an existing file
        type: 'append',
        path: 'src/routes/index.js',
        // Pattern tells plop where in the file to inject the template
        pattern: `/* PLOP_INJECT_IMPORT */`,
        template: `import {{pascalCase name}} from './{{pascalCase name}}';`,
      },
Enter fullscreen mode Exit fullscreen mode
Collapse
 
amirsohel007 profile image
Amir Sohel

but also we need to create router :(

Thread Thread
 
amirsohel007 profile image
Amir Sohel

your linkedin? how can we connect

Thread Thread
 
ivanms1 profile image
Ivan

yeah, let's talk on twitter

Collapse
 
tarasg profile image
Taras Gordienko • Edited

Great! How can I add a dynamic path for files instead of the default src/components/?

Collapse
 
ivanms1 profile image
Ivan • Edited

Thank you!
You can change the path of the action to whatever you want, for example

path: "src/routes/{{pascalCase name}}/.tsx",
Enter fullscreen mode Exit fullscreen mode

or a dynamic value from the prompts

path: "{{path}}/{{pascalCase name}}/.tsx",
Enter fullscreen mode Exit fullscreen mode
Collapse
 
nabilsliti profile image
Nabil SLITI

Great, and currently there is a package which is based on plop and preconfigured to create components in react, angular, vue and stencil. npmjs.com/package/plop-gen
Create any component and you can rename it and you can choose where you want to create it.
Good luck

Collapse
 
kepobangetmyid profile image
Kepobanget

Thanks

Collapse
 
ivanms1 profile image
Ivan

You're welcome!

Collapse
 
rohil_cris profile image
Rohil

Great article ivan.

Collapse
 
ivanms1 profile image
Ivan

Thanks Rohil!