Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

useUrl hook for router #338

Merged
merged 2 commits into from Feb 19, 2019
Merged

useUrl hook for router #338

merged 2 commits into from Feb 19, 2019

Conversation

cknitt
Copy link
Contributor

@cknitt cknitt commented Feb 17, 2019

No description provided.

Copy link
Contributor

@rickyvetter rickyvetter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requesting for use0 and just making sure we have a plan here. Code looks good (and super excited for this).

cc @chenglou since he's invested in this router.

let watcherId = watchUrl(url => setUrl(_ => url));
Some(() => unwatchUrl(watcherId));
},
[||],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use(Layout)Effect0 should get you this effect already.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, right! That's convenient!

src/ReasonReact.rei Show resolved Hide resolved
let useUrl = () => {
let (url, setUrl) = React.useState(dangerouslyGetInitialUrl);

React.useLayoutEffect0(() => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you opt for LayoutEffect here? I think that because this doesn’t need to be synchronous Effect might be a better choice, but I’m not sold either way really. I don’t think this is blocking this PR but since it would technically be a breaking change I think we should resolve before a wider release.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The use case I had was only working with useLayoutEffect.

(In my app, for some routes, I would render a Redirect component that would push a new route. For some reason, this only worked when using useLayoutEffect here and useEffect in the Redirect component. I did not investigate it any further.)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting. LayoutEffect happens earlier than Effect so perhaps these hooks are called upward and this order is required in order to trigger this subscription before the Redirect component executes. That seems like a reasonable excuse to put this here.

@rickyvetter rickyvetter merged commit 0591ee2 into reasonml:hooks Feb 19, 2019
@cknitt cknitt deleted the use-url branch February 19, 2019 20:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants