Javascript Reactjs-修改状态并更改URL onChange
我正在尝试在Javascript Reactjs-修改状态并更改URL onChange,javascript,reactjs,url,Javascript,Reactjs,Url,我正在尝试在上更改状态和URLonChange。 (我正在使用reactstrap) 这就是我得到的错误 未捕获的TypeError:无法读取未定义的属性“push” console.log(this.props.history)未定义 我只需要在设置状态发生后更改URL的方法。历史道具只传递给路由组件的组件 {/* The Login component will get the history prop */} <Route path="/login" component={Login
上更改状态和URLonChange
。
(我正在使用reactstrap)
这就是我得到的错误
未捕获的TypeError:无法读取未定义的属性“push”
console.log(this.props.history)
未定义
我只需要在设置状态
发生后更改URL的方法。历史
道具只传递给路由
组件的组件
{/* The Login component will get the history prop */}
<Route path="/login" component={Login} />
谢谢你。这也解决了很多其他问题。我还希望
道具。历史记录。只有在设置状态
发生后才能推送
。同样的技术可以应用到问题上吗?@SreekarMouli我从未尝试过路由器(NavLink)的代码>但是它可能有用,是的。我正在更新那个帖子,请看一下,谢谢
handleChange(event) {
this.setState({
selected: event.target.value,
})
this.props.history.push(`/${this.state.selected}/new/`)
}
{/* The Login component will get the history prop */}
<Route path="/login" component={Login} />
class NewPostComponent extends Component {
state = {
selected: ''
}
handleChange(event) {
this.setState({
selected: event.target.value,
}, () => {
this.props.history.push(`/${this.state.selected}/new/`)
})
}
render() {
return (
<Input type='select' onChange={(e) => handleChange(e)}>
<option>option 1</option>
<option>option 2</option>
<option>option 3</option>
<option>option 4</option>
</Input>
)
}
}
export default withRouter(NewPostComponent)