编程式导航
每一个使用了路由的组件都会被Route包裹起来,每个被Route包裹的组件都会多出几个对象,通过this.props查看
import React,{Component} from 'react';
class List extends Component{
push = () => {
// push会将浏览信息存进浏览器中,replace则不会
this.props.history.push('/home')
// this.props.history.replace('/Home')
}
render () {
let his = this.props.history
return (
<div>
<button onClick = { this.push }>点击返回首页</button> //push只能使用事件触发,直接在这里写带上括号会导致一直触发
<button onClick = { his.goBack.bind() }>点击返回上一级</button> //goBack可以使用.bind(),如果不是用的话会自动触发函数,导致一直跳转
</div>
)
}
}
export default List