-
Notifications
You must be signed in to change notification settings - Fork 28
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
Add combineReducers #19
Comments
Another idea: const ConnectedFoo = connect(FooComponent, state => state.foo); |
While I can't say I'm experienced with Redux (yet), I can say that having the selector as the second argument would be beneficial, since it could denote a simple @stasm, to be be clear, the selectors would allow specific renders to happen only when the correct state changes, correct? If so then yeah, I am on-board with this sort of change. |
I don't really selectors are going to make much of a difference performance wise. The whole block is re-rendered anyways. However it helps in code sanity, so I think it should be fine as an optional parameter. Also I was actually thinking about combineReducers this weekend. it would be a great (and necessary) addition. Of course as an additional module. |
@wrick17 is right — this won't affect performance. Innerself just re-renders the entire And I also agree with the point about code sanity. That's what I meant by "self-documenting". Selectors help express in code which part of the state tree the component needs without hard-coding the shape of the tree into the component itself. |
@stasm Makes sense. Putting it as an optional parameter sounds good to me. People can choose to write less code or could go for more readable code for bigger projects. |
Adding a As for selectors, one option might be to adopt/suggest a import html from 'innerself';
import { connect } from '../store';
const ConnectedFoo = connect(
state => ({ bar: state.innermost.prop.bar }),
({ bar }) => html`
<div>
check out my bar: ${bar}
</div>
`
); (the above would be achievable with your selector proposal in general, just thought it could be a nice pattern to suggest, build around) |
I also like the idea of mirroring const ConnectedFoo = connect(state => ({ bar: state.inner.bar }))(Foo) |
Yeah, I like that too. Redux's Originally, I didn't want to enforce any pattern wrt. |
What I like about the variable args patterns is that you can then do things like |
Optional parameters or returning a function that accepts a component both seem fine to me with a slight preference towards variable args. My main concern around all of this is the potential for All of that being said - with a router on the horizon, multiple reducers do seem to be the logical next step and the |
I completely agree. And I share your concerns—I wouldn't want innerself to get too advanced precisely because I believe that its simplicity communicates its purpose. @bsouthga has a good point about how this discussion also touches the good practice around the API of components. If the reason to introduce selectors is to improve code sanity and separate the operation of connecting a component from defining it, we shouldn't taint the signature of components with the connected state. The current solution of passing the state as the first argument doesn't scale well: it tight-couples the component's definition with the fact that it is supposed to be connected. React/Redux have a good solution for this, as @bsouthga already pointed out: the pattern of passing a I have a WIP branch locally which I'll push later tonight for your review. |
Alright, I submitted #22 and would love to hear your thoughts about it! |
When working on medium-sized apps, it's helpful to enforce the separation of concerns by writing separate reducers for different parts of the state tree. An example of this is routing which needs its own state and logic.
I'd like to add an optional
combineReducers
function to innerself. Perhaps in a new module:innerself/combine
?@bsouthga created his own implementation in innerself-hg and I used this one in A moment lost in time.
I wonder if we should also add selectors to
connect
. innerself actually used to have selectors when I first wrote it. I then removed them to keep things simple.Selectors are functions which select a part of the state before it's passed on to the connected component. Something like:
In Redux selectors are super-useful: they help Redux decide if the connected component needs to update. In innerself everything re-renders at all times, so the main benefit would be decoupling the shape of the state tree from the API of individual components.
Even though this looks nice and makes the code self-documenting, I expect the most common scenario to not need selectors. We'd then force the
connect(state => state)(…)
boilerplate onto everyone or have a special-case for the undefined selector:connect()(…)
. This in turn would be different from how Redux treats the undefined selector: it ignores the store completely. Perhaps that's OK. If you want to ignore state, you simply don'tconnect
your component to it in innerself.For now in my code I went for the following pattern whenever I needed selectors:
This pattern could be easily abstracted to a helper function, for instance:
And then:
This helper could also live in the optional module alongside
combineReducers
. I'm not particularly fond of this API, though.The text was updated successfully, but these errors were encountered: