【问题标题】:react-router, access a route param in the configreact-router,访问配置中的路由参数
【发布时间】:2016-05-12 21:46:59
【问题描述】:

我正在做一个 i18n 实现。我有像/:locale/rest/of/route 这样的路线,我想根据加载的语言环境传递字典。在路由配置上这样做会很酷。但我只能考虑在每个容器上这样做。但我主要的不确定性是代码重复。 (在每个容器中this.props.dictionary[this.props.route.locale])

有没有办法避免这种代码重复?

这是我的配置文件:

import React from 'react'
import ReactDOM from 'react-dom'
import { Provider } from 'react-redux'
import thunk from 'redux-thunk'
import { Router, Route, IndexRoute, Redirect } from 'react-router'
import createBrowserHistory from 'history/lib/createBrowserHistory'
import { syncReduxAndRouter } from 'redux-simple-router'
import * as containers from './app/app/containers'
import AppUser from './app/app/AppUser'
import AppCompany from './app/app/AppCompany'
import { createStore, applyMiddleware } from 'redux'
import reducers from './app/app/reducers'

const createStoreWithMiddleware = applyMiddleware(
  thunk
)(createStore)

const store = createStoreWithMiddleware(reducers)
const history = createBrowserHistory()

syncReduxAndRouter(history, store)

ReactDOM.render(
  <Provider store={store}>
    <Router history={history}>
      <Route path="/:lang" component={AppUser}>
        <IndexRoute component={containers.Landing} />

        <Route
          path="workflow"
          component={containers.Workflow} />

        <Route
          path="register"
          component={containers.Register} />

        <Route
          path="login"
          component={containers.Login} />

      </Route>
      <Redirect from="/" to="/en" />
      <Route path="/:lang/company" component={AppCompany}>

        <Route
          path="inbox"
          component={containers.Inbox} />

        <Route
          path="inbox/:caseId"
          component={containers.CaseDetail} />

      </Route>
      <Redirect from="/company/inbox" to="/en/company/inbox" />

    </Router>
  </Provider>,
  document.getElementById('root')
)

【问题讨论】:

    标签: reactjs react-router react-router-redux


    【解决方案1】:

    由于您使用的是redux,因此您可以在您的container 组件上使用react-redux 的connect 并实现类似的东西并使用this.props.currentDictionary 访问字典。

    function mapStateToProps(state, ownProps) {
        return {
            currentDictionary: state.dictionary[ownProps.route.locale]
        }
    }
    
    export default connect(mapStateToProps)(Container);
    

    【讨论】:

    • 不好的是我需要在每个容器上都这样做,有没有办法只做一次?
    【解决方案2】:

    最后最好的模式是使用组件Router中的属性createElement,这样我就不必编辑每个容器了

    函数如下:

    import React from 'react'
    
    import i18n from '../../i18n'
    
    const createElement = (Component, p) => {
      let dictionary = i18n[p.params.locale]
      if (dictionary == undefined) {
        dictionary = i18n['en']
      }
      return <Component {...p} dictionary={dictionary} />
    }
    
    export default createElement
    

    最后在你的 index.js 或你的路由文件中:

    import React from 'react'
    import ReactDOM from 'react-dom'
    import { Provider } from 'react-redux'
    import { createStore, applyMiddleware } from 'redux'
    import thunk from 'redux-thunk'
    import { Router, Route, IndexRoute, Redirect } from 'react-router'
    import createBrowserHistory from 'history/lib/createBrowserHistory'
    import { syncReduxAndRouter } from 'redux-simple-router'
    import * as containers from './app/app/containers'
    import AppUser from './app/app/AppUser'
    import AppCompany from './app/app/AppCompany'
    import reducers from './app/app/reducers'
    import createElement from './app/app/createElement'
    
    const createStoreWithMiddleware = applyMiddleware(
      thunk
    )(createStore)
    
    const store = createStoreWithMiddleware(reducers)
    const history = createBrowserHistory()
    
    syncReduxAndRouter(history, store)
    
    ReactDOM.render(
      <Provider store={store}>
        <Router history={history} createElement={createElement}> <-- NOTICE THIS LINE!!
          <Route path="/:locale" component={AppUser}>
    
            <IndexRoute component={containers.Landing} />
    
            <Route
              path="workflow"
              component={containers.Workflow} />
    
            <Route
              path="register"
              component={containers.Register} />
    
            <Route
              path="login"
              component={containers.Login} />
    
          </Route>
          <Redirect from="/" to="/es" />
          <Route path="/:locale/company" component={AppCompany}>
    
            <Route
              path="inbox"
              component={containers.Inbox} />
    
            <Route
              path="inbox/:caseId"
              component={containers.CaseDetail} />
    
          </Route>
          <Redirect from="/company/inbox" to="/es/company/inbox" />
    
        </Router>
      </Provider>,
      document.getElementById('root')
    );
    

    【讨论】:

      猜你喜欢
      • 2023-03-12
      • 2022-06-23
      • 2016-06-21
      • 2021-01-09
      • 1970-01-01
      • 1970-01-01
      • 2016-09-13
      • 2017-10-03
      • 1970-01-01
      相关资源
      最近更新 更多