Coder Social home page Coder Social logo

victortrusov / react-router-loading Goto Github PK

View Code? Open in Web Editor NEW
61.0 4.0 13.0 3.83 MB

Wrapper for react-router that allows you to load data before switching the screen

License: MIT License

JavaScript 14.23% TypeScript 85.77%
router topbar switching react fetch loading prefetch preloading react-router bar

react-router-loading's Issues

Dynamic routes do not work

The Switch doesn't seem to recognize dynamically inserted routes, eg.:

<Switch>
    { routes.map(route => <Route key={route.id} path={route.path} loading>
        { React.createElement(getComponent(route.component) }
    </Route>}
</Switch>

The routes simply do not work. If I manually add routes outside the .map(), they work as expected. The dynamic routes work as expected in react-router-dom.

Any workaround or a way to get this working, or is the support for this use case just missing?

Wildcard and fallback routes do not work

This lib was a great help to me but I believe I found two bugs. In the below example, the expected behavior is to have the NotFoundPage component render any non matching path. Instead the router renders nothing.

<Switch>
    <Route path="/page1" component={Page1} />
    <Route path="/page2" component={Page2} />
    ...
    <Route component={NotFoundPage} />
</Switch>

Bug 2: When using a wildcard path an error is thrown, "Error: Invariant failed: You should not use <Route> outside a <Router>".

<Switch>
    <Route path="/page1" component={Page1} />
    <Route path="/page2" component={Page2} />
    ...
    <Route path="*" component={NotFoundPage} />
</Switch>

I was able to fix the first bug by modifying findMatchingRoute (https://github.com/victortrusov/react-router-loading/blob/master/src/utils.js#L6) to force a match when there is no path attribute on a route but I'm unsure if that breaks anything else. I suspect a better fix would solve both problems at once.

There is no way to use LoadingContext globally

I need to change LoadigContext globally, now I'm trying this code

import { useContext, useEffect } from 'react';
import { Route, LoadingContext, Switch, } from "react-router-loading"
import { useIsFetching } from 'react-query'

const App = () => {
  const loadingContext = useContext(LoadingContext);
  const isFetching = useIsFetching()

  useEffect(() => {
    // handle loading state globally by external library
    if(isFetching) loadingContext.start()
    else loadingContext.done()
  }, [isFetching])


  return (
    <Switch>
      <Route loading path="/" component={HomePage} />
      <Route loading path="/user" component={UserPage} />
      <Route loading path="/developer/:id" component={SettingPage} />
    </Switch>
  )
}

So the LoadingContext is not available because provided in Switch component
and I don't know any other ways to get LoadingContext globally

react-router-dom vs react-router issue

First thank you for taking your time and abstracting out loading functionality like this. Now regarding the issue that I observe.

In peerDependencies you specify react-router-dom as a dependency, however in components you tend to use react-router (like here), which is not an equivalent and is in fact conflicting with the former one, 'causing nasty and sometimes unreproducible issues.

I'd suggest to import from react-router-dom everywhere.

Error: Could not find a declaration file for module

tsc throws the following error:

error TS7016: Could not find a declaration file for module 'react-router-loading'. './node_modules/react-router-loading/dist/react-router-loading.es.js' implicitly has an 'any' type.

There are types at './node_modules/react-router-loading/dist/index.d.ts', but this result could not be resolved when respecting package.json "exports". The 'react-router-loading' library may need to update its package.json or typings.

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.