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!
“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.
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.
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.
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.
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)
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
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('/');
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With