我正在尝试以编程方式使用路由,this.props.history.push(..)
但它似乎不起作用。
这是路由器:
import {
BrowserRouter as Router,
Route
} from 'react-router-dom';
<Router>
<Route path="/customers/" exact component={CustomersList} />
<Route path="/customers/:id" exact component="{Customer} />
</Router>
在 CustomerList 中,呈现客户列表。单击客户 (li) 应该使应用程序路由到客户:
import { withRouter } from 'react-router'
class Customers extends Component {
static propTypes = {
history: PropTypes.object.isRequired
}
handleCustomerClick(customer) {
this.props.history.push(`/customers/${customer.id}`);
}
render() {
return(
<ul>
{ this.props.customers.map((c) =>
<li onClick={() => this.handleCustomerClick(c)} key={c.id}>
{c.name}
</li>
</ul>
)
}
}
//connect to redux to get customers
CustomersList = withRouter(CustomersList);
export default CustomersList;
代码是部分的,但完美地说明了情况。发生的情况是浏览器的地址栏根据 history.push(..) 发生变化,但视图没有更新,Customer 组件没有呈现,CustomersList 仍然存在。有任何想法吗?