question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

usePopState has wrong type definition, hook dependencies

See original GitHub issue

Hey again!

So I have a use-case where a modal dialog contains links and the modal needs to close on any history change.

Rather than making my own listener, I went to see how raviger implements it, and found the nifty usePopState hook that does pretty much what I need. However I found the following:

  1. usePopState is exposed but has no entry in the documentation. Is it part of the API? Is it fine to use it?

  2. In the type definition, it says that the predicate parameter is a function that returns a boolean, however, according to the actual implementation and usages across the library, it seems that it’s only expecting a boolean.

  3. The predicate parameter is not added to the dependencies of the useEffect hook, which means that if we need to change that condition after the initial render, it won’t be taken into account.

  4. I feel that predicate should be an optional argument, and could be moved to being the third parameter?

  5. Probably have the same feeling about the first parameter. Seems like basePath could be optional and default to ''.

Looking forward to hearing about your opinion on these 👍

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
kyeoticcommented, Nov 5, 2019

Hey @yenoiwesa 👋

  1. It was exposed without a lot of thought. I’m glad you’re brining this up before the 1.0 release is finalized, because I don’t think I want to use that name if its part of the API. It does make sense in the API though, being a routing library and all. Any name suggestions?
  2. This is a result of its exposure being under-considered. I wrote the type and the implementation on different nights. I’m leaning towards making it either, but if it’s going to be one it should probably be the function predicate. This needs some freshly-caffeinated consideration.
  3. This is an error.
  4. Yes, it should be optional.
  5. This is actually the stickiest point for me. Given that the hook will have access to the router-context it could just fetch the basePath, if one exists. But then if you want to opt-out its awkward. But otherwise most times you will be calling usePopState(useBathPath()), which is also awkward. This needs more thought.
1reaction
kyeoticcommented, Nov 5, 2019

@yenoiwesa Check out #29 and let me know what you think.

Its published with the first release candidate

npm i raviger@rc
# or
npm i raviger@1.0.0-rc.1
Read more comments on GitHub >

github_iconTop Results From Across the Web

1.0 release · Issue #27 · kyeotic/raviger - GitHub
If nothing blocking is raised I will cut a release candidate on Tuesday Nov ... usePopState has wrong type definition, hook dependencies #28....
Read more >
React Hook useEffect has missing dependencies: 'colors' and ...
My component is showing the error on the last line. What am I doing wrong? function MemoryGame({ options, setOptions, highScore, setHighScore }) ...
Read more >
Understanding common frustrations with React Hooks
“React Hook useEffect has a missing dependency”. This error occurs if we are miss adding a necessary dependency to the useEffect hook dependency ......
Read more >
Hooks FAQ - React
Is it safe to omit functions from the list of dependencies? ... The latest Flow and TypeScript React definitions include support for React...
Read more >
Solve - React Hook useEffect has a missing dependency error.
In React whenever a component re-renders all the functions within it are re-created since functions are objects or reference types. So putting ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found