javascript - React Router 4 和 props.history.push

标签 javascript reactjs react-router react-redux mern

React 中有一些东西让我抓狂,我需要你的帮助。基本上,当用户单击“我的个人资料”时,我想重定向到用户的个人资料。为此,我使用以下代码片段

viewProfile = () => {
    console.log('My USER ID: ', this.props.userId);
    this.props.history.push(`/profile/${this.props.userId}`);
}

这应该有效。但是,尽管单击“我的个人资料”时 URL 会更改为正确的 URL,但该页面并未显示。它只是保留为旧页面。

谷歌搜索了一段时间后,我知道这与 redux 有关......但是,我找不到解决方案。 (this.props.userId 来自布局组件,该组件又从 redux 存储中获取)

这是我的代码:

// React
import React, { Component } from 'react';
import { withRouter } from 'react-router-dom';
// Redux
import { connect } from 'react-redux';
import * as actions from '../../../store/actions/';
// Containers and Components
   ...// more imports

const styles =  // styles

class NavBar extends Component {

  handleAuthentication = () => {
    if (this.props.isAuthenticated) {
      this.props.history.push('/logout');
    } else {
      this.props.history.push('/login');
    }
  };

  viewProfile = () => {
    console.log('My USER ID: ', this.props.userId);
    this.props.history.push(`/profile/${this.props.userId}`);
  };

  render() {
    let buttons = (
      <Auxillary>
        {this.props.isAuthenticated ? (
          <RaisedButton
            label="MY PROFILE"
            primary={true}
            style={styles.button}
            onClick={this.viewProfile} // THIS IS THE METHOD
          />
        ) : null}
        <RaisedButton
          backgroundColor={grey900}
          label={this.props.isAuthenticated ? 'LOGOUT' : 'LOGIN'}
          labelColor={grey50}
          style={styles.button}
          onClick={this.handleAuthentication}
        />
      </Auxillary>
    );

    let itemSelectField = null;
    if (this.props.location.pathname === '/items') {
      itemSelectField = (
        <ItemSelectField
          onSelectTags={tags => this.props.filterItemsByTagName(tags)}
        />
      );
    }
    let bar = null;
    if (
      this.props.location.pathname !== '/login' &&
      this.props.location.pathname !== '/register'
    ) {
      bar = (
        <AppBar
          style={styles.appBar}
          title={itemSelectField}
          iconElementLeft={
            <img style={styles.logoHeight} src={Logo} alt="logo" />
          }
          iconElementRight={buttons}
        />
      );
    }
    return <Auxillary>{bar}</Auxillary>;
  }
}

const mapDispatchToProps = dispatch => {
  return {
    filterItemsByTagName: selectedTags =>
      dispatch(actions.filterItemsByTagName(selectedTags))
  };
};

export default withRouter(connect(null, mapDispatchToProps)(NavBar));

您可以在此处找到整个应用程序:https://github.com/Aseelaldallal/boomtown/tree/boomtown-backend-comm

我要疯狂地尝试解决这个问题。帮助。

最佳答案

我认为与您使用<BrowserRouter>有关。它创建自己的历史实例,并监听其变化。因此,不同的实例将更改 url,但不会更新 <BrowserRouter> 。相反,您可以使用 ConnectedRouter并将历史记录实例传递为

import createHistory from 'history/createBrowserHistory';
...
const history = createHistory();
...
<ConnectedRouter history={history}>
...
</ConnectedRouter>

关于javascript - React Router 4 和 props.history.push,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48799733/

相关文章:

javascript - 使用 JavaScript Ajax 从另一个站点检索内容

javascript - 数据表根据按钮单击事件隐藏和显示行

html - 阻止 Prettier 格式化程序将 React JSX 文件中的每个 HTML 标签放在自己的行上?

javascript - 使用 mapbox 和 react context 时防止过度重新渲染

javascript - 在 React 中使用 map 函数时出错

javascript - 如何从谷歌地图中的集群中分离出一个特殊标记?

javascript - 如何将此 js.erb 更改为 haml

reactjs - 混淆react-router-dom和react-router-redux

javascript - Redux 和 react 路由器 : Provider bug

reactjs - 保护基于react/react-router构建的单页面应用程序