Zum Hauptinhalt springen
tsecurity.de LIVE
Echtzeit-Radar & Feeds
Alle RSS Feeds
👥 Community & Social
YouTube Security VideosAnonymous Official: I'm begging you to understand this..(20.09.2026 um 21:30 Uhr)
Sichere ProgrammierungHow to Monitor Cron Jobs with a Simple HTTP Health Check(20.09.2026 um 23:14 Uhr)
Sichere ProgrammierungWhy my builds don't run on my laptop(20.09.2026 um 23:15 Uhr)
Sichere ProgrammierungDesigning offline-first when there's no server(20.09.2026 um 23:16 Uhr)
Sichere ProgrammierungSearching for Better Game Recommendations with Jev(20.09.2026 um 23:19 Uhr)
Linux Tipps & HardeningUbuntu 26.10 stops low memory from killing your desktop session(20.09.2026 um 19:55 Uhr)
YouTube Security VideosAnonymous Official: I'm begging you to understand this..(20.09.2026 um 21:30 Uhr)
Sichere ProgrammierungHow to Monitor Cron Jobs with a Simple HTTP Health Check(20.09.2026 um 23:14 Uhr)
Sichere ProgrammierungWhy my builds don't run on my laptop(20.09.2026 um 23:15 Uhr)
Sichere ProgrammierungDesigning offline-first when there's no server(20.09.2026 um 23:16 Uhr)
Sichere ProgrammierungSearching for Better Game Recommendations with Jev(20.09.2026 um 23:19 Uhr)
Linux Tipps & HardeningUbuntu 26.10 stops low memory from killing your desktop session(20.09.2026 um 19:55 Uhr)
Intelligence View
⚡ tsecurity.de Intelligence

OTP Timer in React Native | No External Package

Reagiere als Erste:r — dein Feedback zählt!

Almost every application will have a sign up page which may or may not perform email or phone number verification done by using an OTP (One Time Password). I had to make one for my startup. Even though it is obvious that there will be some external package or module to make this, but I decided to do it on my own.

Here in this article I will show step by step how to implement such a timer in react.

1. Setting up the Project:

This project will be managed using the Expo CLI.

npx create-expo-app OTPApp --template blank

2. Creating the Timer Component

Create a new file OTPTimer.js for your OTP timer component. Import useState, useRef, useEffect from react. Import other necessary basic components like Text, View, etc.

We are also going to use a prop timeInSeconds to decide how long the countdown should run.

import { useRef, useState,useEffect } from "react";
import { Text } from "react-native";

const TimerComponent=({timeInSeconds})=>{  

 return (
<Text style={{color:'grey', margin:10, textAlign:'center'}}></Text>
)

}

3. Create State

We create a state called time which would be initiated with the prop timeInSeconds. It will also be used under the Text Component.

const TimerComponent=({timeInSeconds})=>{  

 const [time,setTime]=useState(timeInSeconds) 

 return (
<Text style={{color:'grey', margin:10, textAlign:'center'}}>{time}</Text>
)

}

4. Logic for Countdown

The logic for countdown is that every 1 second we will decrement the state time which will then be rendered on the component. Now to perform this task we will take advantage of the setInterval function. We set an interval 1000 ms, after which we will perform the decrement.

Once the value of the time state reaches <=0 then we will call clearInterval(IntervalID) to remove the setInterval function.

 function countDown(){
        setTime(prev=> prev-1)

        console.log(time)
        if(time<=0){
            clearInterval(IntervalID)
        }
    }

    const IntervalID=setInterval(countDown,1000)

But this is not going to be enough. In pure javascript this would have worked but not in react. The reason being that time value inside the countDown function is stale, meaning it's not using the most up-to-date state value due to how closures and setInterval work in React.

In the countDown function, the time variable is referencing the value it had when setInterval was initially set. Even though setTime updates the state, the next setInterval call doesn't have access to the updated state value.

Hence we take the help of useRef and useEffect.

5. useRef and useEffect

    const intervalRef=useRef(null)

    useEffect(() => {
        intervalRef.current = setInterval(() => {
            setTimer(prev => {
                if (prev <= 1) {
                    clearInterval(intervalRef.current);
                    return 0;
                }
                return prev - 1;
            });
        }, 1000);

        // Cleanup interval on component unmount
        return () => {
            clearInterval(intervalRef.current);
        }
    }, []);

