我正在尝试在一个<Input type='select'>
上通过onChange
来改变state
和URL。(我正在使用reactstrap)
import React, {Component} from 'react'
import {
Input,
} from 'reactstrap'
export default class NewPostComponent extends Component {
constructor(props) {
super(props)
this.state = {
selected: '',
}
}
handleChange(event) {
this.setState({
selected: event.target.value,
})
}
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>
)
}
}
我正在改变state
,但问题出在改变URL上。我已经尝试过props.history.push
,但是在如下的handleChange
中:
handleChange(event) {
this.setState({
selected: event.target.value,
})
this.props.history.push(`/${this.state.selected}/new/`)
}
我遇到了以下错误:
未捕获的类型错误:无法读取未定义的属性“push”
console.log(this.props.history)
返回值为undefined
。
我需要在setState
发生后更改URL,请问有什么方法吗?
props.history.push
发生在setState
之后。 - Sreekar MouliwithRouter(NavLink);
但它可能有效。 - Tholle