In react, one can use the <Link> component to change the URL when the user clicks on the <Link>.
However, this is not what I want.
I need to change the URL when a useState value changes. So I need to change the URL inside of a useEffect.
How can this be done?
>Solution :
If you want to use dynamic URL use can use below approach
const [link, setLink] = useState('/profile');
useEffect(() => {
// logic
setLink('/logout');
}, [state you want]);
return (
<div>
<Link to={link} />
</div>
);
or you want to go to a URL after some logic
const history = useHistory();
useEffect(() => {
// logic
history.push('/profile');
}, [some state])
To use useHistory you want import like import { useHistory } from "react-router-dom";