-
Notifications
You must be signed in to change notification settings - Fork 40
Remove update/action code and re-expose React's setState functions #73
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
Conversation
I've been using the actions a lot and while it's verbose sometimes, even just making a SetState (state -> state) has been okay for me. Surely there's a compromise somehow to expose both the Action/update version and a normal setState version |
I find that working with the Action/Update idiom makes pages (for me, at least) much easier to follow, mostly because it treats the page as a state machine, with clearly-defined transitions. I wonder, though, if we can still achieve a similar code structure in its absence. I'm thinking something like:
The problem with something like this is that it relies on social conventions rather than API. |
I think if we are going to remove actions, the README.md probably needs a minor update to reflect that? I am not deeply familiar with react-basic but since:
then presumably it makes sense for If we do want to make both available, I think making
Yes, I think an actions-based API is probably not particularly useful unless we can be sure that state changes only happen via |
This is one of the arguments for hooks as well, and it allows for both of these patterns (as well as user-created ones) on a per-component basis. I'd rather stick with the least-breaking change possible until we've explored that. |
I find that the reducer approach makes code reviews quite a bit harder, and potentially complicates the logic, since I have to look in at least two places to understand the logic (at least, since the As for the argument about structuring code, I don't think actions improve anything there since each action can just be replaced by a named function (since actions are not part of the component's API but just an implementation detail). I also would prefer |
I agree with all these statements. |
#71 has been fixed (v7.0.0), so that bit of my original comment is no longer relevant. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good. Let's leave this open for a little while longer to test it out and collect feedback, and then we can plan to merge and release if it looks okay.
An example of the recommended style for existing reducer-based components might be nice too, if only in the readme.
Sounds good, I have a branch in progress in Gumball we can review soon. |
I've converted some internal Lumi components to use this branch. So far nothing's been hard or even drastically different. I tried a few different approaches depending on what seemed to make the most sense for the component:
Before: update self = case _ of
ToggleOpen ->
UpdateAndSideEffects
self.state { isOpen = not self.state.isOpen }
\{ props, state } -> when state.isOpen props.onOpen
Close ->
Update
self.state { isOpen = false } After toggleOpen self = do
self.setStateThen
(_ { isOpen = not self.state.isOpen })
do
props <- readProps self
state <- readState self
when state.isOpen props.onOpen
close self = do
self.setState _ { isOpen = false }
Before: update self = case _ of
IsDragging isDragging ->
if self.state.isDragging == isDragging
then NoUpdate
else Update self.state { isDragging = isDragging } After: send self = case _ of
IsDragging isDragging -> do
unless (self.state.isDragging == isDragging) do
self.setState _ { isDragging = isDragging } The rest of the changes were related to moving/removing some event helpers which never should have been in the Before: onClick: capture_ self $ CancelPendingFile value After: capture_ $ send self $ CancelPendingFile value |
Surely some of this code could live in a Utils module or something |
Which code? |
@justinwoo What do you think of this? |
Looks nice, I think this will work for me, thanks. |
We've been finding the update/action pattern to be too verbose for basic use cases.
There was also a bug with type checking actions correctly (#71).This update keeps the current types and lifecycle code while returning state updates to the more traditionalsetState
andsetStateThen
.