Home > Net >  Storing list with objects in a state for future use in react
Storing list with objects in a state for future use in react

Time:04-09

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

CodePudding user response:

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!

CodePudding user response:

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)

CodePudding user response:

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.

  • Related