Answer the question
In order to leave comments, you need to log in
Why doesn't my route in React work?
I have my own SharedRoute route, which is successfully imported into the app, wrapped with a switch and configured (the path, layout, component are specified), but for some reason it renders an empty page, I don’t understand the reason at all, maybe I made a mistake somewhere? thanks for the help.
SharedRoute:
export const SharedRoute = ({ component: Component, layout: Layout, componentProps, layoutProps, ...rest }) => {
const encloseInErrorBoundary = props => (
<ErrorBoundary>
{Layout ?
<Layout { ...layoutProps }>
<Component { ...componentProps }{ ...props }/>
</Layout>
:
<Component {...componentProps} { ...props } />
}
</ErrorBoundary>
);
// if (!Component) throw new Error('A component needs to be specified for path ${(rest).path}');
return <GuardedRoute { ...rest } render={ encloseInErrorBoundary } />;
};
export default SharedRoute;
function App() {
const isAuth = useSelector(state => state.user.isAuth)
const dispatch = useDispatch()
useEffect(() => {
dispatch(auth())
}, [])
return (
<>
<BrowserRouter>
<Switch>
<SharedRoute path="/" exact layout={ Layout } component={ Navbar } />
<SharedRoute
path="/login"
component={Login}
exact
/>
<SharedRoute
component={Registration}
path="/registration"
/>
<GuardedRoute path="/page" component={NewPage} auth={isAuth} />
</Switch>
</BrowserRouter>
</>
);
}
export default App;
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question