Add a getFieldValueSetter props to avoid React Native boilerplate
See original GitHub issueBug, 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:
- Created 6 years ago
- Comments:5 (2 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
getFieldValueSetter is pretty long to type, but very explicit.
i wonder if we just overload handleChange and handleBlur like this:
ProBot automatically closed this due to inactivity. Holler if this is a mistake, and we’ll re-open it.