I need to share a File object created via a file input, from a child component (the code below) to its parent, using a function passed as a prop from the parent component.
import React, { Component } from "react";
class FileInput extends Component {
watchFile = event => {
event.stopPropagation();
event.preventDefault();
const file = event.target.files[0];
const metadata = {
contentType: file.type
};
this.props.getKBISinput({ file, metadata });
};
render() {
return (
<label className="btn btn-default">
Browse<input
type="file"
style={{ display: "none" }}
onChange={this.watchFile}
/>
</label>
);
}
}
export default FileInput;
Here is the passed function:
getKBISinput = kBis => {
this.setState({ kBis });
};
An the parent component's state:
state = {
// ...
kBis: {
file: {},
metadata: ""
}
};
This doesn't work. Once set in the parent component's state, I try to view the File object from the React devtools.
It's stuck like that, I can't open it from the tree view:
Never had to share a special type of object between components. What's the ideal practice in this case?