We use useRef to store the interval ID so that it's preserved across renders and can be cleared when needed.

We also clear the interval when time reaches 0 or if the component unmounts.

6. Printing format

I used the below function so that the timer is shown in MM:SS format which is more readable.

    function convertSecondsToMMSS(seconds) {
        const minutes = Math.floor(seconds / 60);
        const remainingSeconds = seconds % 60;

        const formattedMinutes = minutes.toString().padStart(2, '0');
        const formattedSeconds = remainingSeconds.toString().padStart(2, '0');

        return `${formattedMinutes}:${formattedSeconds}`;
    }

7. Complete component code

Here is the complete code for your reference.

import { useRef, useState,useEffect } from "react";
import { Text } from "react-native";

const TimerComponent=({timeInSeconds,expireCallback})=>{  //expireCallback is a function called when timer ends

    const [timer,setTimer]=useState(timeInSeconds)

    const intervalRef=useRef(null)

    useEffect(() => {
        intervalRef.current = setInterval(() => {
            setTimer(prev => {
                if (prev <= 1) {
                    clearInterval(intervalRef.current);
                    return 0;
                }
                return prev - 1;
            });
        }, 1000);

        // Cleanup interval on component unmount
        return () => {
            clearInterval(intervalRef.current);
            if (typeof expireCallback === 'function') {
                expireCallback();
            } else {
                console.log('Callback is undefined or not a function');
            }
        }
    }, []);


    function convertSecondsToMMSS(seconds) {
        const minutes = Math.floor(seconds / 60);
        const remainingSeconds = seconds % 60;

        const formattedMinutes = minutes.toString().padStart(2, '0');
        const formattedSeconds = remainingSeconds.toString().padStart(2, '0');

        return `${formattedMinutes}:${formattedSeconds}`;
    }


    return(
        <Text style={{color:'grey', margin:10, textAlign:'center'}}>{convertSecondsToMMSS(timer)}</Text>
    )

}

export default TimerComponent
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten OTP Timer in React Native | No External Package

Thematisch verwandte Begriffe: Timer, React, Native, External · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Zum Aktualisieren ziehen
ZERO-DAY CVE-2026-93956 | A flaw has been found in olivier-ls PHP-FTS up to 1.1.2. Affected by thi…
Advisory →
TTS Reader • tsecurity.de Voice
tsecurity.de Icon
tsecurity.de App
Offline-Lesen, Eilmeldungen & 0ms Ladezeit

Installiere tsecurity.de direkt auf deinen Home-Bildschirm für das ultimative Vollbild-Magazinerlebnis ohne Browser-Leisten.

Nächster Beitrag
Themen-Radar & Intelligence Matrix
Echtzeit-Taxonomie nach Angriffsvektoren & Plattformen

tsecurity.de Live Threat Radar

🔴 LIVE RADAR
MONITORING
AKTIV
CVE-DATENBANK
LIVE
🔍
Community Radar & Live Chat
Sentinel Bot online • Live-Stream
Dein Cluster: Security Explorer
Match:
lädt…
Verbindung zum Community-Stream wird aufgebaut...
Bearbeitungsmodus — Senden überschreibt deine Nachricht
Community-Puls — was gerade passiert
lädt…
Aktivitäten deiner Analysten
lädt…
Neues Thema oder Eilmeldung einreichen

Reiche interessante Links, Zero-Days oder Debatten ein. Die Community entscheidet per Upvote über die Veröffentlichung.

Heiß diskutierte Einreichungen
🔖 Gespeicherte Artikel
📂 Keine gespeicherten Artikel vorhanden.
Zurück Ziehen Vor
Links: vorheriger Artikel Rechts: nächster Artikel unten: schließen
News NIS-2 Frühwarnung Tier-1 Intel ⏱️ 3 Min vor 10 Min
Artikeldaten werden geladen...

Zurück: vorheriger Vor: nächster
↗ Original-Quelle
Social Reaktionen Deine Reaktion zählt
Einstufung & Relevanz-Poll 0 Stimmen
In sozialen Netzwerken teilen 1-Klick