【发布时间】:2020-07-25 01:23:09
【问题描述】:
我正在使用包react-native-background-downloader。 我的状态是这样初始化的:
const [downloadFile, setDownloadFile] = useState({});
在我从 api 获取数据之后 我更新了我的状态:
setDownloadFile({
thumbnail:
'https://img.youtube.com/vi/J6rVaFzOEP8/maxresdefault.jpg',
heading: 'Guide To Becoming A Self-Taught Software Developer',
timing: 123,
status: 'Staring ...',
});
然后我使用包从 url 下载视频
const ts = RNBackgroundDownloader.download({
id: inputUrl,
url: 'url too long to display',
destination: `${RNBackgroundDownloader.directories.documents}/test.mp4`,
});
setTask(ts);
ts.begin(async (res) => {
await setDownloadFile({
...downloadFile,
timing: res / 1024,
});
console.log('onbegin', downloadFile);
});
ts.progress(async (pr) => {
await setDownloadFile({
...downloadFile,
status: `Downloading... ${Math.round(pr * 100)}%`,
});
console.log('onProgress', downloadFile);
});
ts.done(async () => {
await setDownloadFile({
...downloadFile,
status: 'done',
});
console.log('onDone', downloadFile);
});
我的问题是时间变量中 .begin() 中的状态更新没有在 .progress() 中进行
initially => timing:123,
.begin() => timing: res / 1024,
.progress() => timing:123 (as it was in first place);
【问题讨论】:
标签: reactjs react-native setstate