Closed jpeyper closed 7 years ago
My initial reaction to this was "sure, why not", but I have been thinking about it and I don't think passing on the props makes much sense.
SubspaceProvider
is designed to written in a jsx block and as such, any props can easily be provided to the children, as your workaround quite nicely shows. Implementing your suggesting might work in this case but the following snippet might behave a bit unexpectedly for someone else:
const MyAwesomeComponent = () =>
<SubspaceProvider mapState={state => state.childState} childValue='value1'>
<ChildComponent childValue='value2'/>
</SubspaceProvider>
What would you expect childValue
to equate to?
I have 2 thoughts to get around this:
SubspaceProvider
to be around the Route
instead of inside it (Route
might not allow this), i.e.
ReactDOM.render(
<Provider store={store}>
<Router history={history}>
<SubspaceProvider mapState={state => state.login} namespace='subPage1'>
<Route path="/sub-page-1" component={SubPage1} />
</SubspaceProvider>
<SubspaceProvider mapState={state => state.characters} namespace='subPage2'>
<Route path="/sub-page-2/:someParam" component={SubPage2} />
</SubspaceProvider>
</Router>
</Provider>,
document.getElementById('root')
)
connect
function for cases where you don't want to use SubspaceProvider
in a jsx block that does pass on the props
, e.g.
ReactDOM.render(
<Provider store={store}>
<Router history={history}>
<Route path="/sub-page-1" component={subspaced(state => state.login, 'subPage1')(SubPage1)} />
<Route path="/sub-page-2/:someParam" component={subspaced(state => state.characters, 'subPage2')(SubPage2)} />
</Router>
</Provider>,
document.getElementById('root')
Happy to hear any comments or suggestions on this.
While it didn't cause any errors in the console in relation to proptypes, option 1 did not work. If I did a console.log(state)
in the mapStateToProps
function, it gave me the entire state tree.
Option 2 (using a HOC) seems like a good compromise for places that you just want to add in subspace to something that needs to accept props.
Although having said all that, the more I play with my little project (adding more routes and default routes that point to "home" style pages), the more my "workaround" seems to actually be good solution, as I can wrap the component once and use it in any route I like... however, option 2 could probably give the same result.
I have been using redux-subspace with react-router to isolate my pages from one another.
This has worked great as it has allowed me to develop each page as almost an entirely seperate application.
How I have this setup is as follows (simplified from my actual code)
index.js
SubPage1.js
SubPage2.js
This works great, except for the
someParam
part. React router provides the params to the components props, which is SubspaceProvider and not the wrapped component.I did manage to get around this with the following changes to index.js
However, I can also see other use cases where the parent component wants to pass params down to the underlying component, but either doesn't have access to the child to do so (like my case above where it was wrapped by another component) or it has imported a subspaced component and doesn't realise it cannot pass props (its is probably a bad design to expect props and also subspace yourself, but if you did want that its current not doable).
I propose that
SubspaceProvider
passes any extra props it receives down to the child component.