Enviar datos a través de rutas en React
const navigate = useNavigate();
navigate('/other-page', { state: { id: 7, color: 'green' } });
Technical Heaven
const navigate = useNavigate();
navigate('/other-page', { state: { id: 7, color: 'green' } });
// React Router v6
// pass data between routes
// ---------------------------------------------------------------------
// sender.js/jsx
import { useNavigate } from "react-router-dom";
const navigate = useNavigate();
navigate('/other-page', { state: { username: 'user', password: '696' } });
// ---------------------------------------------------------------------
// receiver.js/jsx
import { useLocation } from "react-router-dom";
const location = useLocation();
console.log(location.state) // gives: {username: 'user', password: '696'}
const {state} = useLocation();
const { id, color } = state; // Read values passed on state
<Route path="/" component={() => <Search name={this.props.name} />} />
render={routeProps => <Search name={this.props.name} {...routeProps} />}