【问题标题】:Pass props down in Redux React app with React Router使用 React Router 在 Redux React 应用程序中传递 props
【发布时间】:2016-04-19 14:55:41
【问题描述】:

这是我的入口点文件:

import React, { PropTypes } from 'react'
import ReactDOM from 'react-dom'
import { createStore, combineReducers } from 'redux'
import { Provider } from 'react-redux'
import createBrowserHistory from 'history/lib/createBrowserHistory'
import { Router, Route } from 'react-router'
import { syncReduxAndRouter, routeReducer } from 'redux-simple-router'
import reducers from './reducers'

import App from './containers/app.jsx'
import About from './about.jsx'
import Dashboard from './dashboard/index.jsx'

const reducer = combineReducers(Object.assign({}, reducers, {
    routing: routeReducer
}))
const store = createStore(reducer)
const history = createBrowserHistory()

syncReduxAndRouter(history, store)

const routes = {
  path: '/',
  component: App,
  childRoutes: [
    { path: 'about', component: About },
    { path: 'dashboard', component: Dashboard }
  ]
}

ReactDOM.render(
    <Provider store={store}>
        <Router routes={routes} history={history} />
    </Provider>,
    document.getElementById('app')
)

从我能收集到的信息来看,这似乎是相当标准的。但是,我对如何将道具从App 降低到例如Dashboard 组件有点困惑。这是App 组件:

import React, {PropTypes} from 'react'
import {bindActionCreators} from 'redux'
import {connect} from 'react-redux'
import {Link} from 'react-router'

import * as authActions from './../actions/auth'

class App extends React.Component {
    render() {
        return (
            <div>
                <h1>App</h1>
                <ul>
                    <li>
                        <Link to="/">Home</Link>
                    </li>
                    <li>
                        <Link to="/about">About</Link>
                    </li>
                    <li>
                        <Link to="/dashboard">Dashboard</Link>
                    </li>
                </ul>
                {this.props.children}
            </div>
        )
    }
}

App.propTypes = {
    authActions: PropTypes.object.isRequired,
    authState: PropTypes.object.isRequired
}

function mapStateToProps(state) {
    return {authState: state.authState}
}

function mapDispatchToProps(dispatch) {
    return {
        authActions: bindActionCreators(authActions, dispatch)
    }
}

export default connect(mapStateToProps, mapDispatchToProps)(App)

抱歉,代码不是更简洁,但我特别想将 authState(来自我的 Reducer)传递给 Dashboard 组件,我对它的工作原理有点困惑使用 React Router 和 redux-simple-router 库...

编辑:

我更改了Dashboard 组件以将其包含在底部:

Dashboard.propTypes = {
    authActions: PropTypes.object.isRequired,
    authState: PropTypes.object.isRequired
}

function mapStateToProps(state) {
    return {authState: state.authState}
}

function mapDispatchToProps(dispatch) {
    return {
        authActions: bindActionCreators(authActions, dispatch)
    }
}

export default connect(mapStateToProps, mapDispatchToProps)(Dashboard)

我现在可以访问 Redux 状态和操作。我不确定这是否是正确的方法,但它现在有效......

【问题讨论】:

  • 你使用的是什么版本的 react-router?你也可以用 jsx fyi 编写你的路由对象
  • @DominicTobias 1.0.2。是的,不过似乎更喜欢对象表示法!

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


【解决方案1】:

使用 react-router v1+,您可以通过 this.props.route 访问当前最深路线上的道具。

<Route path='/' component={App}>
        <IndexRoute component={Home} />
        <Route path='about' component={About} />
        <Route path='dashboard' component={Dashboard} customprop="Hello, it's me" />
        <Route path="*" component={NoMatch} />
</Route>

在您的仪表板组件中:

this.props.route.customprop => "Hello, it's me"

要访问当前路由树中的所有路由,您可以遍历this.props.routes(最深的路由在最后)。

【讨论】:

  • 啊哈,我找到了这个功能,谢谢。但我希望使用 Redux 注入的道具,即。 App中可访问的那些。我想因为 App 是我的“根”组件,它会/可以将道具传递给我的 childRoutes 组件
  • 嗯,好的,您的连接代码看起来正确,尽管您可以省略 mapDispatchToProps。如果需要,您可以直接发送操作,因为您有 this.props.dispatch。阅读推荐这种方法的第 2 句和第 3 句:rackt.org/redux/docs/api/bindActionCreators.html
  • @DominicTobias 这行得通,但如果 prop 值发生变化,React Router 会抛出错误“警告:[react-router] 你不能更改 ;它将被忽略”(我通过来自 redux 商店的道具,当这些发生变化时,我希望新道具进入我的组件,但不是因为这个警告。)
  • 如果你使用 redux,那么我建议你使用 react-redux 来传递 props @jeznag
  • 谢谢多米尼克。我意识到我是在错误地认为我只被允许将一个组件连接到商店的情况下进行操作的。已更改为将一些 smart 组件连接到商店,现在一切都很好:)
猜你喜欢
  • 1970-01-01
  • 2016-07-03
  • 2016-04-13
  • 2017-05-19
  • 1970-01-01
  • 2018-11-12
  • 1970-01-01
  • 2017-09-14
  • 1970-01-01
相关资源
最近更新 更多