更新历史记录

时间:2020-07-10 11:29:21

标签: reactjs react-router-dom history

我已将历史记录包更新到最新版本,但出现此错误:

TypeError: setting getter-only property "location"
./modules/configureHistory.js/<
src/modules/configureHistory.js:2

  1 | // @flow
> 2 | import { createBrowserHistory } from 'history'
  3 | import qs from 'qs'
  4 | 
  5 | const history = createBrowserHistory()

有关此代码: history.js

// @flow
import { createBrowserHistory } from 'history'
import qs from 'qs'

const history = createBrowserHistory()

history.location = {
  ...history.location,
  query: qs.parse(history.location.search.substr(1)),
  state: {},
}

/* istanbul ignore next */
history.listen(() => {
  history.location = {
    ...history.location,
    query: qs.parse(history.location.search.substr(1)),
    state: history.location.state || {},
  }
})

const { go, goBack, push, replace } = history

export { go, goBack, push, replace }
export default history

我这样重定向用户:

PrivateRoute.jsx

import React from 'react'
import PropTypes from 'prop-types'
import { Route, Redirect } from 'react-router-dom'

const RoutePrivate = ({ component: Component, isAuthenticated, to, ...rest }) => (
  <Route
    {...rest}
    render={props =>
      isAuthenticated ? (
        <Component {...props} />
      ) : (
        <Redirect
          to={{
            pathname: to,
            state: { redirect: props.location.pathname, isAuthenticated },
          }}
        />
      )
    }
  />
)

RoutePrivate.propTypes = {
  component: PropTypes.oneOfType([PropTypes.func, PropTypes.object]).isRequired,
  isAuthenticated: PropTypes.bool.isRequired,
  location: PropTypes.object,
  to: PropTypes.string,
}

RoutePrivate.defaultProps = {
  to: '/',
}

export default RoutePrivate

因此,如果该私人路由未登录,则会在/上重定向我的用户。

我找不到新的解决方案来重定向具有历史记录位置的私有路由。新版本的历史记录包有什么解决方案?

0 个答案:

没有答案