Tengo el siguiente componente donde espero ir a otra ruta a través router's push method immediately cos router push is inside the gancho useEffect`.
Pero el empuje del enrutador nunca parece suceder. El componente ShowLoading es solo una pantalla de carga (muestra un control giratorio de carga).
La página está atascada en la pantalla giratoria de carga.
¿Qué estoy haciendo mal? ¿Por qué no me envían a la nueva página? Consejo por favor. Gracias.
import React from 'react'; import Cookies from 'js-cookie'; import { ShowLoading } from '../../ShowLoading'; import { withRouter } from 'react-router'; const MyComponent = ({ router: { push, location }, }) => { React.useEffect(() => { // this cookie gets set thus clearly we are entering this useEffect. Cookies.set('temp', '1'); const value = 'test'; const params = location.search ? `${location.search}` : ''; // seems like this has no effect, no pushing and moving to new page path. push(`/${value}/my_path${params}`); }, []); return (<ShowLoading />); }; export default (withRouter(MyComponent);PD: va a una nueva ruta como se esperaba si hago lo siguiente pero busco hacerlo a través de un enrutador.
window.location.pathname = `/${value}/my_path${params}`;Puede obtener match , history y location como accesorios cuando usa withRouter . Entonces puede obtener el impulso de la history de esta manera:
import React from 'react'; import Cookies from 'js-cookie'; import { ShowLoading } from '../../ShowLoading'; import { withRouter } from 'react-router'; const MyComponent = ({ history, location }) => { React.useEffect(() => { // this cookie gets set thus clearly we are entering this useEffect. Cookies.set('temp', '1'); const value = 'test'; const params = location.search ? `${location.search}` : ''; history.push(`/${value}/my_path${params}`); }, []); return (<ShowLoading />); }; export default withRouter(MyComponent);