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

Simplify state handling #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Nirei
Copy link
Collaborator

@Nirei Nirei commented May 18, 2022

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 the BrowserHistory mutable reference has been transformed to a component constant.

Fixes #4 as a side effect.

@Nirei Nirei added the enhancement New feature or request label May 18, 2022
@Nirei Nirei requested a review from hryuk May 18, 2022 22:50
@Nirei Nirei self-assigned this May 18, 2022
@Nirei Nirei force-pushed the refactor/simplify-state-handling branch from 8709f88 to e0c8327 Compare May 18, 2022 23:19
@Nirei Nirei force-pushed the refactor/simplify-state-handling branch from e0c8327 to a726c76 Compare May 18, 2022 23:24

const login = () => {
setContext({auth: {logued: true, username: 'notadmin'}});
//Optional, force refresh on login
setLocation(location);
Copy link
Collaborator Author

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(() => {
Copy link
Collaborator Author

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) {
Copy link
Collaborator Author

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>({})
Copy link
Collaborator Author

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.

Copy link
Owner

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()
Copy link
Collaborator Author

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 })
Copy link
Collaborator Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

useLocation callbacks not referentially stable
2 participants