简体   繁体   English

react-router-redux 将历史传递给中间件

[英]react-router-redux pass history to middleware

I have the following files:我有以下文件:

configureStore.js

import { createStore, applyMiddleware, compose, combineReducers } from 'redux'
import { routerReducer } from 'react-router-redux'
import middleware from './middleware'

export default (initialState) => createStore(
  combineReducers({
    ...
    router: routerReducer
  }),
  initialState,
  compose(applyMiddleware(...middleware))
)

middleware.js

import { routerMiddleware } from 'react-router-redux'

export default [
  ...
  routerMiddleware(/* How do I access history here? */) //<-- history
]

index.js

import React from 'react'
import ReactDOM from 'react-dom'
import { Provider } from 'react-redux'
import { ConnectedRouter as Router } from 'react-router-redux'
import { createBrowserHistory } from 'history'
import App from './App'
import configureStore from './configureStore'

const history = createBrowserHistory()

ReactDOM.render(
  <Provider store={configureStore()}>
    <Router history={history}> //<-- history
      <App />
    </Router>
  </Provider>,
  document.getElementById('root')
)

Versions: "history": "4.7.2", "react-redux": "5.0.6", "react-router-redux": "5.0.0-alpha.8", "redux": "3.7.2"版本: "history": "4.7.2", "react-redux": "5.0.6", "react-router-redux": "5.0.0-alpha.8", "redux": "3.7.2" ”

So I must pass the history to the <Router> and to the routerMiddleware() .所以我必须将历史记录传递给<Router>routerMiddleware() Is there an easy way to access the created history in middleware.js ?有没有一种简单的方法可以访问middleware.js创建的历史记录?

A simple answer is no.一个简单的答案是否定的。 But in JS everything is possible even if it's not a good thing.但在 JS 中,即使不是一件好事,一切皆有可能。 You can pass in history to a function that then returns the router middleware.您可以将历史传递给一个函数,然后该函数返回路由器中间件。

// middleware.js

import { routerMiddleware } from "react-router-redux";


let createMiddleWare = ({history}) => { [
  routerMiddleware(history); //<-- history
])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM