# react-form-lifecycle

> Effortless forms, no payload. Render-prop wrapper for npm.im/form-lifecycle

Latest version **2.4.3** (published 2020-01-31) · MIT license · 0 weekly downloads

## Install

```sh
npm install react-form-lifecycle
pnpm add react-form-lifecycle
yarn add react-form-lifecycle
bun add react-form-lifecycle
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.4.3 |
| Published | 2020-01-31 |
| First published | 2018-04-10 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 12.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 7 |
| Author | Andrew Joslin |
| Maintainers | andytjoslin |
| Keywords | react, form-lifecycle, state |

## Links

- npm: https://www.npmjs.com/package/react-form-lifecycle
- Repository: https://github.com/ajoslin/react-form-lifecycle
- Homepage: https://github.com/ajoslin/react-form-lifecycle#readme
- Issues: https://github.com/ajoslin/react-form-lifecycle/issues
- npm.io page: https://npm.io/package/react-form-lifecycle

## Dependencies (1)

- [form-lifecycle](https://npm.io/package/form-lifecycle.md) ^1.2.0

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 2.4.3 (latest) — 2020-01-31
- 2.4.2 — 2020-01-31
- 2.4.1 — 2020-01-31
- 2.4.0 — 2020-01-31
- 2.3.0 — 2018-09-07
- 2.2.0 — 2018-05-10
- 2.1.3 — 2018-05-01
- 2.1.2 — 2018-05-01
- 2.1.1 — 2018-04-17
- 2.1.0 — 2018-04-17
- 2.0.0 — 2018-04-11
- 1.0.3 — 2018-04-11
- 1.0.2 — 2018-04-11
- 1.0.1 — 2018-04-10
- 1.0.0 — 2018-04-10
- … 5 more at https://npm.io/package/react-form-lifecycle/versions

## README

# react-form-lifecycle [![Build Status](https://travis-ci.org/ajoslin/react-form-lifecycle.svg?branch=master)](https://travis-ci.org/ajoslin/react-form-lifecycle)

> Effortless forms, no payload. Render-prop wrapper for [FormLifecycle](https://npm.im/form-lifecycle).


## Install

```
$ npm install --save react-form-lifecycle
```


## Usage

Recommended way of doing forms:

```jsx
var FormLifecycle = require('react-form-lifecycle')
var filterBoolean = require('boolean-filter-obj')
var isEmail = require('is-email-maybe')

function renderForm () {
  return <FormLifecycle render={({ form, lifecycle }) => {
    var validationErrors = getValidationErrors(form)
    return <form onSubmit={e => {
      e.preventDefault()
      if (Object.keys(validationErrors).length) {
        return lifecycle.error()
      } else {
        lifecycle.submit()
        sendApiRequest('submit-form', form.fields)
          .then(lifecycle.success, lifecycle.error)
      }
    }}>
      {form.error && <p>Looks like there was a submission error: {form.error}</p>}
      {validationErrors.email && !form.pristine &&
        <p>{validationErrors.email.message}</p>}
      <input value={form.fields.email} type='email' onChange={e => lifecycle.edit({ email: e.target.value })} />

      <button type='submit' disabled={form.pending}>Submit</button>
    </form>
  }} />
}

function getValidationErrors (form) {
  return filterBoolean({
    email: !form.fields.email || !isEmail(form.fields.email)
      ? new Error('Please enter a valid email.')
      : null
  })
}
```

## API

## `<FormLifecycle>`

#### Props

#### onChange

> `function (form, prevForm)` | optional

Called whenever the form is changed via [`lifecycle` methods](https://github.com/ajoslin/form-lifecycle#lifecyclecreatedata---form). Receives the newForm and prevForm as parameters.

#### onReset

> `function (form, prevForm)` | optional

Called whenever `lifecycle.reset` is called

#### onEdit

> `function (form, prevForm)` | optional

Called whenever `lifecycle.edit` is called

#### onSubmit

> `function (form, prevForm)` | optional

Called whenever `lifecycle.submit` is called

#### onError

> `function (form, prevForm)` | optional

Called whenever `lifecycle.error` is called

#### onSuccess

> `function (form, prevForm)` | optional

Called whenever `lifecycle.success` is called

#### formDefaults

> `object` | optional

These values will be passed to the new [FormLifecycle object](https://github.com/ajoslin/form-lifecycle) that is created when the component is instantiated.

Example: `<FormLifecycle formDefaults={{ fields: {rememberMe: true} }} />`

#### render

> `function()` | *required*

`<FormLifecycle render={({ form, lifecycle }) => <div />} />`

Also supports child render function:
`<FormLifecycle>{({ form, lifecycle }) => <div />}}</FormLifecycle>`

The `render` prop function is called with an object containing the following:

- `form`: An instance of [FormLifecycle](https://github.com/ajoslin/form-lifecycle#api).
- `lifecycle`: An object containing all [FormLifecycle methods](https://github.com/ajoslin/form-lifecycle#api). When called, the form will be edited and re-rendered.
  - Example: `lifecycle.edit({ email: 'test@email.com' })`.

## License

MIT © [Andrew Joslin](http://ajoslin.com)

---
_Source: https://npm.io/package/react-form-lifecycle · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
