Home > Back-end >  how to display element firstname and lastname ... from strore redux
how to display element firstname and lastname ... from strore redux

Time:01-07

find here my code whene I try to get firstName I get the issue “Cannot read properties of undefined” could some one give a solution or expalain what's the issue whith my code

let state = useSelector(state => {
      // console.log('State: ', state.profile);
       return state.profile;
     });


   const [profile, setProfile] = useState();
   const [formState, setFormState] = useState({
       isValid: false,
       values: {},
       touched: {},
       errors: {}
   });

   useEffect(()=>{
       console.log({accesstoken: token});
   if(token)
       dispatch(profileActions.getInfos({accesstoken: token}));
   },[])


   useEffect(()=>{
       if (state)
       setProfile(state)
       console.log(profile.profile);
   },[state, profile])
/// here the peace of code to display the profile.data.firstname
ListItemText primary="Date de creation" secondary{JSON.stringify(profile.data.firstName)} />

in a debug i could get state with selector in usestate

CodePudding user response:

Did you console the variable state just before your useEffect. I guess no need to use (useState & useEffect) you can directly use yout variable state. try to add a console.log(state) before or after the useEffect and share it with as.

CodePudding user response:

I try

const profile = useSelector(state => state.profile.profile.data);
console.log(profile);



output => {
"_id": "61d6cbe4e3afe444b1500f50",
"email": "[email protected]",
"firstName": "elmahdi",
"lastName": "frid",
"number": null,
"created_at": "2022-01-06T11:00:52.009Z",
"is_verified": null}

const profile = useSelector(state =>state.profile.profile.data.firstName) first debug withou filds firstname error with filds

but when i add the field that I need like this i get undifined

  •  Tags:  
  • Related