In a functional component, it would be more appropriate to use the hook version, useRef
:
const ref = useRef();
It won't make a difference in the logic here, but it's a good habit to get into. (createRef
will create a new ref each render, which usually isn't desirable - if other things used it as a prop or in a dependency array, they would re-run/re-render more often than needed)
Though, if you wanted, you could remove the use of refs altogether by navigating to the previous element inside the click handler:
const clickHandler = e => handleUpload(e.target.previousElementSibling);
<input type='submit' value='Upload file' onClick={clickHandler}/>
(The type="submit"
seems odd - if this is in a form, you'll want to call e.preventDefault()
too. If it's not in a form, the type="submit"
doesn't do anything, I believe)
I also don't see any sense in making a hook of handleUpload
, I'd just declare handleUpload
in the main body CSVUpload
, maybe with a useCallback
if you wanted.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…