I am attempting to relay data between a file uploader and a table, using a shared ancestor. I have a file uploader but once I add my JSON file I receive the error TypeError: setProductState is not a function.
When I console log my inputJson file it goes though, but when I apply setProductState and input my file I receive this error.
Based on what I've read I don't need a bind because I am using arrow syntax, but I possibly might need a 'this'.
Any thoughts or critiques are welcome.
function FileUploader({ productState, setProductState }) {
const onDrop = useCallback((acceptedFiles) => {
// this.setProductState = this.setProductState.bind(this); tried
acceptedFiles.forEach((file) => {
const reader = new FileReader()
reader.onload = () => {
const inputJson =
this.inputJson = inputJson //tried
JSON.parse(reader.result)
setProductState(inputJson); //console.log goes through
}
reader.readAsText(file)
})
}, [])
const {getRootProps, getInputProps} = useDropzone({onDrop})
return (
<div {...getRootProps({style})}>
<input {...getInputProps()} value={productState}/>
<p>Drag files here, or click to browse</p>
</div>
)
}
Ancestor.js
import React, { useState } from 'react'
import FileUploader from './FileUploader'
import Table from './Table'
const Ancestor = () => {
const [products, setProducts] = useState({});
return <>
<FileUploader productState={products} setProductState={setProducts} />
<Table productState={products} />
</>;
}
export default Ancestor;