@shopify/react-form-state
@shopify/react-form-state
npm i @shopify/react-form-state
@shopify/react-form-state

@shopify/react-form-state

A loosely related set of packages for JavaScript/TypeScript projects at Shopify

by Shopify

2.0.4 (see all)License:MITTypeScript:Built-In
npm i @shopify/react-form-state
Readme

@shopify/react-form-state

Build Status Build Status License: MIT npm version npm bundle size (minified + gzip)

Manage React forms tersely and type-safely with no magic.

This library is now superseded by @shopify/react-form as it allows you to write the preferred, functional, and hooks-driven React components over class-based ones.

Installation

$ yarn add @shopify/react-form-state

Usage

<FormState />

The default component exported by this library is <FormState />.

import FormState from '@shopify/react-form-state';
// Fields here refers to the inferred type of your initialValues object
interface Props<Fields> {
  initialValues: Fields;
  validators?: Partial<ValidatorDictionary<Fields>>;
  onSubmit?: SubmitHandler<Fields>;
  validateOnSubmit?: boolean;
  children(form: FormDetails<Fields>): React.ReactNode;
}

Its only mandatory props are initialValues and children. The initialValues prop is used to infer all the types for the rest of the component, and to generate handlers and field state objects. The children prop expects a function of the current state of the form, which is represented by a FormDetails object.

<FormState initialValues={myInitialValues}>
  {({fields, dirty, valid, submitting, errors, reset, submit}) => {
    return /* some cool ui */;
  }}
</FormState>

For detailed explanations of how to use <FormState /> check out the guide.

validators

The library also makes a number of validation factory functions available out of the box that should help with common use cases, as well as some tools to make building reusable custom validators easy.

import {validate, validators} from '@shopify/react-form-state';

For detailed explanations of the validation utilities, check out the validation docs.

Downloads/wk

64.9K

GitHub Stars

1.4K

LAST COMMIT

7mos ago

MAINTAINERS

19

CONTRIBUTORS

316

OPEN ISSUES

99

OPEN PRs

43
VersionTagPublished
2.0.4
latest
21d ago
0.0.0-snapshot-20220629200129
snapshot
3mos ago
2.0.1-react-18-react-testing-beta.10.0
next
4mos ago
1.0.1-address-mocks-minified-beta.0
beta
1yr ago
No alternatives found
No tutorials found
Add a tutorial

Rate & Review

100
No reviews found
Be the first to rate