Getting Started
React Native Setup
npm install -g react-native-cli
# Create a new React Native project
npx react-native init MyProject
# Or create a project with TypeScript
npx react-native init MyProject --template react-native-template-typescript
# Run on Android
npx react-native run-android
# Run on iOS
npx react-native run-ios
# Using Expo CLI
npm install -g expo-cli
expo init MyProject
expo start
Basic App Structure
import React from 'react';
import { View, Text, StyleSheet } from 'react-native';
const App = () => {
return (
<View style={styles.container}>
<Text style={styles.text}>Hello React Native!</Text>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
text: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
});
export default App;
// index.js - Entry point
import { AppRegistry } from 'react-native';
import App from './App';
import { name as appName } from './app.json';
AppRegistry.registerComponent(appName, () => App);
Core Components
Basic Components
<View
style={{
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#fff'
}}
>
<!-- Content here -->
</View>
// Text - Display text
<Text
style={{
fontSize: 18,
fontWeight: 'bold',
color: '#333'
}}
numberOfLines={1}
>
Hello World!
</Text>
// Image - Display images
<Image
source={{ uri: 'https://example.com/image.jpg' }}
style={{ width: 200, height: 200 }}
resizeMode="cover"
/>
// Local image
<Image
source={require('./assets/image.png')}
style={{ width: 200, height: 200 }}
/>
Interactive Components
<Button
title="Press Me"
onPress={() => alert('Button pressed!')}
color="#841584"
/>
// Touchable Components
<TouchableOpacity
onPress={() => alert('Pressed with opacity effect')}
style={{ padding: 10, backgroundColor: '#ddd' }}
>
<Text>Press Me</Text>
</TouchableOpacity>
<TouchableHighlight
onPress={() => alert('Pressed with highlight effect')}
underlayColor="white"
>
<View style={{ padding: 10 }}>
<Text>Press Me</Text>
</View>
</TouchableHighlight>
// TextInput
<TextInput
style={{ height: 40, borderColor: 'gray', borderWidth: 1 }}
onChangeText={(text) => setText(text)}
value={text}
placeholder="Enter text here"
keyboardType="default"
/>
// Switch
<Switch
value={isEnabled}
onValueChange={setIsEnabled}
trackColor={{ false: '#767577', true: '#81b0ff' }}
/>
List Components
<FlatList
data={[
{ id: '1', title: 'Item 1' },
{ id: '2', title: 'Item 2' },
{ id: '3', title: 'Item 3' },
]}
keyExtractor={(item) => item.id}
renderItem={({ item }) => (
<Text>{item.title}</Text>
)}
/>
// SectionList - for sectioned lists
<SectionList
sections={[
{
title: 'Section 1',
data: ['Item 1', 'Item 2'],
},
{
title: 'Section 2',
data: ['Item 3', 'Item 4'],
},
]}
renderItem={({ item }) => <Text>{item}</Text>}
renderSectionHeader={({ section }) => (
<Text style={{ fontWeight: 'bold' }}>
{section.title}
</Text>
)}
keyExtractor={(item, index) => index.toString()}
/>
// ScrollView - for scrollable content
<ScrollView
style={{ flex: 1 }}
contentContainerStyle={{ padding: 20 }}
showsVerticalScrollIndicator={false}
>
<Text>Long content here...</Text>
</ScrollView>
Styling & Layout
<View style={{
flex: 1,
flexDirection: 'row', // or 'column'
justifyContent: 'space-between', // or 'flex-start', 'center', 'flex-end', 'space-around', 'space-evenly'
alignItems: 'center', // or 'flex-start', 'flex-end', 'stretch', 'baseline'
backgroundColor: '#f0f0f0',
}}>
<View style={{ width: 50, height: 50, backgroundColor: 'red' }} />
<View style={{ width: 50, height: 50, backgroundColor: 'green' }} />
<View style={{ width: 50, height: 50, backgroundColor: 'blue' }} />
</View>
// Using StyleSheet
const styles = StyleSheet.create({
container: {
flex: 1,
padding: 20,
backgroundColor: '#fff',
},
title: {
fontSize: 24,
fontWeight: 'bold',
marginBottom: 20,
color: '#333',
},
button: {
backgroundColor: '#007AFF',
padding: 15,
borderRadius: 5,
alignItems: 'center',
},
buttonText: {
color: '#fff',
fontWeight: 'bold',
},
});
// Platform-specific styles
const styles = StyleSheet.create({
container: {
flex: 1,
...Platform.select({
ios: {
backgroundColor: 'red',
},
android: {
backgroundColor: 'blue',
},
}),
},
});
State Management
useState & useEffect
import React, { useState } from 'react';
import { View, Text, Button } from 'react-native';
const Counter = () => {
const [count, setCount] = useState(0);
return (
<View>
<Text>You clicked {count} times</Text>
<Button
title="Click me"
onPress={() => setCount(count + 1)}
/>
</View>
);
};
// useEffect hook
import React, { useState, useEffect } from 'react';
const Example = () => {
const [data, setData] = useState([]);
const [loading, setLoading] = useState(true);
// Similar to componentDidMount and componentDidUpdate
useEffect(() => {
// Update the document title using the browser API
document.title = `You clicked ${count} times`;
});
// Effect with cleanup (similar to componentWillUnmount)
useEffect(() => {
const subscription = API.subscribe();
// Cleanup function
return () => {
subscription.unsubscribe();
};
}, []);
// Fetch data on component mount
useEffect(() => {
const fetchData = async () => {
try {
const response = await fetch('https://api.example.com/data');
const json = await response.json();
setData(json);
setLoading(false);
} catch (error) {
console.error(error);
setLoading(false);
}
};
fetchData();
}, []); // Empty array means this effect runs once on mount
if (loading) {
return <Text>Loading...</Text>;
}
return (
<View>
{data.map(item => (
<Text key={item.id}>{item.name}</Text>
))}
</View>
);
};
Context API & Redux
import React, { createContext, useContext, useState } from 'react';
// Create a Context
const AuthContext = createContext();
// Create a provider component
export const AuthProvider = ({ children }) => {
const [user, setUser] = useState(null);
const login = (userData) => {
setUser(userData);
};
const logout = () => {
setUser(null);
};
return (
<AuthContext.Provider value={{ user, login, logout }}>
{children}
</AuthContext.Provider>
);
};
// Custom hook to use the auth context
export const useAuth = () => {
const context = useContext(AuthContext);
if (!context) {
throw new Error('useAuth must be used within an AuthProvider');
}
return context;
};
// Using the context in a component
const UserProfile = () => {
const { user, logout } = useAuth();
return (
<View>
<Text>Welcome {user?.name}</Text>
<Button title="Logout" onPress={logout} />
</View>
);
};
// Redux Toolkit setup
import { configureStore, createSlice } from '@reduxjs/toolkit';
// Create a slice
const counterSlice = createSlice({
name: 'counter',
initialState: {
value: 0,
},
reducers: {
increment: (state) => {
state.value += 1;
},
decrement: (state) => {
state.value -= 1;
},
incrementByAmount: (state, action) => {
state.value += action.payload;
},
},
});
// Export actions
export const { increment, decrement, incrementByAmount } = counterSlice.actions;
// Create store
const store = configureStore({
reducer: {
counter: counterSlice.reducer,
},
});
// Using Redux in components with React-Redux hooks
import { useSelector, useDispatch } from 'react-redux';
const Counter = () => {
const count = useSelector((state) => state.counter.value);
const dispatch = useDispatch();
return (
<View>
<Text>Count: {count}</Text>
<Button title="Increment" onPress={() => dispatch(increment())} />
<Button title="Decrement" onPress={() => dispatch(decrement())} />
</View>
);
};
Comprehensive React Native Developer Cheatsheet Reference
This React Native Developer cheatsheet on Nikhil Learn Hub collects syntax, commands, and practical snippets for quick revision. Learn React Native components, navigation, hooks, styling, and mobile app development concepts with practical examples.
Use the reference cards and examples above during coding sessions; return here instead of scattered searches when you need dependable reminders. Follow the React Native learning roadmap when you want structured lessons beyond one-page lookups.
Quick lookup coverage
- Syntax, commands, and API signatures
- Copy-ready examples and common patterns
- Terminology for coursework and interviews
- Cross-links to the matching learning roadmap
How to study with this sheet
- Production debugging and tuning reminders
- Security, performance, or scale cautions
- Integration with adjacent stacks on this site
- Deeper study through tutorials and roadmaps
Who Should Use This Cheatsheet
Students, self-taught developers, and professionals who need fast React Native Developer lookups during labs, debugging, or interview revision should keep this page bookmarked.
Related Resources on Nikhil Learn Hub
- React Native learning roadmapstructured learning path for the same technology
- Cheatsheets hubbrowse all quick-reference sheets
- Technology hubtutorials, roadmaps, and practice hubs