简体   繁体   English

如何使用 React-Native 和 Moment.js 创建倒计时组件?

[英]How to create countdown component with React-Native and Moment.js?

I am creating simple countdown component with React-Native, for date formatting I am using moment.js , I am getting data from API call in seconds format, then providing to child component, I want to transform this kind of data 121 into 2:01 , How to use moment to achieve this result and do not kill performance?我正在使用 React-Native 创建简单的倒计时组件,用于日期格式我正在使用moment.js ,我从 API 以秒格式调用数据,然后提供给子组件,我想将这种数据121转换为2:01 ,如何使用时刻来达到这个结果并且不破坏性能?

 import { MaterialCommunityIcons } from "@expo/vector-icons"; import moment from "moment"; import React, { useEffect, useState} from "react"; import { View, Text} from "react-native"; const Timer: React.FC<{ expirationDate: number,focus:boolean, loading:boolean}> = ({ expirationDate, focus, loading}) => { const [inactive, setIncative] = useState(false); const [timerCount, setTimer] = useState<number>(0) //can be any number provided from props useEffect(() => { setTimer((prev:number)=> (expirationDate)) let interval = setInterval(() => { setTimer(lastTimerCount => { lastTimerCount <= 1 && clearInterval(interval) return lastTimerCount - 1 }) }, 1000) return () => clearInterval(interval) }, [loading]); return ( <View style={{ backgroundColor: expirationDate < 300 || inactive? "#edbcbc": "#cee5f4", width: 70, borderRadius: 7, paddingVertical: 6, flexDirection: "row", alignItems: "center", justifyContent: "center", position: "relative", }} > <View style={{ position: "relative", left: 4 }}> <MaterialCommunityIcons name="timer-outline" size={13} color={expirationDate < 300 || inactive? "#f54c4c": "#004978"} /> </View> //line sholud be changed <Text>{moment.duration(timerCount).format("h:mm")}</Text> </View> ); }; export default Timer;

I found solotion: using moment utc method and transform state value into miliseconds我发现了独奏:使用moment utc method并将 state 值转换为毫秒

 import { MaterialCommunityIcons } from "@expo/vector-icons"; import moment from "moment"; import React, { useEffect, useState} from "react"; import { View, Text} from "react-native"; const Timer: React.FC<{ expirationDate: number,focus:boolean, loading:boolean}> = ({ expirationDate, focus, loading}) => { const [inactive, setIncative] = useState(false); const [timerCount, setTimer] = useState<number>(0) useEffect(() => { setTimer((prev:number)=> (expirationDate)) let interval = setInterval(() => { setTimer(lastTimerCount => { lastTimerCount <= 1 && clearInterval(interval) return lastTimerCount - 1 }) }, 1000) return () => clearInterval(interval) }, [loading]); return ( <View style={{ backgroundColor: expirationDate < 300 || inactive? "#edbcbc": "#cee5f4", width: 70, borderRadius: 7, paddingVertical: 6, flexDirection: "row", alignItems: "center", justifyContent: "center", position: "relative", }} > <View style={{ position: "relative", left: 4 }}> <MaterialCommunityIcons name="timer-outline" size={13} color={expirationDate < 300 || inactive? "#f54c4c": "#004978"} /> </View> <Text>{moment.utc(timerCount * 1000).format("mm:ss")}</Text> </View> ); }; export default Timer;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM