-2

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.

enter image description here

Please help me how I store this in a state. I am very new to react.

Ethan
  • 876
  • 8
  • 18
  • 34

3 Answers3

0

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!

Amit Maraj
  • 344
  • 1
  • 5
0

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)
Alan66
  • 11
  • 5
0

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.