-
Notifications
You must be signed in to change notification settings - Fork 31
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
Investigate passing specific properties to React components #6
Comments
Wouldn't it be a good idea/possible (with type safety) to implement mechanism similar to reduxes |
@elderapo By passing the withStore(({ foo, bar, set }) =>
({ baz }) =>
<div>..</div>
) Work in progress is in this branch, if you're curious or have more thoughts. |
Personally, this is the main turn-off for me with undux because IMHO it goes against React conventions. It feels wrong to call getter methods in my components. This also breaks the separation of concerns since now my component is bound to a method If the trade off is to have a closure like you show, I'm 100% ok with that and think any beginner (eg people who know React well enough to pick a tool like undux) would be OK with it. Just my $0.02! Really like what you're doing with undux! |
Not sure if this is doable while keeping typechecking, but perhaps an API like this? import React from 'react'
import { StoreProps, withStore } from './store'
type Props = {
greeting: string
markAsRead(): void
}
function Welcome<StoreProps && Props>({ greeting, markAsRead, messages, user }) {
return (
<div>
<p>{greeting} {user.name}, you have {messages.length} messages!</p>
<button onClick={markAsRead}>Clear new messages</button>
<div>
)
}
}
// Option 1:
export default withStore(
({ messages, set, user }) => ({
markAsRead() {
set('messages', [])
},
messages,
user,
}),
Welcome
)
// Option 2:
// export default withStore(
// store => ({
// markAsRead: () => store.set('messages', []),
// messages: store.get('messages'),
// user: store.get('user'),
// }),
// Welcome
// ) Usage: <WelcomeMessage greeting="bonjour" /> Something like this is clear to the user while (hopefully) getting all the existing benefits of undux. This may be a bit more verbose but it clearly separates the state management/access from the component which I think is desirable. This way the component stays unaware of the state management tool in use and can just expect specific props. |
@danawoodman Thanks for the feedback! Your proposed API looks reasonable, and your concerns are really fair. The one reason I haven't jumped on this issue is that I love how simple and easy to understand a single Your What do you think? |
Jumping in here. @danawoodman is spot on in terms of decoupling component implementation from the store per react style. @bcherny you are also right for I was drawn to undux because of its simplicity. I think the biggest gain undux provides is getting rid of all the action/reducer boilerplate that comes with redux. Anyone using undux will be using react and will have to understand the concept of HOC sooner rather than later. From my point of view, going with option 1 or 2 is still far better than redux with boilerplate. In short, I don't think going in any of the proposed options takes way the simplicity of the library compared to the status quo. With that said, it also possible to offer the current api (passing the store into the component) as more or less a beginners level and the decoupled options per @danawoodman suggest for advanced usage. That should mitigate the risk of getting people up and running while providing a path for advanced usage as your application grows. Personally, I think that will add complexity and might be best to roll with something along the lines of options 1 or 2 but you know best. I'd also like to propose the 3rd option below which is a slight modification on option 1. This helps address the issue of what props come from where.
|
Instead of passing the entire store.
Note: This will be hard to type safely.
Pros:
forceRender
shouldComponentUpdate
, and use other lifecycle methodsAlternative: Use an immutable data structure to back
store
, so the reference changes.Cons:
Asymmetrical API for getting/setting (fixed by simplify the set/get of store, just like mobx #7):
Precludes an API where we automatically infer what to watch based on what is used. We can't infer types from the arguments (unless there's a clever trick I haven't thought of), and passing
store
directly lets us do thisThe text was updated successfully, but these errors were encountered: