-
-
Notifications
You must be signed in to change notification settings - Fork 15.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Component Loading #1390
Comments
It is advisable that both actions and state are always serializable. Therefore putting a component into the action doesn’t seem like the correct way, and I would encourage you not to do that.
This is exactly what I would suggest. I don’t think at all it is bad. You can have a separate module that does this. import Button from './Button'
import Checkbox from './Checkbox'
function resolveComponent(componentId) {
switch (componentId) {
case 'BUTTON':
return Button
case 'CHECKBOX':
return Checkbox
}
} and in your component: function mapStateToProps(state) {
let Type = resolveComponent(state.componentId)
let children = <Type />
return { children }
} If your bundler supports code splitting, you can even make |
Thanks, I can massage this into my real example |
I'm curious about best practices for my use-case (which I believe is probably very common). It's combination of
react
,redux
,react-router
but I believe the discussion is best suited forredux
Documentation and guides are very vague when it comes to component-to-component communication (of a non parent-child nature). Imagine using
react-router
where the overall page is derived of nested layout components such asDefaultLayout
looks like this:For the sake of this example,
FooLayout
(loaded intothis.props.children
) can be any sub layout you want to imagine.All three routes:
/products
,/users
, or/widgets
have a button in their component. When clicked, it will load a whole new component into theDefaultLayout
's<div class="receive-any-component"></div>
section. For exampleProducts
component:This whole example is over simplified. I'm using
react-redux
in the real thing. Official react docs only say one small bit on this type of use-case:So, I understand that I can use Redux to dispatch info whereby
DefaultLayout
is subscribed, but I need to load a whole component intoDefaultLayout
. It would seem to me there are two basic options, neither of which I feel are correct.Option One
Have our
Product
component dispatch the component we want to load:Ultimately, I'd like the
DefaultLayout
to be unaware of what components it's loading, so this solution works out in that regards. But sending a component intoredux
just seems so wrong, but it works.Option Two
Instead of dispatching a component, dispatch some state that indicates which component should be loaded. In this case, it would seem that
DefaultLayout
would have toimport
every possible component it might need to load (which in my real usecase is many), then subscribe to our state and use a switch statement to determine which one of our loaded components should be placed into.receive-any-component
. This feels better from theredux
end but feels crappy thatDefaultLayout
is aware of every possible component that it might need to load.I'm not new to JS, but somewhat new to React/Redux. What am I missing?
And I apologize this question is so long. It's difficult for me to describe this any other way
The text was updated successfully, but these errors were encountered: