React & React Native Component Generator
This VS Code extension helps you generate React and React Native components quickly and efficiently. It also includes utilities for form validation, date operations, Redux actions and reducers, Axios API services, and React context providers.
Features
- Generate basic React functional components.
- Generate React class components.
- Generate React Native functional components.
- Generate React Native card components.
- Generate React Native modal components.
- Integrate React Native stack navigation.
- Integrate React Native tab navigation.
- Add basic React Native animations.
- Perform API calls with all methods using Axios.
- Form validation utilities.
- Date operations utilities.
- Redux actions and reducers.
- Axios API service.
- React context and provider.
- Express.js route handler.
- Express.js middleware.
- React Higher-Order Component (HOC).
- Custom React hook.
- React Native StyleSheet.
- React and React Native login and signup components.
- Express.js JWT authentication middleware.
- Real-time notifications using WebSockets for Express and NestJS backends, and React/React Native frontends.
- API documentation generation.
- Comprehensive logging module.
Installation
Install the extension from the VS Code marketplace.
Clone the repository if you prefer to modify or extend the extension:
git clone https://github.com/shaaz1000/react-reactnative-component-generator.git
cd react-reactnative-component-generator
npm install
npm run compile
Run the extension:
- Open the project in VS Code.
- Press
F5 to open a new VS Code window with your extension loaded.
Usage
- Create a new file in your project with a meaningful name. For example,
MyComponent.js .
- Type one of the following commands to generate the corresponding component or utility:
React Components
React Native Components
React Native Functional Component:
React Native Card Component:
- Command:
Create React Native Card Component
- Generated Code:
import React from 'react';
import { View, Text, StyleSheet } from 'react-native';
const MyComponent = () => {
return (
<View style={styles.card}>
<Text>MyComponent Component</Text>
</View>
);
}
const styles = StyleSheet.create({
card: {
padding: 10,
margin: 10,
backgroundColor: '#fff',
shadowColor: '#000',
shadowOpacity: 0.2,
shadowRadius: 5,
borderRadius: 5,
},
});
export default MyComponent;
React Native Modal Component:
- Command:
Create React Native Modal Component
- Generated Code:
import React, { useState } from 'react';
import { Modal, View, Text, Button, StyleSheet } from 'react-native';
const MyComponent = () => {
const [visible, setVisible] = useState(false);
return (
<View>
<Button title="Show Modal" onPress={() => setVisible(true)} />
<Modal
visible={visible}
transparent={true}
animationType="slide"
>
<View style={styles.modal}>
<Text>MyComponent Component</Text>
<Button title="Close" onPress={() => setVisible(false)} />
</View>
</Modal>
</View>
);
}
const styles = StyleSheet.create({
modal: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: 'rgba(0, 0, 0, 0.5)',
},
});
export default MyComponent;
React Native Stack Navigation:
- Command:
Create React Native Stack Navigation
- Generated Code:
import React from 'react';
import { NavigationContainer } from '@react-navigation/native';
import { createStackNavigator } from '@react-navigation/stack';
import { View, Text, Button } from 'react-native';
const Stack = createStackNavigator();
const HomeScreen = ({ navigation }) => (
<View>
<Text>Home Screen</Text>
<Button title="Go to Details" onPress={() => navigation.navigate('Details')} />
</View>
);
const DetailsScreen = () => (
<View>
<Text>Details Screen</Text>
</View>
);
const MyComponent = () => {
return (
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen name="Home" component={HomeScreen} />
<Stack.Screen name="Details" component={DetailsScreen} />
</Stack.Navigator>
</NavigationContainer>
);
}
export default MyComponent;
React Native Tab Navigation:
- Command:
Create React Native Tab Navigation
- Generated Code:
import React from 'react';
import { NavigationContainer } from '@react-navigation/native';
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';
import { View, Text } from 'react-native';
const Tab = createBottomTabNavigator();
const HomeScreen = () => (
<View>
<Text>Home Screen</Text>
</View>
);
const SettingsScreen = () => (
<View>
<Text>Settings Screen</Text>
</View>
);
const MyComponent = () => {
return (
<NavigationContainer>
<Tab.Navigator>
<Tab.Screen name="Home" component={HomeScreen} />
<Tab.Screen name="Settings" component={SettingsScreen} />
</Tab.Navigator>
</NavigationContainer>
);
}
export default MyComponent;
Utilities
Express.js Components
Express Route:
- Command:
Create Express Route
- Generated Code:
const express = require('express');
const router = express.Router();
// GET endpoint
router.get('/', (req, res) => {
res.send('GET request to the homepage');
});
// POST endpoint
router.post('/', (req, res) => {
res.send('POST request to the homepage');
});
// PUT endpoint
router.put('/:id', (req, res) => {
res.send(`PUT request to update item with id ${req.params.id}`);
});
// DELETE endpoint
router.delete('/:id', (req, res) => {
res.send(`DELETE request to delete item with id ${req.params.id}`);
});
module.exports = router;
Express Middleware:
Advanced React Components
Authentication Components
Real-Time Notifications
API Documentation
- Generate API Documentation:
Logging
- Create Logger Module:
- Command:
Create Logger Module
- Generated Code:
const winston = require('winston');
const logger = winston.createLogger({
level: 'info',
format: winston.format.json(),
defaultMeta: { service: 'user-service' },
transports: [
new winston.transports.File({ filename: 'error.log', level: 'error' }),
new winston.transports.File({ filename: 'combined.log' }),
],
});
if (process.env.NODE_ENV !== 'production') {
logger.add(new winston.transports.Console({
format: winston.format.simple(),
}));
}
module.exports = logger;
React Components
- React Auth Higher-Order Component (HOC):
Real-Time Notifications
- NestJS WebSocket Gateway:
- Command:
Create NestJS WebSocket Gateway
- Generated Code:
import { SubscribeMessage, WebSocketGateway, OnGatewayInit, WebSocketServer } from '@nestjs/websockets';
import { Server } from 'socket.io';
@WebSocketGateway()
export class MyGateway implements OnGatewayInit {
@WebSocketServer() server: Server;
afterInit(server: Server) {
console.log('WebSocket server initialized');
}
@SubscribeMessage('message')
handleMessage(client: any, payload: any): string {
return 'Hello world!';
}
}
Theming
- React Native Theme Switching:
- Command:
Create React Native Theme Switching
- Generated Code:
import React, { createContext, useState, useContext } from 'react';
import { View, Button, Text, StyleSheet } from 'react-native';
const ThemeContext = createContext();
const themes = {
light: {
background: 'white',
color: 'black',
},
dark: {
background: 'black',
color: 'white',
},
};
export const ThemeProvider = ({ children }) => {
const [theme, setTheme] = useState(themes.light);
const toggleTheme = () => {
setTheme(theme === themes.light ? themes.dark : themes.light);
};
return (
<ThemeContext.Provider value={{ theme, toggleTheme }}>
{children}
</ThemeContext.Provider>
);
};
export const useTheme = () => useContext(ThemeContext);
const App = () => {
const { theme, toggleTheme } = useTheme();
return (
<View style={[styles.container, { backgroundColor: theme.background }]}>
<Text style={{ color: theme.color }}>Hello, Theme!</Text>
<Button title="Toggle Theme" onPress={toggleTheme} />
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
});
export default App;
Animations
React Native Animations:
- Command:
Create React Native Animations
- Generated Code:
import React, { useRef } from 'react';
import { View, Animated, Button } from 'react-native';
const ReactNativeAnimations = () => {
const fadeAnim = useRef(new Animated.Value(0)).current;
const fadeIn = () => {
Animated.timing(fadeAnim, {
toValue: 1,
duration: 1000,
useNativeDriver: true,
}).start();
};
return (
<View>
<Animated.View style={{ opacity: fadeAnim }}>
<View style={{ width: 100, height: 100, backgroundColor: 'blue' }} />
</Animated.View>
<Button title="Fade In" onPress={fadeIn} />
</View>
);
};
export default ReactNativeAnimations;
Maps
React Native Maps:
- Command:
Create React Native Maps
- Generated Code:
import React from 'react';
import { View, StyleSheet } from 'react-native';
import MapView, { Marker } from 'react-native-maps';
const ReactNativeMaps = () => {
return (
<View style={styles.container}>
<MapView style={styles.map}>
<Marker coordinate={{ latitude: 37.78825, longitude: -122.4324 }} />
</MapView>
</View>
);
};
const styles = StyleSheet.create({
container: {
...StyleSheet.absoluteFillObject,
height: 400,
width: 400,
justifyContent: 'flex-end',
alignItems: 'center',
},
map: {
...StyleSheet.absoluteFillObject,
},
});
export default ReactNativeMaps;
Context API
- React Context API Boilerplate:
- Command:
Create React Context API Boilerplate
- Generated Code:
import React, { createContext, useContext, useReducer } from 'react';
const initialState = {
// initial state
};
const reducer = (state, action) => {
switch (action.type) {
// define case actions
default:
return state;
}
};
const StateContext = createContext();
export const StateProvider = ({ children }) => (
<StateContext.Provider value={useReducer(reducer, initialState)}>
{children}
</StateContext.Provider>
);
export const useStateValue = () => useContext(StateContext);
Email, Phone Number, and Website Link Handlers
Internet Connectivity
- Check Internet Access:
- Command:
Create Check Internet Access
- Generated Code:
import React, { useEffect, useState } from 'react';
import { View, Text, StyleSheet } from 'react-native';
import NetInfo from '@react-native-community/netinfo';
const CheckInternetAccess = ({ children }) => {
const [isConnected, setIsConnected] = useState(true);
useEffect(() => {
const unsubscribe = NetInfo.addEventListener(state => {
setIsConnected(state.isConnected);
});
return () => {
unsubscribe();
};
}, []);
if (!isConnected) {
return (
<View style={styles.container}>
<Text style={styles.text}>No internet connection</Text>
</View>
);
}
return children;
};
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
text: {
fontSize: 18,
color: 'red',
},
});
export default CheckInternetAccess;
Firebase Analytics
Location
Contributing
Feel free to submit issues and enhancement requests.
License
MIT © Shaaz Khan
| |