score:0

Accepted answer

react-router-dom ships with the two components { navigate, usenavigate }. if you want to use the navigate component, you can just import it and then use it.

import { navigate } from 'react-router-dom' // import it directly
    
const privateroute = ({ children }) => {
  const { user } = useauthcontext();

  if (user) {
    return children;
  } else {
    return <navigate to="/login" />
  }
};

score:1

usenavigate returns a function and not a component.

you probably want something like

const privateroute = ({ children }) => {
  const { user } = useauthcontext();
  const navigate = usenavigate();

  if (!user) {
    navigate("/login");
    return <></>;
  }

  return children;
};

Related Query

More Query from same tag