【发布时间】:2021-10-30 23:37:53
【问题描述】:
我正在实现一个简单的 MyPage。当 Mypage.tsx 首次渲染时,Redux 将用户信息保存为 useEffect。但是,当我尝试从右下方使用 UseSelector 获取数据时,它说找不到该值。
也许是因为数据在接收到值的同时立即进行处理。有没有办法解决这个问题?
错误:TypeError:无法读取 null 的属性“myPostList”
部分错误:const MyPost = userData.myPostList.map(.....
import { GetUserInfo } from "../modules/action-creator";
import { RootState } from "../modules/Store";
import "../styles/mypage.scss";
function Mypage() {
const dispatch = useDispatch();
useEffect(() => {
dispatch(GetUserInfo())
}, [])
const userData = useSelector((state : RootState) => state.User.userData)
const MyPost = userData.myPostList.map(
(item:any, index:number) => {
return(
<tbody id = {"body" + index} key = {item.id}>
<tr>
<td id = "title">{item.postName}</td>
</tr>
<tr>
<td id = "content">{item.content}</td>
</tr>
</tbody>
)
}
)
return (
<div className="mypost">
<div className="post_info">
전체글 0개
<button>글쓰기</button>
</div>
<table className="post_detail">{MyPost == null ? "표시할 정보가 없습니다." : MyPost}</table>
</div>
)
}
如下所述,数据似乎在接收到所有值之前就已处理完毕。有办法解决吗?
【问题讨论】:
标签: reactjs typescript react-redux react-hooks dispatch