reactjs - React-redux-router 不起作用

标签 reactjs react-router redux

我是 Redux 的新手。现在我尝试使用 redux-react-router,但遇到了问题。我点击了链接,但什么也没发生。我的应用程序不呈现组件,也不更改 URL。

我有包含以下代码的 app.js 文件:

import '../stylesheets/main.scss';

import React from 'react';
import { render } from 'react-dom';
import { Provider } from 'react-redux';
import { combineReducers, createStore } from 'redux';
import { Router, Route, IndexRoute, browserHistory } from 'react-router';
import { routerReducer } from 'react-router-redux';
import rootReducer from './reducers/rootReducer';

import Home from './containers/HomePage';
import RegisterPage from './containers/RegisterPage';

import 'lazysizes';

const store = createStore(
  combineReducers({
    rootReducer,
    routing: routerReducer
  })
);

const rootElement = document.getElementById('root');

render(
  <Provider store={store}>
     <Router history={browserHistory}>
      <Route path="/" component={Home}>
        <IndexRoute component={Home} />
        <Route path="foo" component={RegisterPage}/>
      </Route>
    </Router>
  </Provider>,
  rootElement
);

我有 导航 组件,供 主页 组件使用。

import React, { Component } from 'react';

import classNames from 'classnames';
import { Link } from 'react-router';

export default class Navigation extends Component {

  constructor() {
    super();

    this.state = {
      links: [
        { href: '#', isActive: true, title: 'Home' },
        { href: '/foo', isActive: false, title: 'Lorem' }
      ]
    };
  }

  render() {
    return (
      <nav className="navigation" role="navigation">
        <ul className="navigation_list" role="list">
          {this.state.links.map((link, i) => {
            const linkClass = classNames({
              link: true,
              'link-active': link.isActive
            });

            return (
              <li key={i} className="item" role="listitem">
                <Link className={linkClass} to={link.href}>{link.title}</Link>
              </li>
            );
          })}
        </ul>
      </nav>
    );
  }
}

当我点击链接时......没有任何反应。为什么?

最佳答案

您的路线定义不完全正确。试试这个:

<Router history={browserHistory}>
  <Route path="/"> <!-- No component on this line -->
    <IndexRoute component={Home} />
    <Route path="foo" component={RegisterPage}/>
  </Route>
</Router>

当您访问 foo 链接时,它会匹配该部分并加载 Home 组件。它还匹配 URL 中的“foo”,因此它尝试添加一个 RegisterPage 组件作为 Home 中的 children 属性,但该组件不会渲染 this.props.children任何地方。这就是 RegisterPage 没有被渲染的原因。

关于reactjs - React-redux-router 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35556694/

相关文章:

javascript - React 上的 componentWillReceiveProps 在触发时不会传递当前属性

javascript - 使用 React-Router 关闭 Bootstrap 模式后重定向

javascript - localStorage 值不会自动更新

Golang 与 React 路由器?

reactjs - Firebase在 Electron 应用程序中默默失败

javascript - 警告 : setState(. ..):无法在使用 redux/immutable 的现有状态转换期间更新

javascript - 有没有办法在 react 中使用 leaflet.heat ?

javascript - 如何使用 MUI React 左对齐 Item1 并右对齐 Item2 和 Item3?

reactjs - redux-saga, TypeError : (0 , _effects.takeLatest) 不是一个函数?

javascript - react-draft-wysiwyg 缺少编辑器字段输入