-
Notifications
You must be signed in to change notification settings - Fork 2
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
Simplify state handling #5
base: master
Are you sure you want to change the base?
Conversation
8709f88
to
e0c8327
Compare
e0c8327
to
a726c76
Compare
|
||
const login = () => { | ||
setContext({auth: {logued: true, username: 'notadmin'}}); | ||
//Optional, force refresh on login | ||
setLocation(location); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No longer necessary to do manual refresh, since we are now fully reactive to changes in context.
|
||
// Listen to location changes | ||
useEffect(() => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This effect is now the getComponentFromRoute
method.
|
||
if (!route) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will help catch bugs in client applications.
}, []); | ||
export const Router = ({ routes, children }: IRouterProps) => { | ||
const [location, setLocation] = useState<string>(browserHistory.location.pathname) | ||
const [context, setContext] = useState<object>({}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I minimized the state necessary to run the router.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice! With this every change in context will trigger the router logic. The only concern I have is if it could be useful to change the router context without triggering a reroute?
routedElement: undefined, | ||
forceRefresh: 0 | ||
}; | ||
const browserHistory = createBrowserHistory() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This now lives in a constant since the only reason for it being a ref was because it lived inside the component so it needed to mutate once. Now it's a singleton for the life of the module.
|
||
}, [state.location, state.forceRefresh]); | ||
const params = Path.parse(route.path, location) | ||
const component = getComponentFromRoute(route, { location, setLocation, context, setContext, params }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Both this and the route computation could be memoized but I think every re-render will need to recompute it anyway so I think it's safe to just roll with it.
On very heavy routings (hundreds of thousands of routes times hundreds of thousands of guards per route, this could be slow) but if the case ever comes up we could just optimize it.
This greatly simplifies the
Router
component. Every calculated property has been removed from state. State has been separated into its individual parts (with the added benefit of referentially stable setters provided by default by React). Simplificable effects have been transformed to normal computations and theBrowserHistory
mutable reference has been transformed to a component constant.Fixes #4 as a side effect.