@native-base/formik-ui is the official NativeBase - Formik bindings by NativeBase Team. @native-base/formik-ui provides with form components integrated with formik hooks out of the box, Each component makes use of Formik's context, therefore you need to have Formik installed in your project, after that all you have to do is write your validationSchema, build your Form using NativeBase Formik UI components and wrap that with <Formik/> , rest will be taken care by us.
importReactfrom'react';import{CheckboxControl,Text,SubmitButton,ResetButton,Box,ButtonGroup,}from'@native-base/formik-ui';import{Formik}from'formik';import{Heading,Center,NativeBaseProvider}from'native-base';import*asYupfrom'yup';constvalidationSchema=Yup.object().shape({tnc: Yup.boolean().equals([true],'Terms and Condition must be checked !'),newsletter: Yup.boolean(),});functionFormikCheckboxBasicExample(){constonSubmit=async(values)=>{awaitnewPromise((resolve)=>setTimeout(resolve,1000));console.log(values);};return(<FormikinitialValues={{tnc: true,newsletter: false,}}onSubmit={onSubmit}validationSchema={validationSchema}>{({ values, errors })=>(<Box><Heading>Checkbox FormControl</Heading><CheckboxControlname="tnc"label="Checkbox Control Label"mt={4}><Textmx={2}>Terms and Conditions</Text></CheckboxControl><CheckboxControlname="newsletter"mt={4}><Textmx={2}>Subscribe to Our Newsletter</Text></CheckboxControl><Boxpb={4}/><ButtonGroupspacing={6}><SubmitButtoncolorScheme="teal">Next</SubmitButton><ResetButtoncolorScheme="yellow">Reset</ResetButton></ButtonGroup><Boxmt={4}bg="gray.100"p={3}><Headingsize="sm"mb={2}>
Current State
</Heading>
Values: {JSON.stringify(values,null,2)}
Errors: {JSON.stringify(errors,null,2)}</Box></Box>)}</Formik>);}exportdefaultfunction(){return(<NativeBaseProvider><Centerflex={1}><FormikCheckboxBasicExample/></Center></NativeBaseProvider>);}# Using Yarn
yarn add @native-base/formik-ui
# Using NPM
npm i @native-base/formik-uiNativeBase Formik UI has Formik, NativeBase and some various other libraries as Peer Dependencies. So make sure to have those packages installed to your project to make use of NativeBase Formik UI.
# Using Yarn
yarn add formik native-base@next react-native-svg @expo/vector-icons styled-components styled-system react-native-safe-area-context @react-native-picker/picker
# Using NPM
npm i formik native-base@next react-native-svg @expo/vector-icons styled-components styled-system react-native-safe-area-context @react-native-picker/pickerTo validate the form fields, we recommend using Yup.
# Using Yarn
yarn add yup
# Using NPM
npm i yupSee the Contribution guide to learn how to contribute to the repository and the development workflow.
MIT © NativeBase