I am getting a response like this from my axios response and I want to store the same in a state to pass it further.
Please help me how I store this in a state. I am very new to react.
I am getting a response like this from my axios response and I want to store the same in a state to pass it further.
Please help me how I store this in a state. I am very new to react.
You can accomplish this by setting your state variable to something like this:
const Component = () => {
let [items, setItems] = useState([]);
useEffect(() => {
axios.get('https://someurl.com')
.then(data => {
setItems(data)
})
}, [])
}
In your render function, you can then access the items array normally. Hope this helps!
first import useState from react and then do something like this with the response you are getting.
import react, {useState} from 'react'
const App = () => {
const [data,setData} = useState([])
}
And inside the function that triggers the add event
setData(res.data)
If you want to save the response in the state, first you need to create a state:
const [value, setValue] = React.useState([]);
Next, you need to use setValue
to put your response in the state:
setValue(API_RESPONSE)
So React will render this change and you can print value
to see what value this const has.