Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Constructor props equivalent in React Hooks for history push

I'm trying to migrate one class component to function with hooks but i have a problem when try to change the url with my history.push.

With my class component this is the way to change the url:

  constructor(props) {

    super(props)

    this.state = {
     // GeneralValidate: false,
      paisValue: '',
      paisError: true,
      loading: false,
      tipo: '',
  };

  }


.//more code here


 this.props.history.push({
  pathname: '/Pais',
  state: { detail: 'hola' }
})

And works fine but in my function component the props its empty and i dont know how i cant use the history.push.

 const PaisForm = (props) => { 
 props.history.push("/Pais")
}

What i'm doing wrong? Thanks and sorry for the issues!

like image 735
Carlos Deseda Avatar asked Sep 26 '19 17:09

Carlos Deseda


People also ask

How do you use history PUSH IN React hooks?

“useHistory()” hook returns the history instance created by React Router, and history. push(“/profile/John”) adds the given URL to the history stack which results in redirecting the user to the given URL path. Similarly, you can use other methods and parameters of the history object as per your need.

What does props history PUSH do?

history. push() is another approach where we make use of the history props React Router provides while rendering a component. In other words, this works when the component is being rendered by React Router, bypassing the component as a Component prop to a Route.

Can you access props in constructor React?

props will be undefined in the constructor, which can lead to bugs. Typically, in React constructors are only used for two purposes: Initializing local state by assigning an object to this.

Why we use constructor props in React JS?

In React, constructors are mainly used for two purposes: It used for initializing the local state of the component by assigning an object to this. state. It used for binding event handler methods that occur in your component.


Video Answer


3 Answers

props.history and props.location are special props injected by withRouter, it works for class and functional components

import { withRouter } from 'react-router-dom'    

export const Component = withRouter(({ history, location }) =>{

})

class Comp extends Component {
    render(){
        const { location, history } = this.props
    }
}
export default withRouter(Comp)
like image 190
Dupocas Avatar answered Oct 18 '22 23:10

Dupocas


1st:

import {withRouter} from "react-router-dom"

and wrap your component

export default withRouter (PaisForm);

2nd:

or pass history object from parent if parent has access to history, like below :

<PaisForm history={this.props.history}/>

3rd:

Use useHistory hooks from router

import { useHistory } from 'react-router';

function PaisForm (props){
  const history = useHistory();

  return <button onClick={()=> history.push('/path')}>click</button>
}

Fixed typo

like image 3
Jaisa Ram Avatar answered Oct 18 '22 23:10

Jaisa Ram


You can either wrap your component with the withRouter Higher-Order-Component, which will provide history as a prop:

import { withRouter } from 'react-router-dom';

const Component = ({ history, ...props }) => {

    /* ... */

    history.push('/');

}

withRouter(Component);

Or you can use the hooks that came with the latest update (v5.1.0). With these you do not have to wrap your functional component inside a HOC.

Available hooks are useHistory (history prop), useLocation (location prop), useParams (params object of match prop) and useRouteMatch (match prop).

import { useHistory } from `react-router`;

const Component = (props) => {
    const history = useHistory();

    /* ... */

    history.push('/');
}
like image 1
Rallen Avatar answered Oct 19 '22 00:10

Rallen