Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.2k views
in Technique[技术] by (71.8m points)

react-router在hash模式下如何销毁部分已经push的路由

react-router在hash模式下

我现在从第一个页面push到了第五个页面, 然后我现在想跳回第一个页面的同时, 将第二到第五个页面的路由全部销毁, 让用户无法点击前进按钮向前跳转, 这样的话需要怎么销毁呢?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

// jsx 模式

<Redirect to="/somewhere/else" />

// event 模式

import React from 'react'
import { Redirect } from 'react-router-dom'class MyComponent extends React.Component { state = {
    redirect: false
  } setRedirect = () => {
    this.setState({
      redirect: true
    })
  } renderRedirect = () => {
    if (this.state.redirect) {
      return <Redirect to='/target' />
    }
  } render () {
    return (
       <div>
        {this.renderRedirect()}
        <button onClick={this.setRedirect}>Redirect</button>
       </div>
    )
  }}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...