【问题标题】:React-Router-Redux dispatched LOCATION_CHANGE twice, remove search valueReact-Router-Redux 两次调度 LOCATION_CHANGE,移除搜索值
【发布时间】:2018-02-21 04:46:05
【问题描述】:

我正在使用与 react-router 4.x 兼容的下一个 (5.0.0-alpha.6) 版本的 react-router-redux。 我尝试在重定向到下一页时在 url 上设置参数。

每次移动到下一页。我意识到它调用了@LOCATION_CHANGE 2 次,in the second time it removes search value。如果我从按钮事件处理它,它工作正常

import { requireAuth } from '../components/AuthenticatedComponent'
import createHistory from 'history/createBrowserHistory'

const history = createHistory()  

return (
    <ConnectedRouter history={history}>
      <div className="container">
        <Header />
        <Switch>
          <Route exact path="/" component={Home} />
          <Route path="/about" component={requireAuth(About)} />
          <Route path="/login" component={Login} />
          <Route component={NotFound} />
        </Switch>
      </div>
    </ConnectedRouter>
  )

AuthenticatedComponent.js

import { push } from 'react-router-redux'

export function requireAuth(Component) {

class AuthenticatedComponent extends React.Component {

    componentWillMount() {
        this.checkAuth(this.props.isAuthenticated);
    }

    componentWillReceiveProps(nextProps) {
        this.checkAuth(nextProps.isAuthenticated);
    }

    checkAuth(isAuthenticated) {
        if (!isAuthenticated) {
            let redirectAfterLogin = this.props.location.pathname;
            this.props.dispatch(push({pathname: "/login", search:`?redirect=${redirectAfterLogin}`}))
        }
    }
 //....

}

【问题讨论】:

    标签: javascript reactjs redux react-redux react-router-redux


    【解决方案1】:

    以防万一有人遇到类似问题的这个问题,我正在使用connected-react-router 库,而push 导致双重LOCATION_CHANGE 事件触发。第一个事件将具有正确的搜索参数,而第二个事件将清除它。我的问题是我将一个按钮从react-router-dom 渲染为Link,如下所示:

    <Button
      as={Link}
      basic
      className={styles.basicTealButton}
      onClick={this.handleButtonClick}
    >
    ...
    

    一旦我摆脱了as={Link} 部分,一切正常。

    【讨论】:

      【解决方案2】:

      react-router-redux 不再维护。使用 React Router 4+ 满足您的 Redux 路由器同步需求。让我们改用connected-react-router 库来解决这个问题。

      【讨论】:

        【解决方案3】:

        我认为你应该在 nextProps 中传递路径名。

        class AuthenticatedComponent extends React.Component {
        
        //...
        
        componentWillReceiveProps(nextProps) {
            this.checkAuth(nextProps.isAuthenticated); // <--- call checkAuth
        }
        
        checkAuth(isAuthenticated) {
            if (!isAuthenticated) { // <-- isAuthenticated is in nextProps
        
                // this.props.location.pathame is not in nextProps
                let redirectAfterLogin = this.props.location.pathname;
                this.props.dispatch(push({pathname: "/login", search:`?redirect=${redirectAfterLogin}`}))
            }
         }
        
        //...
        

        class AuthenticatedComponent extends React.Component {
        
        componentWillMount() {
            this.checkAuth(this.props);
        }
        
        componentWillReceiveProps(nextProps) {
            this.checkAuth(nextProps); // <--- call checkAuth
        }
        
        checkAuth({isAuthenticated, location, dispatch}) {
            if (!isAuthenticated) { 
                let redirectAfterLogin = location.pathname;
                dispatch(push({pathname: "/login", search:`?redirect=${redirectAfterLogin}`}))
            }
         }
        
        //...
        

        【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-11-11
        • 1970-01-01
        • 2019-08-24
        • 2020-07-05
        • 1970-01-01
        • 2018-01-05
        • 2018-06-06
        • 2021-10-08
        相关资源
        最近更新 更多