
Security News
Opengrep Adds Apex Support and New Rule Controls in Latest Updates
The latest Opengrep releases add Apex scanning, precision rule tuning, and performance gains for open source static code analysis.
react-native-user-activity-detection
Advanced tools
A React Native library for detecting user activity and handling app inactivity with native module support for iOS and Android. Perfect for implementing auto-lock functionality, session management, and security features.
A React Native library for detecting user activity and handling app inactivity with native module support for iOS and Android. Perfect for implementing auto-lock functionality, session management, and security features.
.
npm install react-native-user-activity-detection
cd ios && pod install
cd android && ./gradlew build
import React, { useCallback, useMemo } from 'react';
import { View, Text, Alert } from 'react-native';
import { useNativeActivityDetection } from 'react-native-user-activity-detection';
const App = () => {
// Memoize callbacks to prevent unnecessary re-renders
const handleInactivity = useCallback(() => {
Alert.alert('Session Expired', 'Please login again');
// Lock the app or navigate to login screen
}, []);
const handleActivity = useCallback(() => {
console.log('User is active');
}, []);
const handleBackground = useCallback(() => {
console.log('App went to background');
}, []);
const handleForeground = useCallback(() => {
console.log('App came to foreground');
}, []);
// Memoize options object to prevent hook re-initialization
const activityOptions = useMemo(() => ({
inactivityTimeout: 300000, // 5 minutes
backgroundTimeout: 120, // 2 minutes
onInactivity: handleInactivity,
onActivity: handleActivity,
onBackground: handleBackground,
onForeground: handleForeground,
debug: false
}), [handleInactivity, handleActivity, handleBackground, handleForeground]);
const { isModuleAvailable, resetTimer } = useNativeActivityDetection(activityOptions);
return (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<Text>Native Module Available: {isModuleAvailable ? 'Yes' : 'No'}</Text>
<Text>Activity detection is running...</Text>
</View>
);
};
export default App;
For best performance in complex applications, follow these patterns:
import React, { useState, useCallback, useMemo } from 'react';
import { View, Text, TouchableOpacity, Alert } from 'react-native';
import { useNativeActivityDetection } from 'react-native-user-activity-detection';
// Memoize components that don't need frequent updates
const StatusDisplay = React.memo<{
isModuleAvailable: boolean;
isEnabled: boolean;
activityCount: number;
}>(({ isModuleAvailable, isEnabled, activityCount }) => (
<View>
<Text>Module: {isModuleAvailable ? 'ā
' : 'ā'}</Text>
<Text>Enabled: {isEnabled ? 'ā
' : 'ā'}</Text>
<Text>Activity Count: {activityCount}</Text>
</View>
));
const OptimizedApp = () => {
const [isEnabled, setIsEnabled] = useState(true);
const [activityCount, setActivityCount] = useState(0);
// Stable callback references
const handleInactivity = useCallback(() => {
Alert.alert('Inactivity Detected', 'You have been inactive!');
}, []);
const handleActivity = useCallback(() => {
setActivityCount(prev => prev + 1);
}, []);
const toggleEnabled = useCallback(() => {
setIsEnabled(prev => !prev);
}, []);
// Memoized options prevent hook re-initialization
const options = useMemo(() => ({
enabled: isEnabled,
inactivityTimeout: 10000,
backgroundTimeout: 5000,
onInactivity: handleInactivity,
onActivity: handleActivity,
debug: __DEV__
}), [isEnabled, handleInactivity, handleActivity]);
const { isModuleAvailable, resetTimer } = useNativeActivityDetection(options);
return (
<View style={{ flex: 1, padding: 20 }}>
<StatusDisplay
isModuleAvailable={isModuleAvailable}
isEnabled={isEnabled}
activityCount={activityCount}
/>
<TouchableOpacity onPress={toggleEnabled}>
<Text>{isEnabled ? 'Disable' : 'Enable'} Detection</Text>
</TouchableOpacity>
<TouchableOpacity onPress={resetTimer}>
<Text>Reset Timer</Text>
</TouchableOpacity>
</View>
);
};
export default OptimizedApp;
import React, { useCallback, useMemo } from 'react';
import { useSelector, useDispatch } from 'react-redux';
import { useNativeActivityDetection } from 'react-native-user-activity-detection';
import { lockApp, unlockApp, updateActivity } from './store/authSlice';
const AppWithRedux = () => {
const { isAuthenticated, isUnlocked } = useSelector((state) => state.auth);
const dispatch = useDispatch();
// Memoize dispatch callbacks
const handleInactivity = useCallback(() => {
dispatch(lockApp());
}, [dispatch]);
const handleActivity = useCallback(() => {
dispatch(updateActivity());
}, [dispatch]);
// Memoize options to prevent unnecessary hook re-initialization
const options = useMemo(
() => ({
enabled: isAuthenticated && isUnlocked,
inactivityTimeout: 480000, // 8 minutes
backgroundTimeout: 240, // 4 minutes
onInactivity: handleInactivity,
onActivity: handleActivity,
debug: false,
}),
[isAuthenticated, isUnlocked, handleInactivity, handleActivity]
);
useNativeActivityDetection(options);
// Rest of your component...
};
useNativeActivityDetection(options)
Parameter | Type | Default | Description |
---|---|---|---|
inactivityTimeout | number | 480000 | Inactivity timeout in milliseconds |
backgroundTimeout | number | 240 | Background timeout in seconds |
enabled | boolean | true | Whether to enable activity detection |
onInactivity | () => void | undefined | Callback when user becomes inactive |
onActivity | () => void | undefined | Callback when user activity is detected |
onBackground | () => void | undefined | Callback when app goes to background |
onForeground | () => void | undefined | Callback when app comes to foreground |
debug | boolean | false | Enable debug logging |
Property | Type | Description |
---|---|---|
resetTimer | () => void | Reset the inactivity timer |
isModuleAvailable | boolean | Whether the native module is available |
Always wrap your callback functions with useCallback
to prevent unnecessary re-renders:
const handleInactivity = useCallback(() => {
// Your logic here
}, []);
Wrap the options object in useMemo
to prevent hook re-initialization:
const options = useMemo(
() => ({
inactivityTimeout: 300000,
onInactivity: handleInactivity,
// ... other options
}),
[handleInactivity]
);
Memoize components that don't need frequent updates:
const StatusComponent = React.memo(({ status }) => (
<Text>{status}</Text>
));
Don't pass inline functions as callbacks:
// ā Bad - creates new function on every render
useNativeActivityDetection({
onInactivity: () => console.log('inactive'),
});
// ā
Good - stable reference
const handleInactivity = useCallback(() => {
console.log('inactive');
}, []);
useNativeActivityDetection({
onInactivity: handleInactivity,
});
The library uses native modules to detect user interactions at the platform level:
If isModuleAvailable
returns false
:
pod install
./gradlew build
npx react-native start --reset-cache
enabled: true
)useCallback
useMemo
React.memo
for components that don't need frequent updatesThe hook now requires memoized callbacks for optimal performance:
// v1.x - may cause performance issues
useNativeActivityDetection({
onInactivity: () => console.log('inactive'),
});
// v2.x - optimized approach
const handleInactivity = useCallback(() => {
console.log('inactive');
}, []);
const options = useMemo(
() => ({
onInactivity: handleInactivity,
}),
[handleInactivity]
);
useNativeActivityDetection(options);
See the contributing guide to learn how to contribute to the repository and the development workflow.
MIT
Made by ā¤ļø with create-react-native-library
š¤ Hamza Gulraiz
FAQs
A React Native library for detecting user activity and handling app inactivity with native module support for iOS and Android. Perfect for implementing auto-lock functionality, session management, and security features.
We found that react-native-user-activity-detection demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago.Ā It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
The latest Opengrep releases add Apex scanning, precision rule tuning, and performance gains for open source static code analysis.
Security News
npm now supports Trusted Publishing with OIDC, enabling secure package publishing directly from CI/CD workflows without relying on long-lived tokens.
Research
/Security News
A RubyGems malware campaign used 60 malicious packages posing as automation tools to steal credentials from social media and marketing tool users.