rn-restart
v1.0.1
Published
π Restart your React Native app programmatically. Production-ready Turbo Module with zero dependencies. Perfect for language changes, theme switching, OTA updates, and more. Works on iOS & Android.
Maintainers
Readme
Why RN-Restart?
Sometimes your React Native app needs a fresh start. Whether you're applying language changes, switching themes, implementing OTA updates, or recovering from errors β RN-Restart provides a simple, reliable way to restart your app programmatically.
Built with modern React Native architecture (Turbo Modules) for optimal performance and future compatibility.
β¨ Features
π Table of Contents
- Installation
- Compatibility
- Usage
- API Reference
- Platform Differences
- Use Cases
- Troubleshooting
- Contributing
- License
π¦ Installation
Step 1: Install the package
npm install rn-restartor with yarn:
yarn add rn-restartStep 2: Platform setup
iOS
cd ios && pod installAndroid
β
Auto-linked! No additional setup needed.
π§ Compatibility
| Platform | Minimum Version | Status | |:--------:|:---------------:|:------:| | React Native | 0.70+ | β | | iOS | 13.0+ | β | | Android | API 21+ | β | | Node.js | 18+ | β |
π Quick Start
Basic Usage
import { restart, restartWithCacheClear } from 'rn-restart';
// Simple restart
restart();
// Restart with cache clearing (great for Android)
restartWithCacheClear();That's it! Just call the function when you need to restart. β‘
import React from 'react';
import { View, Button, Alert } from 'react-native';
import { restart } from 'rn-restart';
export default function SettingsScreen() {
const handleRestart = () => {
Alert.alert(
'Restart App',
'Are you sure you want to restart?',
[
{ text: 'Cancel', style: 'cancel' },
{ text: 'Restart', onPress: () => restart() }
]
);
};
return (
<View>
<Button title="Restart App" onPress={handleRestart} />
</View>
);
}πΌ Real-World Use Cases
π 1. Language/Locale Change
import { restart } from 'rn-restart';
import AsyncStorage from '@react-native-async-storage/async-storage';
import i18n from 'i18next';
const changeLanguage = async (languageCode: string) => {
try {
// Save the new language preference
await AsyncStorage.setItem('userLanguage', languageCode);
// Update i18n
await i18n.changeLanguage(languageCode);
// Restart to apply changes throughout the app
restart();
} catch (error) {
console.error('Failed to change language:', error);
}
};π¨ 2. Theme Switching
import { restart } from 'rn-restart';
import { Alert } from 'react-native';
const switchTheme = async (theme: 'light' | 'dark') => {
Alert.alert(
'Restart Required',
'The app needs to restart to apply the new theme. Continue?',
[
{ text: 'Cancel', style: 'cancel' },
{
text: 'Restart',
onPress: async () => {
await AsyncStorage.setItem('theme', theme);
restart();
},
},
]
);
};π¦ 3. After OTA Updates
import { restartWithCacheClear } from 'rn-restart';
import codePush from 'react-native-code-push';
const checkForUpdates = async () => {
const update = await codePush.checkForUpdate();
if (update) {
await update.download();
await update.install(codePush.InstallMode.IMMEDIATE);
// Restart with cache clear to ensure clean update
restartWithCacheClear();
}
};π 4. User Logout
import { restartWithCacheClear } from 'rn-restart';
const logout = async () => {
try {
// Clear user data
await AsyncStorage.clear();
// Clear any cached tokens
await clearAuthTokens();
// Restart with cache clear for complete cleanup
restartWithCacheClear();
} catch (error) {
console.error('Logout failed:', error);
}
};π 5. Error Recovery
import { restart } from 'rn-restart';
import { ErrorBoundary } from 'react-error-boundary';
const ErrorFallback = ({ error, resetErrorBoundary }) => (
<View style={styles.container}>
<Text style={styles.title}>Something went wrong</Text>
<Text style={styles.error}>{error.message}</Text>
<Button
title="Restart App"
onPress={() => {
resetErrorBoundary();
restart();
}}
/>
</View>
);
const App = () => (
<ErrorBoundary FallbackComponent={ErrorFallback}>
<YourApp />
</ErrorBoundary>
);π API Reference
restart()
Restarts the React Native application by reloading the JavaScript bundle.
Platform Support: iOS, Android
Returns: void
Example:
import { restart } from 'rn-restart';
restart();What it does:
- iOS: Triggers RCTReloadCommand to reload the React Native bridge
- Android: Restarts the main activity with cleared task flags
restartWithCacheClear()
Restarts the React Native application after clearing cached data.
Platform Support: iOS (network cache only), Android (full app cache)
Returns: void
Example:
import { restartWithCacheClear } from 'rn-restart';
restartWithCacheClear();What it does:
- iOS: Clears NSURLCache and reloads the React Native bridge
- Android: Recursively deletes the app's cache directory, then restarts
π Platform Differences
β οΈ Important Notes
β Perfect For
Language changes β’ Theme switching β’ OTA updates β’ User logout β’ Error recovery β’ Config updates
β Not For
Navigation resets β’ State resets β’ Frequent operations β’ Background tasks
π Troubleshooting
App doesn't restart on iOS
Solution: Ensure your app is using the new architecture with Turbo Modules enabled.
# ios/Podfile
use_react_native!(
:path => config[:reactNativePath],
:hermes_enabled => true,
:fabric_enabled => true # Enable new architecture
)Android app crashes on restart
Solution: Make sure your MainActivity is properly configured:
// MainActivity.kt
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(null) // Pass null to prevent state restoration
}Cache not clearing on Android
Solution: Verify your app has proper permissions. The module uses the app's cache directory which doesn't require special permissions, but ensure you're not blocking file operations.
TypeScript types not found
Solution: Ensure your TypeScript configuration includes the module:
{
"compilerOptions": {
"moduleResolution": "node",
"resolveJsonModule": true
}
}π¨ Example App
This repository includes a beautiful example app showcasing all features:
git clone https://github.com/abdallaemadeldin/RN-Restart.git
cd RN-Restart
yarn
yarn example ios # or: yarn example androidWhat's included:
- β Standard restart with confirmation
- β Restart with cache clearing
- β Delayed restart with countdown
- β Platform-specific behavior demos
- β Real-world use case examples
- β Modern, beautiful UI
π€ Contributing
Contributions are welcome! Please read our Contributing Guide for details.
π Found a Bug?
π‘ Have an Idea?
π¬ Questions?
π License
MIT Β© Abdalla Emad Eldin
β Star this repo if you find it useful!
π More Resources
API Documentation β’ Changelog β’ Quick Start Guide
Built with β€οΈ for the React Native community
Made with create-react-native-library
