【问题标题】:getting error while destructuring the data from react redux. why it is giving error and how to solve this?从 react redux 解构数据时出错。为什么它给出错误以及如何解决这个问题?
【发布时间】:2021-07-11 07:24:10
【问题描述】:

这是我试图获取用户数据并在代码的突出显示部分中出错的配置文件组件。为什么 userData 是未定义的??

import React, {useEffect} from 'react';
import {useDispatch, useSelector} from 'react-redux';
import { getUserProfile } from '../../actions/userActions';
import Styles from './Profile.module.scss';

const Profile = (props) => {
    const userId = props.match.params.id;

    const dispatch = useDispatch();

    const userLogin = useSelector(state => state.userLogin);
    const {userInfo} = userLogin;

    **const userProfile = useSelector(state => state.userProfile);
    const {loading, error, userData} = userProfile;

    const {posts, data} = userData;**

    useEffect(() => {
        if(!userInfo){
            props.history.push('/login');
        }else{
            dispatch(getUserProfile(userId));
        }
    }, [dispatch,userId])

    return (
        <section>
            <div className={Styles.Container}>
                <h1>{data.userName}</h1>
                <h1>{data.followers.length} followers</h1>
                <h1>{data.following.length} following</h1>
                <h1>{posts.length} posts</h1>
            </div>
        </section>
    )
}

export default Profile

这是 userProfile 的 Reducer 文件 -

export const userProfileReducer = (state={ userData: {}}, action) => {
  switch(action.type){
    case GET_USER_PROFILE_REQUEST:
      return { loading: true, ...state, }
    
    case GET_USER_PROFILE_SUCCESS:
      return { ...state, loading: false, userData: action.payload}
    
    case GET_USER_PROFILE_FAIL:
      return { loading: false, error: action.payload}
    default: return state;
  }
}

我收到了这个错误

TypeError: Cannot destructure property 'posts' of 'userData' as it is undefined. Profile

【问题讨论】:

    标签: javascript node.js reactjs react-redux react-router


    【解决方案1】:

    我们不可能知道为什么它是未定义的,因为您没有显示在您的 redux 存储中添加 userProfile 状态的位置。

    我认为userData 在渲染期间“正在加载”,所以对于第一次渲染,userData 是未定义的。

    为避免此错误,请尝试添加 if 语句:

        useEffect(() => {
            if(!userInfo){
                props.history.push('/login');
            }else{
                dispatch(getUserProfile(userId));
            }
        }, [dispatch,userId])
    
        if (userData) {
          const {posts, data} = userData;**
          return (
            <section>
                <div className={Styles.Container}>
                    <h1>{data.userName}</h1>
                    <h1>{data.followers.length} followers</h1>
                    <h1>{data.following.length} following</h1>
                    <h1>{posts.length} posts</h1>
                </div>
            </section>
          )
        }
    
        return <div>Loading...</div>
    

    【讨论】:

    • 我也分享了reducer文件代码,够了吗?
    • 是的,我试过了,非常感谢你
    • 所以这只是因为第一次渲染你的值是未定义的。如果您在项目的顶层使用 IF 语句来在获取 userData 期间显示“加载”状态,则可以避免这种错误;)您也可以使用减速器给出的“加载”状态。
    【解决方案2】:

    问题是您的case GET_USER_PROFILE_FAIL 返回一个具有属性loadingerror 的对象,并且不会从...state 复制任何其他属性。这会导致userData 属性变为undefined

    如果这是所需的行为,那么您需要检查组件中的 undefined 并显示某种错误消息。

    【讨论】:

    • 那么在这种情况下应该是什么?
    • 如果你只是想抑制这个特定的错误,那么你可以在减速器中return { ...state, loading: false, error: action.payload},但你会显示一个奇怪的空屏幕。
    • 就在组件中的主要return 之前,执行if ( ! userData &amp;&amp; ! loading ) { return &lt;div&gt;{error}&lt;/div&gt;; } 之类的操作
    猜你喜欢
    • 2021-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-02
    • 1970-01-01
    • 2021-06-25
    相关资源
    最近更新 更多