mrmilu / react.base

Mr Milú's official starter template for React.js projects.
5 stars 0 forks source link

RouteMiddleware component does not work - it crashes when props change #6

Closed arnaugomez closed 1 year ago

arnaugomez commented 1 year ago

The problem with the RouteMiddleware component is that it takes a React hook as props.

When the props change, a React error is thrown, because the order of the hooks has been changed.

My solution (which I have implemented successfully in the SecretAligner React project) is to create each middleware as a separate React component, instead of thinking of them as hooks.

This has further benefits like, for example, being able to combine middlewares, one inside another.

hazzo commented 1 year ago

Could you give me an example? Why would change the order of the hooks on prop change?

hazzo commented 1 year ago

@arnaugomez I'll close this issue in favor of using keys in the router middleware component. Creating a a middleware component for each middleware it would be now more complex because the middleware usage has changed and has more logic. If we see that this error persists we should find a more composable approach but ditching hooks as props. Also I encountered this error¡, but only happened on hot reload so it will not match a real use case on a production build. Thanks 🙏