reduxjs / react-redux

Official React bindings for Redux
https://react-redux.js.org
MIT License
23.39k stars 3.36k forks source link

Sync data fetching and normalization in real world use case #1731

Closed otakustay closed 3 years ago

otakustay commented 3 years ago

New Features

Currently we don't have a solution for sync data fetching and normalization working together.

The key problem is, when we do a sync data fetching (by directly const data = readData()) in render function, we have to introduce an async useEffect to dispatch actions putting data into redux store, while useEffect is async, sync rendered children cannot reach fetched data.

I've created a sandbox to demonstrate this issue: https://codesandbox.io/s/dazzling-worker-73nem?file=/src/App.tsx

Since react is going forward to 18 and suspense data fetching is about to land, this pattern can be more and more popular in the future.

What is the new or updated feature that you are suggesting?

Should be a new feature

Why should this feature be included?

Normalization is a good idea for robust applications but it is now working well with sync data fetching like Suspense, this can introduce risks when we want to move to newer architectures

What docs changes are needed to explain this?

I don't have any idea about how it can be solved, but this is a problem for current react-redux

markerikson commented 3 years ago

There's a couple problems with this issue.

First, this repo is for React-Redux, which is the bindings layer between a Redux store and your React components. Any requests for features related to core Redux logic aren't relevant in this repo.

Second, we just released our brand new "RTK Query" API as part of Redux Toolkit 1.6, which is a purpose-built data fetching solution built for Redux usage. Please see the RTK Query docs for details on how to use it:

https://redux-toolkit.js.org/rtk-query/overview

Third, "normalization" is a completely different question than "data fetching" and "Suspense".

We'll look at adding Suspense support into RTK Query down the road.