0

I'm using useHistory to route my website from page A

const history = useHistory();
history.push('/this/is/page-b', { object: selectedObject });

and in page B, I have:

const history = useHistory();
const objectData = history.location?.state?.object as Object;
  • First question: Is there anything wrong with this code? Trying to pass data using history.
  • Second question: Any suggestion for another solution to pass data instead of via useHistory hook? Just need some keywords. Thank guys.
Drew Reese
  • 165,259
  • 14
  • 153
  • 181
Kyle
  • 21
  • 5

1 Answers1

1

In react router 6, use useNavigate

const navigate = useNavigate();
navigate('/this/is/page-b', { object: selectedObject });

use useLocation to catch data

const {object} = useLocation();
Sachila Ranawaka
  • 39,756
  • 7
  • 56
  • 80