I have an App component that is connected to a redux store:
import React from 'react'
import { connect } from 'react-redux'
function App(props) {
return (
<div>
{props.children}
</div>
);
}
function mapStateToProps(state) {
return {
todos: state.something
}
}
export default connect(mapStateToProps)(App)
App is the root level component in my react router hierarchy and so receives all child routes as children:
export default(
<Route component={App} path="/">
<IndexRoute component={Home} />
<Route component={PageNotFound} path="*" />
</Route>
);
I want to be able to pass the props that are passed in to App
via mapStateToProps
to child components. It seems the accepted way to do this is to use cloneElement
to allow props to be passed to children (How to pass props to {this.props.children}). So the code in the first snippet above:
<div>
{props.children}
</div>
becomes:
<div>
{React.cloneElement(props.children, { ...props }
</div>
I find this rather ugly because it means I am blindly passing all props from my connected app component in to my children route components.
Is this really the accepted way of doing this?
connect
yourHome
or other components like you did with yourApp
component and have a differentmapStateToProps
function in each. – Mario Tacke