StoryMay 16, 20252 min read

šŸ› ļø Automate Your React Native Workflow with a Custom Yarn Command

Create React Native components with a single command and boost your dev productivity šŸš€


šŸ› ļø Automate Your React Native Workflow with a Custom YarnĀ Command

Create React Native components with a single command and boost your dev productivity šŸš€

šŸ”„ Read for free šŸ‘‰

šŸ¤” TheĀ Problem

As a React Native developer, one of the most repetitive tasks we all face is setting up component folders:

  • index.tsx for the UI
  • styles.ts for styles
  • useComponent.ts for custom logic

You know the drill. Every time, we write the same boilerplateā€Šā€”ā€Šwasting time and breaking focus.

Wouldn’t it be amazing to just run something like:

…and instantly have this?

Let’s make that dream real. šŸ”„

šŸŽÆ What You’llĀ Build

A simple, reusable CLI tool using a Yarn script that:

  • Takes a component name (Header, LoginForm, etc.)
  • Creates a folder under /components/
  • Adds index.tsx, styles.ts, and useComponent.ts files
  • Uses standard React Native + TypeScript boilerplate

All this with just one command.


āš™ļø Step 1: Create theĀ Script

At the root of your project, create a folderā€Šā€”ā€Šscripts

and create a typescript file

touch generate-component.js

Now paste this code:

const fs = require('fs');
const path = require('path');

const componentName = process.argv[2];

if (!componentName) {
  console.error(
    'āŒ Please provide a component name (e.g. yarn component [Component Name]])',
  );
  process.exit(1);
}

const basePath = path.join(
  `${__dirname}`,
  '..',
  'src',
  'components',
  componentName,
);
if (fs.existsSync(basePath)) {
  console.error(`āŒ ${componentName} Component already exists!`);
  process.exit(1);
}

fs.mkdirSync(basePath, { recursive: true });

// index.tsx
fs.writeFileSync(
  path.join(basePath, 'index.tsx'),
  `import React from 'react';
import { View, Text } from 'react-native';
import styles from './styles';
import use${componentName} from './use${componentName}';

const ${componentName} = () => {
  const {} = use${componentName}();

  return (
    <View style={styles.container}>
      <Text>${componentName} Component</Text>
    </View>
  );
};

export default ${componentName};
`,
);

// styles.ts
fs.writeFileSync(
  path.join(basePath, &#x27;styles.ts&#x27;),
  `import { StyleSheet } from &#x27;react-native&#x27;;

export default StyleSheet.create({
  container: {
    padding: 16,
  },
});
`,
);

// useComponent.ts
fs.writeFileSync(
  path.join(basePath, `use${componentName}.ts`),
  `const use${componentName} = () => {
  return {};
};

export default use${componentName};`,
);

console.log(
  `āœ… Component ${componentName} created in components ${componentName}`,
);

🧩 Step 2: Add a Yarn Script

Open your package.json and add this under scripts:

"scripts": {
  "component": "node scripts/generate-component.ts"
}

This lets you run yarn component ComponentName, where component is a custom alias for the component generator.

🧪 Step 3: Use It!

yarn component Headers

Boom šŸ’„!

Your src/components/Header/ folder will be created with all the essential files.


šŸ’” Why This IsĀ Awesome

  • 🧠 Saves mental energy
  • ā±ļø Boosts speed
  • 🧹 Keeps folder structure consistent
  • 🧰 Easy to extend for screens, Redux modules, tests, etc.

This is the kind of small productivity booster that pays off big over time.

Instead of creating folders manually 100+ times across a project, automate it once and focus your energy on building actual features.

Let me know in the comments if you want a create-screen version with navigation next. šŸ’¬


šŸ“Œ Follow Me forĀ More

Did you find this helpful? Give a Clap and Share it with your friends!

For more updates: subscribe to my medium account.

Thank you for reading 😊