10

我正在学习 Redux,随着 react-router-dom 的新变化,我有点困惑。我有这些文件:

索引.js

import React from 'react';
import { Provider } from 'react-redux';
import ReactDOM from 'react-dom';
//  import { AppContainer } from 'react-hot-loader';
import App from './containers/App';
import store from './store';

ReactDOM.render(
  <Provider store={store}>
    <App />
  </Provider>,
  document.getElementById('root'),
);

应用程序.js

import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';
import * as actionCreators from '../actions/actionCreators';
import Main from '../components/Main';

function mapStateToProps(state) {
  return {
    search: state.search,
    navigation: state.navigation,
  };
}
export function mapDispatchToProps(dispatch) {
  return bindActionCreators(actionCreators, dispatch);
}
const App = connect(mapStateToProps, mapDispatchToProps)(Main);
export default App;

最后,Main.js

import React from 'react';
import { BrowserRouter as Router, Route } from 'react-router-dom';
import Header from './Header';
import Footer from './Footer';
import Listing from './Listing';
import SingleListing from './SingleListing';
const Main = () => (
  <Router>
    <div className="wrapper">
     <Header />
      <Route exact path="/" component={Listing} />
      <Route path="/list" component={SingleListing} />
      <Footer />
    </div>
  </Router>
);

export default Main;

这段代码工作得很好,当我去 localhost:3000/list 或者如果我点击它就可以了。

但是我觉得我做错了什么,正确的方法应该是包裹到里面的 index.js

所以我应该做这样的事情:

<Router>
  <Provider store={store}>
    <App />
  </Provider>,
</Router>   

document.getElementById('root'),

然后在 Main.js

<div className="wrapper">
  <Header />
  <Route exact path="/" component={Listing} />
  <Route path="/list" component={SingleListing} />
  <Footer />
</div>

但是,当我这样做时,如果我直接访问 localhost/list 链接,我可以看到该组件,但如果我点击任何链接,则不会发生任何事情。

我对如何正确使用带有 redux 的路由器感到困惑。这是我在 reducers/index.js 中使用的代码

import { combineReducers } from 'redux';
import { routerReducer } from 'react-router-redux';
import search from './search';
import navigation from './navigation';

const rootReducer = combineReducers({ search, navigation, routing: routerReducer });

export default rootReducer;

我在网上和 youtube 上研究了这个网站和许多教程,但我不明白我的代码是否正确,我知道它有效,但我有兴趣了解这是否是正确的方法。谢谢!

4

1 回答 1

6

是的,您正在正确编写路由器。你的担心是可以理解的。直到 4.0.0 版本,react-router才有了不同的方式来声明路由。您将像在第二个示例中一样定义路由器,但不是在组件中定义路由,而是在路由器中定义路由。react-router这是4.0.0 之前的示例:

应用程序.js:

//<boilerplate imports>

import Home from './components/Home';
import router from './router';
import './index.css';

ReactDOM.render(
  <Router router='router' history='browserHistory'>
  </Router>,
  document.getElementById('root')
);

路由器.js:

//<boilerplate imports>

import Photographer from './components/photographer/Photographer';
import Developer from './components/developer/Developer';
import Home from './components/Home';

export default (
    <Route path="/">
        <IndexRoute component={Home} />
        <Route path="photographer" component={Photographer} />
        <Route path="developer" component={Developer} />
    </Route>
);

您将在一个地方定义要使用的路由器和所有路由、子路由和组件,并且最终将其提供给ReactDOM.render()

话虽如此,第一种做事方式是正确的,并且是 React Router 背后的人们期望人们使用接口的方式。

关于最后一个问题,您是否在使用该代码时遇到错误react-router-redux?看看这个以供参考,它似乎是正确的。

于 2017-05-06T13:26:16.867 回答