Here is the profile component where i am trying to fetch the user data and getting error in the highlighted part of code. why is userData is undefined ??
import React, {useEffect} from 'react';
import {useDispatch, useSelector} from 'react-redux';
import { getUserProfile } from '../../actions/userActions';
import Styles from './Profile.module.scss';
const Profile = (props) => {
const userId = props.match.params.id;
const dispatch = useDispatch();
const userLogin = useSelector(state => state.userLogin);
const {userInfo} = userLogin;
**const userProfile = useSelector(state => state.userProfile);
const {loading, error, userData} = userProfile;
const {posts, data} = userData;**
useEffect(() => {
if(!userInfo){
props.history.push('/login');
}else{
dispatch(getUserProfile(userId));
}
}, [dispatch,userId])
return (
<section>
<div className={Styles.Container}>
<h1>{data.userName}</h1>
<h1>{data.followers.length} followers</h1>
<h1>{data.following.length} following</h1>
<h1>{posts.length} posts</h1>
</div>
</section>
)
}
export default Profile
Here is the Reducer file for userProfile -
export const userProfileReducer = (state={ userData: {}}, action) => {
switch(action.type){
case GET_USER_PROFILE_REQUEST:
return { loading: true, ...state, }
case GET_USER_PROFILE_SUCCESS:
return { ...state, loading: false, userData: action.payload}
case GET_USER_PROFILE_FAIL:
return { loading: false, error: action.payload}
default: return state;
}
}
I am getting this error
TypeError: Cannot destructure property 'posts' of 'userData' as it is undefined. Profile