<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Matej Murín on Medium]]></title>
        <description><![CDATA[Stories by Matej Murín on Medium]]></description>
        <link>https://medium.com/@matejmurin2?source=rss-a7bda0432f8c------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*-OqSg0aU2gu6t_Xd1nJJkQ.jpeg</url>
            <title>Stories by Matej Murín on Medium</title>
            <link>https://medium.com/@matejmurin2?source=rss-a7bda0432f8c------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 04:55:17 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@matejmurin2/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Handling multiscreen forms in React Native]]></title>
            <link>https://medium.com/@matejmurin2/handling-multiscreen-forms-in-react-native-8735c63b0f0d?source=rss-a7bda0432f8c------2</link>
            <guid isPermaLink="false">https://medium.com/p/8735c63b0f0d</guid>
            <category><![CDATA[forms]]></category>
            <category><![CDATA[navigation]]></category>
            <category><![CDATA[react-native]]></category>
            <category><![CDATA[redux]]></category>
            <category><![CDATA[redux-form]]></category>
            <dc:creator><![CDATA[Matej Murín]]></dc:creator>
            <pubDate>Mon, 27 May 2019 16:32:22 GMT</pubDate>
            <atom:updated>2019-05-27T16:32:22.701Z</atom:updated>
            <content:encoded><![CDATA[<p>Seamlessly handling forms with React Native, Formik and Redux</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*NZlzijlykWt3_eiZ" /><figcaption>Photo by <a href="https://unsplash.com/@goshua13?utm_source=medium&amp;utm_medium=referral">Joshua Aragon</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>There’s ton of articles, guides and tutorials on how to handle forms in React Native applications. Yet I couldn’t find any suitable and compelling enough for me (probably authors of those guides were thinking the very same). So here am I to contribute to the chaos.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/250/1*dypCDDOt6CdkfTNdKrz4Ew.gif" /><figcaption>Something like this</figcaption></figure><p><strong>What do we want?</strong></p><ul><li><em>easily</em> accessible values from any screen and component</li><li>validation</li><li>dirty checking</li><li>error handling</li><li>smooth back and forth navigation</li><li>absolute control</li></ul><h4>Stack</h4><p>Few of our main heroes. Btw I’m using <a href="https://github.com/rematch/rematch">rematch</a> for Redux and enjoying my life so far, no more redux-saga nightmares interrupting my sleep (joke).</p><pre>&quot;@rematch/core&quot;: &quot;^1.1.0&quot;,<br>&quot;formik&quot;: &quot;^1.5.4&quot;,<br>&quot;react-native&quot;: &quot;0.59.8&quot;,<br>&quot;react-navigation&quot;: &quot;^3.9.1&quot;,</pre><h4>StackNavigator and Formik</h4><p>I’ve been using <a href="https://jaredpalmer.com/formik">Formik</a> for some time with ReactJS in the web environment before I started working with React Native. Back then the most natural way was to wrap all my form-related components within Formik like following:</p><pre>&lt;Formik<br>  ...<br>  render={props =&gt; (<br>    &lt;Switch&gt;<br>      &lt;Route path=&quot;/edit/personal-information&quot; ... /&gt;<br>      &lt;Route path=&quot;/edit/location&quot; ... /&gt;<br>      &lt;Route path=&quot;/edit/invite-friends&quot; ... /&gt;<br>    &lt;/Switch&gt;<br>  )}<br>/&gt;</pre><p>Formik’s inner state was the only source-of-truth and therefore no Redux was needed.</p><p>Things get bitter with React Native considering the nature of StackNavigator:</p><pre>const routeConfigMap = {<br>  Form: {<br>    screen: FormScreen,<br>  },<br>  <br>  Location: {<br>    screen: LocationScreen,<br>  },<br>  <br>  InviteFriends: {<br>    screen: InviteFriendsScreen,<br>  },<br>};</pre><pre>createStackNavigator(routeConfigMap, stackConfig);</pre><ul><li>Every screen is an <strong>isolated</strong> component</li></ul><p>=&gt; we cannot easily wrap bunch of screens inside one Formik parent.</p><ul><li>Screens are <strong>unmounting</strong> when we are navigating back</li></ul><p>=&gt; form values cannot be stored inside of screens.</p><p>One possibility is to store values in the very first screen of the form (beginning of the stack). This approach would mean we have to pass form values and callbacks via navigation screen params. Imagine your form consists of 5 screens. Exactly. This is not the way.</p><p>Avoiding redux is becoming increasingly cool but here we can gain huge benefits from using it (properly):</p><ul><li>form values will be detached from the component tree,</li><li>easily accessible from all screens and components.</li></ul><h3>Approach</h3><ul><li>redux store will contain property form where will all our forms store their values,</li><li>each form will be identified by unique id like EVENT_FORM,</li><li>errors don’t have to be identified by id since we never interact with two forms simultaneously.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/858/1*g8P7vggR-9PQPpzm31xY-w.png" /><figcaption>Screenshot of redux store from <a href="https://github.com/infinitered/reactotron">reactotron</a></figcaption></figure><p><strong>Rematch model</strong></p><p>Keep it simple. We only need to provide reducers for create, update and discard form values.</p><pre>const INITIAL_STATE = {<br>  ERRORS: {},<br>};</pre><pre>// ...</pre><pre>reducers: {<br>  setOne: (state, { formId, formValues }) =&gt; ({<br>    ...state,<br>    [formId]: {<br>      ...formValues,<br>    },<br>  }),</pre><pre>  updateOne: (state, { formId, formValues }) =&gt; {<br>    const newState = cloneDeep(state);</pre><pre>    newState[formId] = {<br>      ...state[formId],<br>      ...formValues,<br>    };</pre><pre>    return newState;<br>  },</pre><pre>  removeOne: (state, { formId }) =&gt; {<br>    const newState = cloneDeep(state);<br>    delete newState[formId];<br>    return newState;<br>  },</pre><pre>  unset: () =&gt; cloneDeep(INITIAL_STATE),<br>},</pre><pre>// effects are simply calling the reducers with payload</pre><pre>effects: {<br>  async initForm(payload) {<br>    this.setOne(payload);<br>  },</pre><pre>  async updateForm(payload) {<br>    this.updateOne(payload);<br>  },</pre><pre>  ...<br>},</pre><p><strong>Screens</strong></p><p>Connect your component (in my case FormScreen.js) to the redux store.</p><pre>const mapState = state =&gt; ({<br>  formValues: state.form[FORM_IDS.EVENT_FORM],<br>  formErrors: state.form[FORM_IDS.ERRORS],<br>});</pre><pre>const mapDispatch = dispatch =&gt; ({<br>  initForm: formValues =&gt;<br>    dispatch.form.initForm({<br>      id: FORM_IDS.EVENT_FORM,<br>      formValues,<br>    }),</pre><pre>  updateForm: formValues =&gt;<br>    dispatch.form.updateForm({<br>      id: FORM_IDS.EVENT_FORM,<br>      formValues,<br>    }),</pre><pre>  discardForm: () =&gt;<br>    dispatch.form.discardForm({<br>      id: FORM_IDS.EVENT_FORM,<br>    }),<br>});</pre><pre>export default connect(<br>  mapState,<br>  mapDispatch<br>)(FormScreen);</pre><p>Now you have the full power over your forms. You can compute your initial values in componentDidMount():</p><pre>componentDidMount() {<br>  this.init();<br>}</pre><pre>init = async () =&gt; {<br>  const { initForm } = this.props;</pre><pre>  this.setState({ ready: false });</pre><pre>  const initialValues = {<br>    name: &quot;Placeholder&quot;,<br>    currency: &quot;EUR&quot;,<br>    ...<br>  };</pre><pre>  await initForm(initialValues);</pre><pre>  this.setState({ ready: true });<br>  // safely render when your component is ready<br>}</pre><p>And update values on submit:</p><pre>onSubmit = async formValues =&gt; {<br>  const { navigation, updateForm } = this.props;</pre><pre>  updateForm(formValues);<br>  navigation.navigate(&quot;Location&quot;);<br>};</pre><p>In render of FormScreen.js you can pass your formValues and formErrors to whatever component. In the 2nd and 3rd screens of the GIF example I’m not using Formik. Sometimes you just want to persist values during navigation.</p><pre>validate = values =&gt; {<br>  let errors = {};</pre><pre>  if (values.name.length &lt;= 2) {<br>    errors.name = &quot;Must be longer than 2 characters&quot;;<br>  }</pre><pre>  // other field errors</pre><pre>  return errors;<br>}</pre><pre>...</pre><pre>render() {<br>  const { formValues, formErrors } = this.props;</pre><pre>return (<br>    &lt;Formik<br>      initialValues={formValues}<br>      errors={formErrors}<br>      validate={this.validate}<br>      onSubmit={this.onSubmit}<br>      render={form =&gt; (<br>        &lt;MyEventForm form={form} ... /&gt;<br>      )}<br>    /&gt;<br>  )<br>}</pre><p>And my MyEventForm.js consists of fields and callbacks:</p><pre>onFieldChange = (form, property) =&gt; value =&gt; {<br>  form.setFieldValue(property, value);<br>  form.setFieldTouched(property);<br>  form.setFieldError(property, null);<br>};</pre><pre>...</pre><pre>&lt;FormField<br>  autoFocus<br>  label=&quot;Name&quot;<br>  name=&quot;name&quot;<br>  error={form.errors[&quot;name&quot;]}<br>  onChange={this.onFieldChange(form, &quot;name&quot;)}<br>/&gt;</pre><p>This approach is simple, light and straightforward. It gives us full control over the form, error handling, persisting values during navigation and exposing Formik’s functionality.</p><p>There’s a ton of alternatives from DIY ones like mine or specialized libraries like <a href="https://redux-form.com/">redux-form</a> which can add undesirable complexity.</p><p>Choice is yours, hope this article helps. Cheers!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8735c63b0f0d" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>