I'm having a strange issue with state in my React app. In this I use one state and one useEffect. State is used to store posts which is fetch from jsonplaceholder. fetch and set state written inside the useffect with empty array as an option for dependency.
I even retrieve the data from jsonplaceholder, it not set the usestate. it display empty array which is the initial value.
sometimes it update when I save the code but after I refresh the page it will gone and not updating again I even tried many times
but the problem is react-dev-tools shows state updated value which is all the posts. but 'console.log() ' doesn't show the data.
tell why the setposts() method doesn't seem to be setting the state,
import React,{ useEffect,useState} from 'react'
export default function Test() {
const [ posts, setposts] = useState([]);
useEffect(()=>{
fetch('https://jsonplaceholder.typicode.com/posts',{
method:'get',
}).then(result=>{
return result.json();
}).then((data)=>{
console.log(data);
setposts(data);
console.log(posts);
})
},[])
return (
<div>
</div>
)
}