I want to merge two state named Data and District value and create a single state named test.Since i am fetching two states value from different API.I cannot make a single state merging the data of the following states,i tried but when i tried to merge two state value a single states value i get but next state value cannot be obtained,since i tried to use spread operator but still cannot solve the issue.Following below are my code:Please help for solution:
import React, { useState, useEffect } from 'react';
import Axios from 'axios';
export default function Testmap() {
const [Data, setData] = useState([]);//fetched data
const [isLoading, setIsloading] = useState(true);
const [District, setDistrict] = useState([]);
const [test, setTest] = useState({
data: [],
districts: []
});
useEffect(() => {
//fetching data from api
Axios.get('https://covid19wst.yilab.org.np/api/v1/form')
.then(res => {
setData(res.data)
setTest({ ...test, data: res.data })
setIsloading(false)
})
.catch(err => {
console.log(err);
})
Axios.get('https://bipad.gov.np/api/v1/district/')
.then(res => {
setDistrict(res.data.results)
setTest({ ...test, districts: res.data.results })
setIsloading(false)
})
.catch(err => {
console.log(err);
})
}, [])
console.log(isLoading);
console.log("data", Data);
console.log("districts>>", District);
console.log("test>>>", test);
return (
<div>
<h2>Hello</h2>
</div>
)
}
Here in above output i tried to keep two state values of data and districts in a single state test but when the code is run i cannot get update value with previous value when i used spread operator to preserve previous value.Still prevoius value is not preserved,how can i solve the issue?Please help.
