[英]How can I render the data that I got from the response that I did in redux-thunk and put it in useEffect?
I want to render the fullName string from response from the API call that I did in redux-thunk, and put it in useEffect with dispatch (useDispatch() hook).我想从我在 redux-thunk 中执行的 API 调用的响应中呈现 fullName 字符串,并将其放入带有调度的 useEffect 中(useDispatch() 挂钩)。 If I return nothing in JSX and if I console.log the state it is actually passes, and I can see the whole data in the console.如果我在 JSX 中什么都不返回,并且如果我 console.log state 它实际上是通过了,我可以在控制台中看到整个数据。 But if I want to render any part of the data, I get an error:但是如果我想渲染数据的任何部分,我会得到一个错误:
Uncaught TypeError: Cannot read properties of null (reading 'fullName').未捕获的类型错误:无法读取 null 的属性(读取“全名”)。
const Profile = (props) => {
let { userId } = useParams();
const dispatch = useDispatch();
const state = useSelector((state) => state);
useEffect(() => {
dispatch(getUserProfile(userId));
}, []);
return <div>
{state.profile.profile.fullName} //error if i want smth to render, two profile because one from redux's initialState, and one from api.
</div>;
};
export default Profile;
THUNK砰的一声
export const getUserProfile = (userId) => (dispatch) => {
profileAPI.getProfile(userId).then((response) => {
debugger
dispatch(setProfile(response));
});
};
I tried conditional rendering, but it didnt get me the solution.我尝试了条件渲染,但没有找到解决方案。 Ive tried a lot ways of conditional rendering, and no one of them was helpful我尝试了很多条件渲染的方法,但没有一种有帮助
Can you Please share Profile Reducer Code also because as per my observation Profile is like an object你能不能也请分享 Profile Reducer 代码,因为根据我的观察,Profile 就像一个 object
export const initialState = {
profile:{fullName:""};
}
export const profileReducer =(state = initialState,action={})=>{
..
}
and you want to get only profileReducer Data then get only profileReducer data by state.profileReducer an get the value as const {profile}并且您只想获取 profileReducer 数据,然后通过 state.profileReducer 仅获取 profileReducer 数据,并将值作为 const {profile}
const {profile} = useSelector((state) => state.profileReducer);
for testing check if are you able to getting proflie data by用于测试检查您是否能够通过以下方式获取配置文件数据
useState(()=>{console.log("profile",profile)},[profile])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.