-
Notifications
You must be signed in to change notification settings - Fork 33
Questions and thoughts #30
Comments
Hi @jcheroske,
Thanks :)
There doesn't appear to be one single, correct-in-all-cases approach emerge as yet. Generally speaking, I find the parent components don't really need to interact much with the children, other than to provide some basic props, as they are able to take care of their own state and actions.
redux-subspace was designed so that the parent controls the bounds of the subspace (i.e. where in the store it lives and what the namespace is), but the children are in control within the subspace. As such, I would not advocate tinkering with the child's state directly from the parent. Using a My advice would be to use global actions from the parent and have the child handle them in it's reducer. This way the parent is controlling what it wants to happen, and the child is controlling how it will happen.
I haven't heard of redux-logic until just now, so I'm not very familiar with it. I've had a (very) brief look at it but looks like Usually, the things being wrapped (components, reducers, sagas, etc.) can be combined in some way at at each level of nesting, but I can't see anyway to do that with redux-logic, so when adding them to the middleware at the top level, you would need to know exactly which levels the originating action passed through to get the namespacing right. Possibly this could be mitigated by having a convention of wrapping the logic each time it was exported at a relevant namespacing layer, but I'm not familiar enough with it yet to say for certain. |
Let's say a child component has a |
@jpeyper and I were just discussing this yesterday. Theoretically, you can just raise an action with the namespace prefix of the child, and the
Obviously, this isn't ideal as it requires the developer to know about the namespacing prefix and to use it appropriately. I think providing a wrapper would be much nicer, i.e.
|
And this really is the rub with the current namespacing libraries. They work, but the abstractions have not captured the all of the common use cases, and so they leak. Having scoping functions for all the different elements (reducers, components, action creators) would not only tighten up the abstractions, but would encourage best practices. Another possible leak is around getting a child reducer's initial state. For example, when adding a reducer dynamically you usually want to get its initial state and push it onto the array that holds the state. How is this done? Do you just pass |
I think having action wrappers to help with common abstractions is a great idea. I think a Other abstractions @jpeyper and I discussed which would be great, but I don't know how much effort is involved (they're not as trivial as they sound), are:
Just to clarify, adding reducers dynamically is not something redux-subspace does, nor do I believe it is something it should be responsible for. That is something else's job. That said, I have some insight into how the initial state is determined when adding reducers dynamically to redux. There is a great Stack Overflow answer by Dan Abramov about how to dynamically inject reducers which I used as the inspiration for our dynamic reducer solution. When
We don't, but redux does. |
I'm going to close this, as it's a bit stale and you seem to be moving forward with many amazing things. I'll take a look at Dan's answer re: reducer injection. Did you use any of the dynamic reducer libs out there, or totally roll your own? |
Totally, rolled my own |
This lib looks fantastic. I have some questions about best practices when using it though. When building a fractal component, what is the preferred way for the parent to interact with the children? Do you ever tinker with child state from a parent reducer? Like if the child needs to be reset back to initial state, would you do that in the parent reducer. Or, would you make the child component a controlled component and have the parent interact with it through
value
andonValueChanged
props, for example? Or maybe there's another way that parent->child communication should happen?The other thing I'm wondering about is
redux-logic
support. I know it's not the most popular side-effect lib, but if you haven't looked at it, it's wonderful. No strange generator or observable syntax to deal with. You can write your side effects using async/await if you want, and the finished code ends up reading so clean. I've used sagas and epics, and I'm moving all of that code over to logics now. Since all logics are created with thecreateLogic
function, it might be quite easy to wrap that in some way to add namespacing support.Anyway, thank you for your work here.
The text was updated successfully, but these errors were encountered: