简体   繁体   中英

how to display element firstname and lastname ... from strore redux

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)} />

在调试中,我可以在 usestate 中使用选择器获取状态

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.

I try

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



output => {
"_id": "61d6cbe4e3afe444b1500f50",
"email": "fridmahdi@live.fr",
"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) 第一次调试不带名字 字段错误

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM