Blog.js
import React, { useState, useEffect } from "react"
import Axios from "axios"
const Home = () => {
const [blog, setBlog] = useState([])
useEffect(() => {
loadBlog()
}, [])
const loadBlog = async () => {
await Axios.get(`http://localhost:3001/api/blog/get`)
.then((res) => {
console.log(res.data.data)
setBlog(res.data.data)
console.log(blog)
})
.catch((err) => {
console.log(err)
})
}
return (
<>
<div className="container">
<div className="col-lg-10">
<h2> React CRUD Operation </h2>
</div>
<div className="col-lg-2">
<button> Add Blog </button>
</div>
<table className="table table-striped">
<thead>
<tr>
<th>ID</th>
<th>Picture</th>
<th>Title</th>
<th>Short Description</th>
<th>Author</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<tr>
<td>{blog.id}</td>
<td>
<img src={"../../../public/logo512.png"} alt="not available" />
</td>
<td>{blog.title}</td>
<td>{blog.short_desc}</td>
<td>{blog.author}</td>
<td>
<button>Edit</button> <button>Delete</button>{" "}
</td>
</tr>
</tbody>
</table>
</div>
</>
)
}
export default Home
I am fetching data from API and getting data. data is coming fine but it is not going in setBlog Hook. When I console response it is fine and data is fetched successfully but problem is with it is not going in setBlog hook.I am new to react and don't know why it is not going in setBlog Hook. any help will be appreicated
blogan object or an array of objects?console.log(blog)isn't reflecting the blog you put in viasetBlog?setStateworks in this answer; hopefully you find it useful. You're not the only one to get confused: stackoverflow.com/a/62460109/119549