I am developing a personal project and faced a problem. I couldn't solve this with my narrow-limited knowledge. So I'm asking your help now.
below is my redux state:
user
userData:{
_id(pin):"614141995694f8ab7357ae0d"
isAdmin(pin):false
isAuth(pin):true
email(pin):"wtf3@naver.com"
name(pin):"dsakljf"
role(pin):0}
and this is a useEffect that I would use axios:
useEffect(() => {
axios.get('/api/blog/getBlogs')
.then(response => {
if(response.data.success){
setBlogs(response.data.blogs)
} else{
alert("Couldn't get post list")
}
})
})
I want to give user's id to axios so I can give user personal page later such as below:
const user = useSelector(state=>state.user)
useEffect(() => {
const userId={userId:user.userData._id}
console.log(user.userData._id)
axios.get('/api/blog/getBlogs',userId)
.then(response => {
if(response.data.success){
setBlogs(response.data.blogs)
} else{
alert("Couldn't get post list")
}
})
})
However, I got error instead of success and console said: 'can't read property '_id' of undefined'
I thought this might be a problem because I declared userId inside of useEffect So It tried to bring userId even before redux loaded. for that, I declared userId outside of useEffect. It seems works at the first moment, however, it returned the same error message as soon as I refresh the page. could you tell me how I can bring redux state in useEffect and use it properly? thx for reading, your help will be appreciated, If you need more detail of my code, don't hesitate to ask please.