6

A simple react native timer hooks

 2 years ago
source link: https://reactnativeexample.com/a-simple-react-native-timer-hooks/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Install

npm install react-native-timer-hooks

Example


Import

import { useClock } from 'react-native-timer-hooks';

Usage

import React from "react"; import { StyleSheet, View, Text, Button } from 'react-native'; import { useClock } from 'react-native-timer-hooks';

const Example = () => { const [counter, start, pause, reset, isRunning] = useClock(0, 1000, false);

return ( <View style={styles.container}> <Text>Seconds: {counter}</Text> <Button onPress={() => { isRunning ? pause() : start(); }} title={isRunning ? 'Pause' : 'Start'} /> <Button onPress={() => reset()} title={'reset'} /> </View> ); };

The useClock hooks has the following parameters:

Name Type Explanation Required Default Value from number The initial value of counter. ✅ undefined ms number The number of millisecond between.


1000ms for 1 second. ❌ 1000 down boolean Counter value should decrease or increase. ❌ false

The useClock hooks has the following returns values:

Name Type Explanation Parameters counter number The initial value of counter.

start function Start the counter.

pause function Pause the counter.

reset function Reset the counter. There is an optional parameter to set the from value from:number = the restart value of the counter. isRunning boolean Return true if the counter is running, false if not.


License

GitHub

https://github.com/samuel3105/react-native-timer-hooks


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK