我是本機反應的新手。我有一個倒數計時器(當前設置為 5 秒),當時間到了時,我想要一個警報,讓用戶可以選擇重新啟動計時器。屏幕中會有其他元素,所以我不一定想要“刷新”(例如,如果用戶輸入了一些文本,我不希望它在計時器延長時消失)。時間到了時會彈出警報,但我不確定如何通過警報按鈕重置計時器。import React, { Component } from 'react';import { StyleSheet, TouchableOpacity, Text, View, TextInput, Alert,} from 'react-native'import CountDown from 'react-native-countdown-component';export default class Payment extends Component { constructor(props) { super(props); this.state = { timer: 5, }; } timesUp = () => { Alert.alert( "Time's Up!", "How can we help you?", [ { text: "Extend Timer", // I want to restart the timer here onPress: this.setState(newTime => ({ timer: 5})) }, { text: "Cancel", onPress: () => console.log("Cancel Pressed") }, ], { cancelable: false } ); } render() { return ( <View> <View style={{ marginTop: 20}}> <CountDown size={15} until={this.state.timer} onFinish={this.timesUp} digitStyle={{backgroundColor: '#FFF', borderWidth: 2, borderColor: '#cccccc'}} digitTxtStyle={{color: 'black'}} separatorStyle={{color: 'black'}} timeToShow={['M', 'S']} timeLabels={{m: null, s: null}} showSeparator /> </View> ); }}
React Native:如何通過警報擴展此計時器?
神不在的星期二
2021-10-21 13:32:14