Forms
#
Example 1Let's break down the code above.
These are just events & effects definitions.
Next piece of code shows how we can obtain a state in effector in a right way. This kind of state retrieving provides state consistency, and removes any possible race conditions, which can occur in some cases, when using getState
.
So far, so good, we've almost set up our model (events, effects and stores). Next thing is to create event, which will be used as onChange
callback, which requires some data transformation, before data appear in setField
event.
Next, we have to deal with how inputs should work. useStoreMap
hook here prevents component rerender upon non-relevant changes.
And, finally, the App
itself! Note, how we got rid of any business-logic in view layer. It's simpler to debug, to share logic, and even more: logic is framework independent now.
Prevent default html form submit behavior using react event from submitted
#
Example 2This example shows, how you can manage state with uncontrolled form, handling loading of data, create components which dependend on stores, transform data passed between events.