Socket
Book a DemoInstallSign in
Socket

react-native-user-activity-detection

Package Overview
Dependencies
Maintainers
0
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-native-user-activity-detection

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.

1.0.7
latest
Source
npmnpm
Version published
Maintainers
0
Created
Source

react-native-user-activity-detection

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.

Features

  • šŸš€ Native Performance: Uses native modules for efficient activity detection
  • šŸ“± Cross-Platform: Supports both iOS and Android
  • ⚔ TypeScript Support: Fully typed with TypeScript
  • šŸŽÆ Customizable: Configurable timeouts and callbacks
  • šŸ”’ Security Focused: Ideal for apps requiring auto-lock functionality
  • šŸ“Š Background Detection: Handles app background/foreground transitions
  • šŸŽ® Touch & Scroll Detection: Detects various user interactions
  • šŸ”„ Optimized Performance: Built-in render optimization to prevent excessive re-renders

demo. demo

Installation

npm install react-native-user-activity-detection

iOS Setup

  • Run pod install:
cd ios && pod install

Android Setup

  • Run ./gradlew build:
cd android && ./gradlew build

Usage

Basic Usage

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;

Performance-Optimized Usage

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;

Advanced Usage with Redux

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...
};

API Reference

useNativeActivityDetection(options)

Options

ParameterTypeDefaultDescription
inactivityTimeoutnumber480000Inactivity timeout in milliseconds
backgroundTimeoutnumber240Background timeout in seconds
enabledbooleantrueWhether to enable activity detection
onInactivity() => voidundefinedCallback when user becomes inactive
onActivity() => voidundefinedCallback when user activity is detected
onBackground() => voidundefinedCallback when app goes to background
onForeground() => voidundefinedCallback when app comes to foreground
debugbooleanfalseEnable debug logging

Returns

PropertyTypeDescription
resetTimer() => voidReset the inactivity timer
isModuleAvailablebooleanWhether the native module is available

Performance Best Practices

1. Memoize Callback Functions

Always wrap your callback functions with useCallback to prevent unnecessary re-renders:

const handleInactivity = useCallback(() => {
  // Your logic here
}, []);

2. Memoize Options Object

Wrap the options object in useMemo to prevent hook re-initialization:

const options = useMemo(
  () => ({
    inactivityTimeout: 300000,
    onInactivity: handleInactivity,
    // ... other options
  }),
  [handleInactivity]
);

3. Use React.memo for Components

Memoize components that don't need frequent updates:

const StatusComponent = React.memo(({ status }) => (
  <Text>{status}</Text>
));

4. Avoid Inline Functions

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,
});

How It Works

Native Module Integration

The library uses native modules to detect user interactions at the platform level:

  • iOS: Intercepts touch events and system notifications
  • Android: Uses touch listeners and activity lifecycle callbacks

Activity Detection

  • Touch Events: Detects taps, swipes, and other touch interactions
  • Scroll Events: Monitors scrolling activity in views
  • App State Changes: Tracks when the app goes to background/foreground
  • System Events: Listens to relevant system-level activity indicators

Timer Management

  • Maintains an inactivity timer that resets on user activity
  • Separate handling for background time tracking
  • Automatic cleanup of timers and listeners
  • Optimized to prevent memory leaks and excessive re-renders

Performance Optimizations

  • Stable References: All internal callbacks maintain stable references
  • Single Initialization: Hook initializes only once, preventing redundant setups
  • Efficient Cleanup: Proper cleanup of all subscriptions and timers
  • Memoized Returns: Return values are memoized to prevent unnecessary re-renders

Troubleshooting

Native Module Not Available

If isModuleAvailable returns false:

  • iOS: Ensure you've run pod install
  • Android: Ensure you've run ./gradlew build
  • Metro: Try clearing Metro cache: npx react-native start --reset-cache
  • Clean Build: Try cleaning and rebuilding your project

Activity Not Detected

  • Ensure the hook is enabled (enabled: true)
  • Check that your app has proper touch targets
  • Verify native module setup
  • Enable debug mode to see activity logs

Performance Issues / Excessive Re-renders

  • Ensure callbacks are wrapped with useCallback
  • Memoize the options object with useMemo
  • Use React.memo for components that don't need frequent updates
  • Avoid passing inline functions as callbacks

Background Detection Issues

  • iOS: Check app background execution permissions
  • Android: Verify activity lifecycle handling
  • Test background timeout values are appropriate for your use case

Migration Guide

From v1.x to v2.x

The 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);

Contributing

See the contributing guide to learn how to contribute to the repository and the development workflow.

License

MIT

Made by ā¤ļø with create-react-native-library

Author

šŸ‘¤ Hamza Gulraiz

šŸ“¬ Contact Me

Email LinkedIn GitHub StackOverflow npm Instagram Facebook

Keywords

react-native

FAQs

Package last updated on 02 Aug 2025

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with āš”ļø by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.