Answer the question
In order to leave comments, you need to log in
Why is the component from the HOC not being rendered?
Hello everyone, there is such a code HOC'a.
import { useSelector } from "react-redux";
import LoginPage from "../containers/LoginPage/LoginPage";
const WithAuth = (ProtectedComponent) => {
const isAuth = useSelector((store) => store.userReducer.isAuth);
const redirect = !isAuth;
return (props) => {
if (redirect) {
return <LoginPage />;
}
return <ProtectedComponent {...props} />;
};
};
export default WithAuth;
const StartPageWithAuth = WithAuth(<StartPage />);
return (
<div className='App'>
<Header />
<Routes>
<Route path='/home' element={<StartPageWithAuth />} />
<Route path='/login' element={<LoginPage />} />
<Route path='/registration' element={<RegistrationPage />} />
</Routes>
</div>
);
}
return <ProtectedComponent {...props} />;
return <div>Protected Page</div>/>
Answer the question
In order to leave comments, you need to log in
Try to give the StartPage component not in the form of JSX, but simply WithAuth(StartPage);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question