-
Notifications
You must be signed in to change notification settings - Fork 1.3k
withState alternatives #308
Comments
I believe Elm also use the similar API, and it looks promising to me. |
Just have read this facebook/react#8854 and there is possibility that this will be done in React, see statefull functuonal components, so in partial cases like all enhancers are referentially transparent, this will work from scratch. |
I am really looking forward to stateful functional component, but it will not land in 16 😞 |
@istarkov are you suggesting a new HOC for the library? |
@slightlytyler there are some problem with this HOC implementation, the same problem have guys with react bindings for reasonml. |
Picking up the conversation from #321 (which would also solve for #243). I love that you are looking ahead to future apis, and I think adding an alternative signature until then is not a bad thing. There's no reason to wait. Especially for such a simple, backwards compatible and non-controversial change. After reviewing this comment I've updated the proposal: Add a new signature to withState(
initialState: Object | (props: Object) => Object,
mapStateProps: ?(state: Object, setState: Function ) => Object
): HigherOrderComponent Only Usage would look like: import { withState } from 'recompose';
// setting initial state only
const enhance = withState({ red: 0, green: 0, blue: 0 });
// automatically get state and setState props
const RGBChooser = enhance(({ state, setState }) =>
<div>
<input type="text" value={ state.red } onChange={ (e) => setState({ red: e.target.value }) } />
<input type="text" value={ state.green } onChange={ (e) => setState({ green: e.target.value }) } />
<input type="text" value={ state.blue } onChange={ (e) => setState({ blue: e.target.value }) } />
<div
style={{
display: block,
width: 30,
height: 30,
backgroundColor: `rgb(${state.red},${state.green},${state.blue})`
}}
/>
</div>
); If I submit a pull request for #321 (code, tests, docs) would it be accepted? |
Very good looking idea here https://twitter.com/jordwalke/status/825305448542724099 which can be used instead or with
withState
.some reasons why current implementation can possibly be changed
So creating an enhancer which will wrap
setState
withwill give us ability to use
updater
as in twit exampleHaving that
updater
is memoized it will give the same function reference inonChange
handler on every rerender.The text was updated successfully, but these errors were encountered: