react route跳转+传参
是这样的。
react小白一只,最近写了一个react项目,类似百度那样,首页搜索,然后跳转三个子页面,需要将关键字传到三个子页面,并且三个页面互相跳转时不丢失关键字。
可能会想到使用redux、mobx这些技术,但刚入门还是从最简单的实现开始不容易蒙蔽。这里介绍react route实现上图需求,简单粗暴,没有太多的陌生操作。
直接上实现的代码说话(只截取有关路由部分):
首先是路由(star/fans/friends是三个子页面):class MainRouter extends Component {
render() {
return (
);
}
}
然后是menu点击跳转的菜单栏(被引用于每个子页面组件中)组件:class Menubar extends Component {
constructor(props) {
super(props);
this.state = {
kw: this.props.kw
};
}
render() {
return (
selectedKeys={[this.state.current]}
mode="horizontal"
>
全网趋势
粉丝圈
朋友圈
);