GithubHelp home page GithubHelp logo

Cannot use useRouter about react HOT 4 CLOSED

mallowigi avatar mallowigi commented on April 27, 2024
Cannot use useRouter

from react.

Comments (4)

mallowigi avatar mallowigi commented on April 27, 2024

Ok I have no idea why, but after I replaced with props.transition.router.stateService and executed a successful go, for an unknown reason now useRouter works again.

Truly speechless.

from react.

christopherthielen avatar christopherthielen commented on April 27, 2024

@mallowigi thanks for the report... I'm not sure why this would happen. This commit is the only thing that seems remotely related: 834c755 which initializes the router object on the first render (and renders null), then renders the children on all subsequent renders. This code was written this way to handle react strict mode's intentional double renders.

The useRouter code is a simple wrapper for useContext and should have normal react context behaviors

https://github.com/ui-router/react/blob/master/src/hooks/useRouter.ts#L29

I dropped your code into a stackblitz and couldn't reproduce the problem https://stackblitz.com/edit/react-pwx5fi

I'm not sure what caused the behavior, but if it can be reproduced, I'd love to fix it in the codebase.

from react.

christopherthielen avatar christopherthielen commented on April 27, 2024

Oh wow, I just encountered this same error! The problem was due to using useRouter inside the same component as the one that renders the <UIRouter/> component. In other words:

function App() {
  const currentState = useRouter().globals.current; // kaboom, the router isn't in context for this component

  return (
    <UIRouter
          config={config}
          plugins={[pushStateLocationPlugin]}
          states={states}
        >
         current state: {currentState.name}
         <UIView/>
        </UIRouter>
      </RecoilRoot>
    </StylesProvider>
  </React.StrictMode>
  );
);

In my case, pulling the <UIRouter/> up one level was what I needed to do:
https://github.com/ui-router/sticky-states/blob/master/examples/create-react-app/src/App.js#L36-L42

from react.

mallowigi avatar mallowigi commented on April 27, 2024

I don't think it was the same setup but I'll try to reproduce from a brand new project and tell you the results. Thanks!

from react.

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.