Home > Blockchain >  how to add json objects to array state in functional component in react.js
how to add json objects to array state in functional component in react.js

Time:10-28

I need to keep The information that came from axios in the form of json at the state with map method .

but the proplem is when i use map method after axios request , just add last json to state but i have 9 object in my json

const [expensesData , setExpensesData] = useState([])

useEffect(() => {
    axios.get('API-address')
    .then(res => {
        res.data.map( info => {
            setExpensesData(
                [
                    ...expensesData,
                    {info}
                ]
            )
        })
    })
    .catch(e => console.log(e))
} , [])

CodePudding user response:

const [expensesData , setExpensesData] = useState([])

useEffect(() => {
    axios.get('API-address')
    .then(res => {
        setExpensesData(res.data);
    })
    .catch(e => console.log(e))
} , [])
  • Related