Skip to content

Instantly share code, notes, and snippets.

@J-Cake
Created April 20, 2019 03:21
Show Gist options
  • Save J-Cake/1d84f3e9690ffa6cd284fde321d3a857 to your computer and use it in GitHub Desktop.
Save J-Cake/1d84f3e9690ffa6cd284fde321d3a857 to your computer and use it in GitHub Desktop.
The React refs keep dissapearing
async beginDownload() {
if (this.state.downloadURL) {
const that = this; // lol I know
const onStatusChange = async function (status) {
if (status === 2 || status === 3) {
that.forceUpdate();
// this.props.remove.bind(this)();
}
};
const remove = function () {
function getIndex(array) {
return array.findIndex(i => i.key === download.key);
}
const index = getIndex(that.state.activeDownloads);
that.state.activeDownloads.splice(index, 1);
console.log(that.state.activeDownloads.slice(index, 1));
that.forceUpdate();
};
const url = this.state.downloadURL,
name = this.state.downloadName,
headers = this.state.customHeaders;
const download = <DownloadComp
url={url}
name={name}
customHeaders={headers}
onComplete={function (err) {
if (!err)
this.props.remove.bind(this)();
that.next(this)
}}
onStatusChange={onStatusChange}
alert={box => this.alert(box)}
id={this.state.activeDownloads.length + 1}
remove={remove}
updateTaskBarProgress={(index, progress) => this.updateTaskBarValue(index, progress)}
key={`download${this.state.downloadNums}`}
ref={this.me}/>;
await this.setState(prev => ({
activeDownloads: [...prev.activeDownloads, download],
downloadNums: prev.downloadNums + 1
}));
await App.addToDownloadHistory(this.state.downloadURL, this.state.downloadName, this.state.customHeaders);
this.closePrompt();
console.log("Refs", this.state.activeDownloads.map(i => i.ref.current));
if (this.state.activeDownloads.length === 1) {
// this.state.activeDownloads[0].ref.current.startDownload();
this.next();
}
} else {
this.setState({requiredField: true});
}
}
next(download) {
if (download) {
console.log("Refs", download.startDownload);
this.setState(prev => ({
inactiveDownloads: [...(prev.inactiveDownloads || []),
<DownloadDisplayComp key={`downloadDisplay${this.state.downloadNums}`}
contents={{...download.state}}/>]
}));
}
if (this.state.activeDownloads[0])
this.state.activeDownloads[0].ref.current.startDownload();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment