Скорее вопрос как реализован.
Если через юзеколбек со сменой переменной после каждой секунды должно менять текст
import React from 'react';
import { AppState, StyleSheet, Text, View } from 'react-native';
import { sprintf } from 'sprintf-js';
const DEFAULT_DIGIT_STYLE = { backgroundColor: '#FAB913' };
const DEFAULT_DIGIT_TXT_STYLE = { color: '#000' };
const DEFAULT_TIME_LABEL_STYLE = { color: '#000' };
const DEFAULT_SEPARATOR_STYLE = { color: '#000' };
const DEFAULT_TIME_TO_SHOW = ['D', 'H', 'M', 'S'];
const DEFAULT_TIME_LABELS = {
d: 'Days',
h: 'Hours',
m: 'Minutes',
s: 'Seconds',
};
class CountDown extends React.Component {
state = {
until: Math.max(this.props.until, 0),
lastUntil: null,
wentBackgroundAt: null,
subscription: null,
};
constructor(props) {
super(props);
this.timer = setInterval(this.updateTimer, 1000);
}
componentDidMount() {
const subscription = AppState.addEventListener('change', this._handleAppStateChange);
this.setState({
subscription,
});
}
componentWillUnmount() {
clearInterval(this.timer);
if (this.state.subscription) {
this.state.subscription.remove();
}
}
componentDidUpdate(prevProps, prevState) {
if (this.props.until !== prevProps.until || this.props.id !== prevProps.id) {
this.setState({
lastUntil: prevState.until,
until: Math.max(prevProps.until, 0),
});
}
}
_handleAppStateChange = (currentAppState) => {
const { until, wentBackgroundAt } = this.state;
if (currentAppState === 'active' && wentBackgroundAt && this.props.running) {
const diff = (Date.now() - wentBackgroundAt) / 1000.0;
this.setState({
lastUntil: until,
until: Math.max(0, until - diff),
});
}
if (currentAppState === 'background') {
this.setState({ wentBackgroundAt: Date.now() });
}
};
getTimeLeft = () => {
const { until } = this.state;
return {
seconds: until % 60,
minutes: parseInt(until / 60, 10) % 60,
hours: parseInt(until / (60 * 60), 10) % 24,
days: parseInt(until / (60 * 60 * 24), 10),
};
};
updateTimer = () => {
if (this.state.lastUntil === this.state.until || !this.props.running) {
return;
}
if (this.state.until === 1 || (this.state.until === 0 && this.state.lastUntil !== 1)) {
if (this.props.onFinish) {
this.props.onFinish();
}
if (this.props.onChange) {
this.props.onChange(this.state.until);
}
}
if (this.state.until === 0) {
this.setState({ lastUntil: 0, until: 0 });
} else {
if (this.props.onChange) {
this.props.onChange(this.state.until);
}
this.setState({
lastUntil: this.state.until,
until: Math.max(0, this.state.until - 1),
});
}
};
renderCountDown = () => {
const { timeToShow, numberStyle } = this.props;
const { until } = this.state;
const { days, hours, minutes, seconds } = this.getTimeLeft();
const newTime = sprintf('%02d:%02d:%02d:%02d', days, hours, minutes, seconds).split(':');
return (
<View style={styles.timeCont}>
{timeToShow.includes('M') ? <Text style={numberStyle}>{newTime[2]}:</Text> : null}
{timeToShow.includes('S') ? <Text style={numberStyle}>{newTime[3]}</Text> : null}
<Text style={numberStyle}> сек. </Text>
</View>
);
};
render() {
return <Text style={styles.container}>{this.renderCountDown()}</Text>;
}
}
CountDown.defaultProps = {
digitStyle: DEFAULT_DIGIT_STYLE,
digitTxtStyle: DEFAULT_DIGIT_TXT_STYLE,
timeLabelStyle: DEFAULT_TIME_LABEL_STYLE,
timeLabels: DEFAULT_TIME_LABELS,
separatorStyle: DEFAULT_SEPARATOR_STYLE,
timeToShow: DEFAULT_TIME_TO_SHOW,
afterTimeLabel: true,
until: 0,
size: 15,
running: true,
};