Skip to content

Commit 57f0ba0

Browse files
committed
fixed clearly change of changeUnit
1 parent ef9e8dc commit 57f0ba0

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed

dist/react-pure-time.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/example.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/react-pure-time.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,22 +142,28 @@ const Time = (props: Props) => {
142142
if (props.relativeTime && isDate(props.value)) {
143143
const date = new Date(props.value);
144144
const diff = getRelativeTimeDiff(date);
145-
state.currentUnit = props.unit || bestFit(diff);
145+
setState({
146+
...state,
147+
currentUnit: props.unit || bestFit(diff),
148+
});
146149

147150
setState({
148151
...state,
149152
relativeTime: updateRelativeTime(date, state.currentUnit),
150153
});
151154
interval = window.setInterval(() => {
152-
state.currentUnit = props.unit || bestFit(diff);
155+
setState({
156+
...state,
157+
currentUnit: props.unit || bestFit(diff),
158+
});
153159
updateRelativeTime(date, state.currentUnit);
154160
}, getInterval(state.currentUnit));
155161
}
156162

157163
return () => {
158164
if (interval) window.clearInterval(interval);
159165
};
160-
}, [props.relativeTime, props.value, props.unit]);
166+
}, [props.relativeTime, props.value, props.unit, state.currentUnit]);
161167

162168
return (
163169
<span className={className}>

0 commit comments

Comments
 (0)