DEV Community

Cover image for Efficiently Managing Timers in a React Native App: Overcoming Background-Foreground Timer State Issues
Shivam Pawar
Shivam Pawar

Posted on

Efficiently Managing Timers in a React Native App: Overcoming Background-Foreground Timer State Issues


Developing a React Native app often involves the use of timers, particularly setInterval, to trigger certain functions at regular intervals. While using timers might seem straightforward, managing them efficiently can become challenging, especially when dealing with background and foreground transitions in the app. In many mobile applications, especially productivity and task-oriented apps, session timers play a crucial role in managing user sessions effectively. Additionally, handling app state changes, such as going to the background or returning to the foreground, is essential for providing a seamless user experience.
This article aims to explore the common difficulties developers face when managing timers in React Native and provide solutions for effectively handling the timer state during background and foreground transitions. We will also dive into the implementation of the TimeTriggeredScheduler component, which handles session timers, background events, and more, using popular libraries such as Redux and AsyncStorage.

The Challenges

I faced the challenge of managing timers and ensuring they functioned correctly when the app transitioned between background and foreground states. Additionally, I needed a robust solution to clear timers from any screen to prevent memory leaks and optimize app performance.

  1. Timer State Loss during Background Transition:
    When a React Native app goes into the background, the JavaScript thread may be paused or even terminated by the operating system to save resources. As a result, any active timers, including setInterval, are lost. When the app returns to the foreground, the timer state must be restored appropriately to ensure the smooth functioning of the app.

  2. Inaccurate Timer Execution:
    React Native app developers might encounter situations where the setInterval timer execution becomes inconsistent or inaccurate, leading to unpredictable behaviour. This issue can arise due to various factors, including app lifecycle events, garbage collection, and asynchronous tasks.

  3. Manage timers from any screen of the app:
    Timers are commonly used to trigger specific actions, track session durations, or schedule periodic updates. However, ensuring timers are properly cleared and managed when navigating between screens can be challenging.

Efficient Timer Management Solutions

  1. Utilize React Native AppState API: The AppState API in React Native provides information about the current state of the app, whether it is in the active, inactive, background, or foreground state. By subscribing to the AppState changes, you can implement logic to handle different app behaviours based on these state transitions. To start using the AppState API, you need to import it from the react-native library. The AppState module exposes three states: active, background, and inactive. By registering a listener with the AppState module, you can receive updates whenever the app transitions between these states.
import { useEffect, useRef } from "react";
import { AppState } from "react-native";
import moment from "moment";

function TimeTriggeredScheduler({ children }) {

  // Ref to store the current app state
  const appState = useRef(AppState.currentState);

  useEffect(() => {
    // Load the timer when the component mounts and register the event listener for app state changes

  const subscription = AppState.addEventListener("change", (nextAppState) => {
      if (
        appState.current.match(/inactive|background/) &&
        nextAppState === "active"
      ) {
        // Load the timer when the app returns to the foreground
      else if (nextAppState === "background" || nextAppState === "inactive") {
        // Save the timer value when the app goes to the background

      // Update the current app state
      appState.current = nextAppState;

    // Clean up the event listener on component unmount
    return () => {
  }, []);

  // Function to save the timer value to AsyncStorage when the app goes to the background

  const saveTimerValue = async () => {
    // Code to save the timer value to AsyncStorage

  // Function to load the timer value from AsyncStorage when the app returns to the foreground

  const loadTimer = async () => {
    // Code to load the timer value from AsyncStorage

  return (
    <View style={{ flex: 1, justifyContent: "center", alignItems: "center" }}>
      <Text>Current App State: {appState}</Text>

export default TimeTriggeredScheduler;
Enter fullscreen mode Exit fullscreen mode
  1. Leveraging Redux to Manage Timers As the app's complexity grows, managing multiple timers across different screens becomes cumbersome. Having a centralized mechanism to clear timers from anywhere in the app is crucial to avoid memory leaks and ensure accurate time management. When the timer starts, we save the IntervalId of setInterval in the Redux store using the redux action. This allows us to access and clear the interval from anywhere in the app by retrieving the 'IntervalId' from the Redux store. Here is the code for your reference, you can change as per your requirement. The idea is only here to store IntervalId in store and fetch anywhere we want and finally clear the interval based on fetched IntervalId.
// Redux Action

// Redux Reducer
const initialState = {
  currentIntervalId: null,

function timerReducer(state = initialState, action) {
  switch (action.type) {
      return { ...state, currentIntervalId: action.payload };
      return state;

// Action Creator to set the currentIntervalId in Redux
const setCurrentIntervalID = (intervalId) => ({
  payload: intervalId,

// Timer Component
import { useDispatch, useSelector } from "react-redux";

function TimerComponent() {
  const dispatch = useDispatch();
  const currentIntervalId = useSelector(
    (state) => state.timer.currentIntervalId

  useEffect(() => {
    const intervalId = setInterval(() => {
      // Timer logic here
      console.log("Timer tick...");
    }, 1000);

    // Store the Interval ID in Redux

    // Clear the Interval when the component unmounts
    return () => {
      // Clear the Interval ID from Redux
  }, []);

  // Other component logic here
  return <div>Timer Component</div>;
Enter fullscreen mode Exit fullscreen mode


Efficiently managing setInterval in a React Native app requires careful consideration of background and foreground transitions. By using the AppState API and third-party background timer libraries, developers can ensure their timers function reliably and consistently. Understanding these techniques will lead to a smoother and more responsive user experience in React Native apps that involve timers. Remember to test your timer implementations thoroughly to account for various scenarios and edge cases that might arise during real-world usage.

Please do share your feedback and experiences with handling Timers in a React Native App. I’d love to see what you come up with!

If you found this article useful, please share it with your friends and colleagues ❤️

Read more articles on:
Dev.To ➡️ Shivam Pawar
Follow me on ⤵️
🌐 LinkedIn
🌐 Github

Happy coding!

Top comments (0)