react-router 3 中的 useRouterHistory(createHistory) 到了 react-router 4 變成了什么?



react-router 3 文檔: https://github.com/ReactTraining/react-router/blob/v3/docs/API.md
react-router 4 文檔: https://reacttraining.com/react-router

1. react-router 3 中的 useRouterHistory(createHistory) :

依賴: react-router,redux-simple-router

作用:useRouterHistory is a history enhancer that configures a given createHistory factory to work with React Router.
         This allows using custom histories in addition to the bundled singleton histories.
         It also pre-enhances the history with the useQueries and useBasename enhancers from history.

         useRouterHistory是一個history增強器,它將給定的createHistory工廠配置為使用React Router。
         這允許使用除了捆綁的單例(單例模式:一個類只能有一個實例存在,並且只返回一個對象)歷史之外的自定義歷史。
         它還通過歷史記錄中的useQueries和useBasename增強器預先增強了歷史history

用法:src => store => index.js

         import createHistory from 'history/lib/createHashHistory'
         import {useRouterHistory} from 'react-router'
         import {syncHistory} from 'redux-simple-router'

         export const history = useRouterHistory(createHistory)({queryKey: false});
         export const reduxRouterMiddleware = syncHistory(history);

         export default function configureStore(preLoadedState) {
              return createStore(
                   rootReducer,
                   preLoadedState,
                   applyMiddleware(..., reduxRouterMiddleware, ...)
              )
          }

       src => main.js

       import configureStore, {history, reduxRouterMiddleware} from './store'
       import App from './containers/App.js'
       export const store = configureStore()
       reduxRouterMiddleware.listenForReplays(store)

       ReactDom.render(
             <Provider store={store}>
                 <Router>
                    <Route path="/" component={App}/>
                 </Router>
             </Provider>,
            document.getElementById('root')
        )

2. react-router 4 中的 useRouterHistory(createHistory) 變成了什么 :

    react-router 4 中沒有 useRouterHistory(createHistory) 這種寫法了,取而代之的是 BrowserRouter。

   依賴: react-router (通用庫,web 和 Native 都可用),react-router-dom (web用)

   用法:src => store => index.js

            export default function configureStore(preLoadedState) {
                return createStore(
                    rootReducer,
                    preLoadedState,
                    applyMiddleware(..., ...)
                )
            }

 
           src => main.js

           import {BrowserRouter as Router, Route} from 'react-router-dom'
           import configureStore from './store'
           import App from './containers/App.js'

           export const store = configureStore()

           ReactDom.render(
               <Provider store={store}>
                   <Router>
                       <Route path="/" component={App}/>
                   </Router>
               </Provider>,
               document.getElementById('root')
            )


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM