Formik: Typescript examples

Created on 8 Jul 2017  路  5Comments  路  Source: formium/formik

It would be nice to have typescript examples to show how the interfaces and generics are supposed to be used.

Most helpful comment

Short version:

import * as React from 'react'
import { Formik, InjectedFormikProps } from 'formik'
import Yup from 'yup'

interface Props {
 ...
}

interface Values {
...
}

interface Payload {
...
}

export const MyInnerForm: React.SFC<InjectedFormikProps<Props, Values>> = (props) => (...)

export default Formik<Props, Values, Payload>({
   displayName: 'MyInnerForm',
   validationSchema: Yup.object().shape({...}),
   // you don't need to explicitly type the mapPropsToValues, mapValuesToPayload, and handleSubmit because their types are inferred
   mapPropsToValues: (props: Props) => ({...}),
   mapValuesToPayload: (values: Values) => ({..}),
   handleSubmit: (payload: Payload, formikBag: FormikBag<Props, Values>) => {
     // formikBag contains all the setters and props
  })(MyInnerForm)

Here is the same thing, but with more semantic naming of the generics:

...

interface PropsThatWillBePassedToTheWrappedComponent {
...
}

interface ShapeOfWhatMapPropsToValuesReturns {
  // this should be the state of  what will be injected as`props,values`
}

interface ShapeOfWhatMapValuesToPayloadReturn {
 // shape of the payload (i.e. what get's passed as first argument of handleSubmit
}



export const MyInnerForm: React.SFC<InjectedFormikProps<PropsThatWillBePassedToTheWrappedComponent, ShapeOfWhatMapPropsToValuesReturns>> = (props) => (...)

export default Formik<PropsThatWillBePassedToTheWrappedComponent, ShapeOfWhatMapValuesToPropsReturns, ShapeOfWhatMapValuesToPayloadReturns>({...})(MyInnerForm)

All 5 comments

Short version:

import * as React from 'react'
import { Formik, InjectedFormikProps } from 'formik'
import Yup from 'yup'

interface Props {
 ...
}

interface Values {
...
}

interface Payload {
...
}

export const MyInnerForm: React.SFC<InjectedFormikProps<Props, Values>> = (props) => (...)

export default Formik<Props, Values, Payload>({
   displayName: 'MyInnerForm',
   validationSchema: Yup.object().shape({...}),
   // you don't need to explicitly type the mapPropsToValues, mapValuesToPayload, and handleSubmit because their types are inferred
   mapPropsToValues: (props: Props) => ({...}),
   mapValuesToPayload: (values: Values) => ({..}),
   handleSubmit: (payload: Payload, formikBag: FormikBag<Props, Values>) => {
     // formikBag contains all the setters and props
  })(MyInnerForm)

Here is the same thing, but with more semantic naming of the generics:

...

interface PropsThatWillBePassedToTheWrappedComponent {
...
}

interface ShapeOfWhatMapPropsToValuesReturns {
  // this should be the state of  what will be injected as`props,values`
}

interface ShapeOfWhatMapValuesToPayloadReturn {
 // shape of the payload (i.e. what get's passed as first argument of handleSubmit
}



export const MyInnerForm: React.SFC<InjectedFormikProps<PropsThatWillBePassedToTheWrappedComponent, ShapeOfWhatMapPropsToValuesReturns>> = (props) => (...)

export default Formik<PropsThatWillBePassedToTheWrappedComponent, ShapeOfWhatMapValuesToPropsReturns, ShapeOfWhatMapValuesToPayloadReturns>({...})(MyInnerForm)

Thanks!

@jaredpalmer Can you provide complete working typescript code?

@fatihapaydin see here https://github.com/jaredpalmer/formik/issues/472
@jaredpalmer If you create a code, please use reactstrap. Thanks for effort.

@jaredpalmer what about an example for the render props version? 馃檹

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Jungwoo-An picture Jungwoo-An  路  3Comments

dearcodes picture dearcodes  路  3Comments

PeerHartmann picture PeerHartmann  路  3Comments

giulioambrogi picture giulioambrogi  路  3Comments

jaredpalmer picture jaredpalmer  路  3Comments