-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Add class-like state behavior option to withState #357
Conversation
I'll write here why I would prefer something like #308 and not a current solution. withState('something', 'setSomething', {a:1, b: 2}),
withHandlers({
onAChange: ({ setSomething }) => ({ a }) => ({
setSomething(prev => ({...prev, a}))
})
}) As you see this is not so a big addition even you will write it inside render. I created an issue here reasonml/reason-react#37 about what I wrote in #308 BTW we can try implementation provided there https://github.com/reasonml/reason-react/blob/master/src/reactRe.re#L422-L454 and even it has some drawbacks now |
I totally agree with you and are we sure it will be solved by React soon? Will they even settle on a final API soon as well? If not, why wait? Let's improve upon what we know now. True, this is not a clever feature, but this is probably how withState should have behaved originally. Typically I agree that most PRs should be rejected because they can be implemented in dev's own code, but this is simply allowing functional stateless components to have state features that mirrors existing class state features. For beginners of recompose it will be very comfortable, and it's way more flexible for when devs need to manage more than a single state variable. |
45812a1
to
24657af
Compare
Codecov Report
@@ Coverage Diff @@
## master #357 +/- ##
=========================================
+ Coverage 93.07% 93.27% +0.2%
=========================================
Files 51 51
Lines 332 342 +10
=========================================
+ Hits 309 319 +10
Misses 23 23
Continue to review full report at Codecov.
|
5aa5e3b
to
31fd710
Compare
agree, btw original React setState with state merging is also confusing, year or more ago I did |
I wanna say current behavior is more predictable, and in a lot of cases allows to use it without withHandlers etc |
When can you ever use it without withHandlers? Or do you mean use an anonymous function with the jsx?
Can't we fix it ourselves instead of waiting on react? Can we throw and error to escape setState and have a wrapper/proxy setState that catches the error? In this way we could implement what you want with the updater approach. |
Adds an additional optional param set for withState, where if you only provide initialState, then stateName and stateUpdaterName are defaulted to 'state' and 'setState'. In this form setState works exactly like setState from a typical class component (because its really just proxying straight to
this.setState
).Example Usage:
I have code, tests, and docs in this PR.
closes #321