Javascript React Redux渲染刷新崩溃
我有一个带Redux的Mern应用程序。我有个问题。配置文件组件在两种情况下崩溃。1) 在构建版本中刷新页面后。2)当直接从地址栏执行Proiles路由时。(用于减小大小的较短代码) 1) js组件通过“getProfiles”操作方法呈现来自redux状态的所有配置文件Javascript React Redux渲染刷新崩溃,javascript,reactjs,react-redux,react-router,mern,Javascript,Reactjs,React Redux,React Router,Mern,我有一个带Redux的Mern应用程序。我有个问题。配置文件组件在两种情况下崩溃。1) 在构建版本中刷新页面后。2)当直接从地址栏执行Proiles路由时。(用于减小大小的较短代码) 1) js组件通过“getProfiles”操作方法呈现来自redux状态的所有配置文件 return ( <Fragment> {loading ? ( <Spinner /> ) : ( <Fragment>
return (
<Fragment>
{loading ? (
<Spinner />
) : (
<Fragment>
<h1 className="large text-primary">Profiles</h1>
<p className="lead">Üyeleri ve grupları keşfedin</p>
<div className="form">
<div className="form-group">
<input
type="text"
placeholder="İsim"
name="company"
value={company}
onChange={(e) => onChange(e)}
/>{" "}
</div>{" "}
</div>
<div
className=" lead d-flex align-items-center"
onClick={() => setOpen(!open)}
>
</div>
<div className="profiles">
{profiles.length > 0 ? (
profiles.map(
(profile) =>
profile.user._id !== user._id && (
<ProfileItem
key={profile._id}
followUser={followUser}
unFollowUser={unFollowUser}
profile={profile}
user={user}
/>
)
)
) : (
<h4>No Profile</h4>
)}
</div>
</Fragment>
)}
</Fragment>
);
};
Profiles.js的减速器
export const getProfiles = (
page,
start,
graduation,
mentorship,
scholarship,
representative,
company
) => async (dispatch) => {
const limit = 10;
const payload = {
params: {
page,
start,
limit,
graduation,
mentorship,
scholarship,
representative,
company,
},
};
console.log(page);
console.log(payload);
try {
const res = await axios.get("/api/profile", payload);
console.log(res.data.profiles);
dispatch({
type: GET_PROFILES,
payload: { profiles: res.data.profiles, totalPages: res.data.totalPages },
});
console.log(res.data);
} catch (err) {
dispatch({
type: PROFILE_ERROR,
payload: { msg: err.response.statusText, status: err.response.status },
});
}
};
case GET_PROFILES:
return {
...state,
profiles: payload.profiles,
totalPage: payload.totalPages,
loading: false,
};
当我用React路由器组件从导航中单击时,一切正常。但当我刷新或直接从地址栏进入页面时,它崩溃了。控制台中出现了这些错误
页面仅在内置模式下崩溃。发展模式良好。我试图更改条件(加载等),但没有任何更改
任何帮助都将不胜感激。
谢谢大家! 问题似乎不在于配置文件,而在于用户id。最初,在第一次呈现配置文件组件时,用户为null,因此您得到的错误id为null 尝试检查用户是否存在,然后渲染配置文件。就像Profile.js一样
if(!user){
return null
}
return (
<Fragment>
// yor rest code
</Fragment>
if(!user){
返回空
}
返回(
//约尔休息代码
另外,在使用console.log加载页面后,检查配置文件的用户属性是否显示为null。我不确定,但您可以进行验证以避免第一个错误,我指的是“\u id”错误:
<div className="profiles">
{
profiles.length > 0 && user ? (
profiles.map((profile) => {
if (profile.user && profile.user._id !== user._id) {
return (
<ProfileItem
key={profile._id}
followUser={followUser}
unFollowUser={unFollowUser}
profile={profile}
user={user}
/>
);
}
})
) : (
<h4>No Profile</h4>
)
}
</div>;
{
profiles.length>0&用户(
profiles.map((profile)=>{
if(profile.user&&profile.user.\u id!==user.\u id){
返回(
);
}
})
) : (
无个人资料
)
}
;
statusText错误是因为您的try catch inside profiles.js中有一个错误,当您想要发送一个PROFILE\u错误操作时,您试图从err.response.statusText访问statusText,但err.response未定义。用户在哪里定义?