Giter Site home page Giter Site logo

Comments (4)

ryanashcraft avatar ryanashcraft commented on May 20, 2024

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.

MrNice avatar MrNice commented on May 20, 2024

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.

simenbrekken avatar simenbrekken commented on May 20, 2024

@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.

ryanashcraft avatar ryanashcraft commented on May 20, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.