简体   繁体   English

我如何呈现从我在 redux-thunk 中所做的响应中获得的数据并将其放入 useEffect 中?

[英]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.

相关问题 我可以在 NextJS (redux-thunk) 中使用本地存储 - I can use local Storage in NextJS (redux-thunk) 我应该通过组件还是Redux-thunk中间件获取状态数据? React Redux - Should i get state data via component or redux-thunk middleware ? React Redux 我可以将 redux-thunk 与 socket.io 一起使用,还是需要构建自定义中间件? - Can I use redux-thunk with socket.io or do I need to build a custom middleware? redux-thunk - 即使我使用了 mapDispatchToProps 也没有调度 - redux-thunk - not dispatching even though I used mapDispatchToProps 我应该如何使用“redux-thunk”进行异步初始状态? (反应/终极版) - How should I use “redux-thunk” for Async Initial state ? (react/redux) 如何使用 redux-thunk 调度操作让连接反应路由器重定向? - How do I get connected-react-router to redirect using a redux-thunk dispatch action? 如何在 useEffect 之后使用更新的数据渲染组件,useEffect 从 API 获取数据 - How can I render Component with updated data after useEffect, the useEffect fetches data from an API 在中间件即 redux-thunk 或 redux-saga 中运行异步代码有什么好处 - What is the advantage of running asynchronous code in middleware i.e. redux-thunk or redux-saga 找不到模块:无法解决“ redux-thunk ”错误我已经尝试了解决方案,但没有发生。解决方案是什么? - Module not found: can't resolve' redux-thunk ' error I have tried the solutions but it does not happen.What is the solution? redux-thunk 如何工作? - How redux-thunk works?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM