I have something like this currently:
const Edit_History = (props) => {
var thing = ''
useEffect(() => {
const id = props.match.params.id
if (thing === '')
axios.get(`http://localhost:3010/v0/student/${id}/${id}`)
.then(({ data }) => {
thing = data
console.log(thing)
})
}, [])
const submitHandler = async (e) => {
console.log(thing)
etc...
}
When I first print out "thing" in useEffect, I get the value that I want. But when I try to print it on-submit with submit handler, it prints out an empty string, even though I don't modify the value of "string" outside of useEffect. I'm out of ideas on why this could be happening.
edit:
I originally tried using useState
const Edit_History = (props) => {
const [thing, setThing] = useState("")
useEffect(() => {
const id = props.match.params.id
axios.get(`http://localhost:3010/v0/student/${id}/${id}`)
.then(({ data }) => {
setThing(data)
console.log(thing)
})
}, [])
const submitHandler = async (e) => {
console.log(thing)
etc...
}
but after setThing(data), thing is still undefined which was why I tried using var thing instead of useState.