4-1 Link

  1. class Home extends Component {
  2. constructor(props){
  3. super(props);
  4. this.state = {
  5. id:1001
  6. }
  7. }
  8. render() {
  9. return (
  10. <div>
  11. 主页
  12. <Link to={`/detail?id=${this.state.id}`}>
  13. <Button>detail</Button>
  14. </Link>
  15. </div>
  16. );
  17. }
  18. }

4-2 事件跳转 this.props.history.push()

  1. <Button onClick={this.handleToggle}>detail</Button>
  2. handleToggle=()=>{
  3. this.props.history.push(`/detail?id=${this.state.id}`)
  4. }

4-3 query-string解析get传值

  1. //Detail.js
  2. //this.props.location.search
  3. //安装依赖
  4. yarn add query-string
  5. import queryString from 'query-string'
  6. class Detail extends Component {
  7. ...
  8. componentDidMount() {
  9. var url = this.props.location.search;
  10. console.log(queryString.parse(url))
  11. }
  12. }
  13. export default Detail;