Comments (4)
Yeah this is a caveat with using URLs as the "cache key" and not basing off entities. A problem that GraphQL clients inherently do not have.
What we sometimes do, which I think is similar to what you're describing, is check to see if we already have the entity we need from the entities reducer, and then return null
or undefined
for the query config, so that connectRequest
doesn't make any request.
For example:
const DashboardContainer = connectRequest((props) => {
if (dashboard) {
return {
url: `/api/dashboard/${props.dashboardId}`,
update: { ... },
};
} else {
// already have dashboard (perhaps from a different query/URL), do not make an unnecessary request
return null;
}
});
const mapStateToProps = (state, props) => {
const dashboard = selectors.getDashboard(props.dashboardId);
return {
dashboard,
};
};
from redux-query.
Yup that code snippet embodies what I meant: mapStateToProps
is the best place to answer "Do we have enough data to render?" and then send a boolean signal prop to your internal mapConfigToProps
as well as the loading component.
from redux-query.
@ryanashcraft Trying out this approach I'm getting warnings from redux-query stating it's attempting to cancel a request that is not in-flight. This is caused by the query being nulled as soon as mapStateToProps
returns the entity.
Probably not an issue at all but the warnings are slightly distracting:
query-advanced.js:281 Trying to cancel a request that is not in flight: {"url":"https://my-api.com/pages/frontpage"}
Code:
const mapStateToProps = (state, { id }) => ({
page: getPageById(state, id),
})
const mapPropsToQuery = ({ id, page }) => (
!page && createPageQuery({ id }))
)
compose(
connect(mapStateToProps),
connectRequest(mapPropsToQuery),
)
from redux-query.
@simenbrekken This should be fixed with 2.0.
Closing this issue. If someone would like to contribute more demos for the redux-query site that demonstrates these patterns, feel free to open up a PR. All I ask is that the demo is smallish and matches the style of the current demos.
from redux-query.
Related Issues (20)
- React redux v6 support? HOT 3
- Body is not being stringified and appended to the URL as query params. HOT 4
- Feature Request - Return data on repeated requests. HOT 3
- useRequest and useMutation not working property with custom middlewares HOT 6
- Feature Request - Add useRequests hooks HOT 5
- use options.header to re-memoize query configs HOT 8
- Accessing store in network interface
- useMutation error after Typescript update HOT 1
- Lazy loaded useMutation HOT 7
- Problem with useRequest HOT 2
- Feature request - Debounce HOT 3
- Testing redux-query HOT 1
- Can't use useRequest with Typescript HOT 5
- isPending Bug on useRequest HOT 4
- Mutation Promise chain setup HOT 3
- Problem with CRA code and the query middleware? HOT 1
- Question about where to put side effects HOT 5
- How can I clear all cached queries and fetch it all again after logout. HOT 1
- Handling errors HOT 1
- Need a code snippet on how to implement forceRequest
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from redux-query.