用于历史推送的 React Hooks 中等效的构造函数道具

Car*_*eda 6 javascript reactjs

我正在尝试迁移一个类组件以使用钩子运行,但是在尝试使用我的 history.push 更改 url 时遇到问题。

使用我的类组件,这是更改 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' }
})
Run Code Online (Sandbox Code Playgroud)

并且工作正常,但在我的函数组件中,道具为空,我不知道如何使用 history.push。

 const PaisForm = (props) => { 
 props.history.push("/Pais")
}
Run Code Online (Sandbox Code Playgroud)

我在做什么错?感谢和抱歉的问题!

Dup*_*cas 8

props.history并且props.locationprops由特殊注入的withRouter,它适用于classfunctional 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)
Run Code Online (Sandbox Code Playgroud)