与 Typescript 反应:“历史"类型上不存在“推送"属性 [英] React with Typescript: Property 'push' does not exist on type 'History'
问题描述
我试图通过推入历史对象来离开视图.但是,当我尝试将路由推入其中时,我收到一条错误消息:
历史"类型不存在推送"属性.
渲染(){返回 (<div className="loginWrapper">withRouter(({history}) => (<button onClick={()=>{history.push('/home')}} className="btn btn-primary">Pieteikties</button>))
)}
我该怎么做才能解决这个问题?
我也试过这个:
登录(){this.props.history.push('/new-location')}
具有这样的组件:
渲染(){返回 (<div className="loginWrapper"><button onClick={this.logIn} className="btn btn-primary">Pieteikties</button>
)}
它没有用.
你在哪里定义 history
在这里?有一个全局的 window.history
这是一个网络标准.如果您想使用 react-router 历史,它会作为道具传递.试试 props.history.push()
代替.
组件必须从 react 路由器接收 history
道具.所以它应该是 Route
组件的直接子组件,例如,或者你必须通过它的父组件传递 props.
I'm trying to navigate away from a view by pushing into the history object. However when I try to push a route into it, I get an error message:
Property 'push' does not exist on type 'History'.
render(){
return (
<div className="loginWrapper">
withRouter(({history}) => (<button onClick={()=>{history.push('/home')}} className="btn btn-primary">Pieteikties</button>))
</div>
)
}
What can I do to fix this?
EDIT:
I also tried this:
logIn(){
this.props.history.push('/new-location')
}
with a component like this:
render(){
return (
<div className="loginWrapper">
<button onClick={this.logIn} className="btn btn-primary">Pieteikties</button>
</div>
)
}
And it didn't work.
Where did you define history
here? There is a global window.history
which is a web standard. If you want to use the react-router history, it's passed as a prop. Try props.history.push()
instead.
The component must receive the history
prop from react router. So it should be a direct child of a Route
component, for example, or you must pass down props via its parents.
这篇关于与 Typescript 反应:“历史"类型上不存在“推送"属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!