繁体   English   中英

如何显示元素名字和姓氏...来自 strore redux

[英]how to display element firstname and lastname ... from strore redux

在这里找到我的代码,当我尝试获取名字时,我得到了“无法读取未定义的属性”的问题,有人可以提供解决方案或解释我的代码有什么问题吗

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 中使用选择器获取状态

您是否在 useEffect 之前对变量 state 进行了控制台操作。 我想不需要使用(useState & useEffect)你可以直接使用你的变量 state。 尝试在 useEffect 之前或之后添加 console.log(state) 并与 as 共享。

我试试

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) 第一次调试不带名字 字段错误

但是当我像这样添加我需要的字段时,我变得不明确

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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