我正在學習Redux,並且在react-router-dom中的新變化讓我有點困惑。 我有這些文件:React Redux和react-router-dom
Index.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'),
);
App.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 /列表,或者如果我點擊它的作品。
但是我有我做錯了什麼的感覺和正確的方法應該是在包裝的內部index.js
所以我應該做這樣的事情:
<Router>
<Provider store={store}>
<App />
</Provider>,
</Router>
的document.getElementById( '根'),
然後在Main.js
<div className="wrapper">
<Header />
<Route exact path="/" component={Listing} />
<Route path="/list" component={SingleListing} />
<Footer />
</div>
然而,當我這樣做,如果我直接去鏈接本地主機/列表我可以看到組件,但如果我碰到任何鏈接什麼都沒有發生。
我很困惑如何正確使用路由器與REDX。 這是我使用的減壓器裏面的代碼/ 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上,但如果我在我的代碼做正確我不明白,我知道它的工作原理,但我有興趣學習如果這是正確的方法。 謝謝!
感謝您的明確解釋!:) –
隨時!如果您發現這可以解答您的問題,您能否將其標記爲答覆?謝謝! –
在你的App.js文件瀏覽器歷史應該在符號''<路由器路由器='路由器'歷史='browserHistory'>。我正在糾正這一點,因爲我愛你的答案:) –