question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add a getFieldValueSetter props to avoid React Native boilerplate

See original GitHub issue

Bug, Feature, or Question?

Feature

Current Behavior

To use formik properly with React Native we need to define a custom Component and use setFieldValue in a wrapping function (to avoid arrow functions). This is a lot of boilerplate.

Cf https://github.com/jaredpalmer/formik#avoiding-new-functions-in-render

Desired Behavior

Enable this use of formik:

// Formik x React Native example
import React from 'react';
import { Button, TextInput, View } from 'react-native';
import { withFormik } from 'formik';

const enhancer = withFormik({
  /*...*/
});

const MyReactNativeForm = props => (
  <View>
    <TextInput
      onChangeText={this.props.getFieldValueSetter('email')}
      value={props.values.email}
    />
    <Button onPress={props.handleSubmit} title="Submit" /> //
  </View>
);

export default enhancer(MyReactNativeForm);

Suggested Solutions

withFormik could pass a new prop called getFieldValueSetter to the React component. This would be a function that takes a field name and return a function, a value setter for this field. A setter would be created during the first call and saved in a cache map by formik. Next calls would return the cached function. It would be something like that:

getFieldValueSetter = (fieldName) => {
    if (!this.valueSetterCacheMap) valueSetterCacheMap = {};
    return valueSetterCacheMap[fieldName] || (valueSetterCacheMap[fieldName] = (value) => {
        this.setFieldValue(fieldName, value);
    });
}

Doc should be updated.

Info

  • Formik Version: not relevant
  • OS: not relevant
  • Node Version: not relevant
  • Package Manager and version: not relevant

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
jaredpalmercommented, Feb 7, 2018

getFieldValueSetter is pretty long to type, but very explicit.

i wonder if we just overload handleChange and handleBlur like this:

handleChange = (maybeStringOrEvent: 'string' | React.ChangeEvent<any>) => {
  const actualHandleChange = (e: React.ChangeEvent<any>) => {
    // our exisitng fn as it is now
  }

  if (isString(maybeStringOrEvent)) {
    // we could cache these handlers too by key like Preact's linkState does for perf boost
    if (this.handleCache[maybeStringOrEvent]) {
      return this.handleCache[maybeStringOrEvent];
    } else {
      this.handleCache[maybeStringOrEvent] = (e: React.ChangeEvent<any>) => actualHandleChange(e);
      return this.handleCache[maybeStringOrEvent]
    }
  } else {
    actualHandleChange(e)
  }
}```
0reactions
stale[bot]commented, Aug 22, 2018

ProBot automatically closed this due to inactivity. Holler if this is a mistake, and we’ll re-open it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How To Customize React Components with Props
You will be building a completely new set of custom components. You'll start by clearing out some boilerplate code so that you can...
Read more >
Direct Manipulation - React Native
Use setNativeProps when frequent re-rendering creates a performance bottleneck! Direct manipulation will not be a tool that you reach for ...
Read more >
Introducing Hooks - React
On this page, we'll continue by explaining why we're adding Hooks to React and how they can help you write great applications. Note....
Read more >
Create a React Native app using Ignite boilerplate
It also automatically adds it to the app/screens/index.ts file. Generators. That's the power of generators: they make life easy for you since ...
Read more >
How we build apps using React Native: Boilerplate - Uptech
Each component can potentially initiate action to change the store, as well as each component which observes the state can receive new Props...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found