4.0 中参数后面加问号,表示可选参数:
import { HashRouter, Route, Switch } from 'react-router-dom';
class App extends Component {
constructor() {
super();
}
render() {
return (
<HashRouter>
<Switch>
<Route path="/" exact component={Home} />
<Route path="/city" component={City} />
<Route path="/Search/:category/:keyword?" component={Search} />
<Route path="/User" component={User} />
<Route path="*" component={NotFound} />
</Switch>
</HashRouter>
);
}
}
withRouter可以包装任何自定义组件,将react-router-dom 的 history,location,match 三个对象传入。
import { withRouter } from 'react-router-dom';
class Test extends Component {
...
render(){
const { match, location, history } = this.props
...
}
}
export default withRouter(Test);
//export default withRouter(connect(...)(Test))//with redux