After setting the state reactJs comes up with an error blogs.map is not a function. How do I convert blogs into arrays, although it is initialized as an array.
const [blogs,setBlogs] = useState([]);
useEffect(async() => {
await axios.get(`${API_URL}/api/getblogs`,{
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Access-Control-Allow-Origin': '*'
}
})
.then((res)=>{
res.data.result.forEach(element => {
element.blogArray.forEach((b)=>{
// console.log(b);
setBlogs(b);
})
});
})
.catch((err)=>{
console.log(err)
})
}, []);
useEffect(()=>{
console.log(blogs)
},[blogs]);
return(
<div className="row">
{
blogs &&
blogs.map((blog)=>{
return (
<Cardschema key={Math.random()}/>
)
})
}
</div>
)
setBlogs(res.data.result). Why are you looping over it? You're overwriting the state with an individual blog every time the forEach iterates.