【问题标题】:How do I find out if it's possible to goBack() or goForward() using react-router?如何确定是否可以使用 react-router 执行 goBack() 或 goForward()?
【发布时间】:2021-01-20 04:12:11
【问题描述】:

如果用户已经导航到另一条路线,他可以导航回上一页。 如果用户已经导航回来,他可以向前导航到上一页。

react-nativereact-nativation 提供了 useNavigation 钩子,所以我可以简单地执行:

import React from 'react'
import { useNavigation} from 'react-navigation'

function Test()
{
    const navigation = useNavigation()
    return (
       
        <div>
            { navigation.canGoBack() && <button onClick=(() => navigation.goBack())>Go Back</button>
        </div>
        
    )
}

ReactJS 中,react-router 提供了useHistory 钩子,所以我可以执行history.goBack() 但不能执行history.canGoBack()

import React from 'react'
import { useHistory } from 'react-router'

function Test()
{
    const history = useHistory()
    return (
       
        <div>
            { history.canGoBack() && <button onClick=(() => history.goBack())>Go Back</button>

            {/* history.canGoBack() is not a function  */}

        </div>
        
    )
}

react-router 是否提供了让我们知道我们是否可以返回或前进的方法?

【问题讨论】:

标签: reactjs react-native react-router


【解决方案1】:
import {History} from 'react-router';
var canGoBack = (History.length > 1);

你能试试这个吗?我认为这可能会解决问题。

【讨论】:

  • 是的,这可以很好地导航回来。前锋呢?两者都必须有更好的方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-11
  • 2019-09-12
相关资源
最近更新 更多