In the following example in recompose
, withState
has an initial value of empty string. What if you needed a value form a database or dynamic source, how would you have it update on that prop change?
For example:
withState('value', 'updateValue', (user) => user.name)
Original code from recompose
;
const enhance = compose(
withState('value', 'updateValue', ''),
withHandlers({
onChange: props => event => {
props.updateValue(event.target.value)
},
onSubmit: props => event => {
event.preventDefault()
submitForm(props.value)
}
})
)
const Form = enhance(({ value, onChange, onSubmit }) =>
<form onSubmit={onSubmit}>
<label>Value
<input type="text" value={value} onChange={onChange} />
</label>
</form>
)