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

Refactor context so its has less hacks #30

Merged
merged 6 commits into from
Mar 18, 2019

Conversation

eightypop
Copy link
Contributor

addresses one of the things for 4.0.0 release #29

@@ -26,15 +23,33 @@ export default function SwitcherProvider(props) {
};
}, []);

const providedMethods = useMemo(() => {
Copy link
Owner

Choose a reason for hiding this comment

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

why not useCallback here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

that was my initial implementation actually. but it seemed better to memo the object passed into the provider so it wouldn't cause the consumers to rerender if the provider rerendered. so i just stuck everything in one memo to make it simpler.

case 'hashchange':
return hashChangeListeners.current;
default:
throw new Error('womp womp');
Copy link
Owner

Choose a reason for hiding this comment

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

nit: lets make this error more helpful even if it technically should never be hit.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

lol that's not a nit. thats just a good pr comment

@@ -1,14 +1,5 @@
import { Children } from 'react';

// http://stackoverflow.com/a/2117523
export function generateGuid() {
Copy link
Owner

Choose a reason for hiding this comment

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

YES!

Copy link
Owner

Choose a reason for hiding this comment

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

I won't miss any of this code

load && window.addEventListener('load', handler);
pushState && window.addEventListener('popstate', handler);
hashChange && window.addEventListener('hashchange', handler);
if (usingProvider) {
Copy link
Owner

Choose a reason for hiding this comment

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

this inversion makes the code so much better! I love it.

What do you think of ALWAYS requiring the provider? such that the second case is never even possible?

I think lots of other libraries do this. Might be worth making some of these breaking changes before the next 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.

it is kind of nice to be able to throw in one component but i think i agree it doesn't scale well if i have multiple switchers rendered in one tree. but i'm not sure how often that case would occur.

Copy link
Owner

Choose a reason for hiding this comment

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

cool, we won't do that in this PR, but maybe before the release

Copy link
Owner

@jdlehman jdlehman left a comment

Choose a reason for hiding this comment

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

Looks great!

@jdlehman jdlehman merged commit 0f79a75 into jdlehman:master Mar 18, 2019
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.

2 participants