[英]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)} />
Did you console the variable state just before your useEffect.您是否在 useEffect 之前对变量 state 进行了控制台操作。 I guess no need to use (useState & useEffect) you can directly use yout variable state.
我想不需要使用(useState & useEffect)你可以直接使用你的变量 state。 try to add a console.log(state) before or after the useEffect and share it with as.
尝试在 useEffect 之前或之后添加 console.log(state) 并与 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但是当我像这样添加我需要的字段时,我变得不明确
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.