【问题标题】:how to display element firstname and lastname ... from strore redux如何显示元素名字和姓氏......来自商店redux
【发布时间】:2022-01-06 14:28:27
【问题描述】:

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

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

【问题讨论】:

    标签: node.js reactjs redux use-state


    【解决方案1】:

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

    【讨论】:

    • 找到我的回复
    • 你怎么看这个错误
    • 你在 redux 操作中对你的 api 响应做了 .json() 吗?
    • 我认为是默认状态的问题,因为总是第一个初始化不起作用
    猜你喜欢
    • 2016-04-28
    • 2021-08-08
    • 2021-07-14
    • 1970-01-01
    • 2016-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多