Getting Started

Building forms in React might be a challenge. We have to face many tedious things like form data, validation, submission, and more ๐Ÿคฏ.

As a React developer, there're two strategies of implementing forms, the controlled components and uncontrolled components, each has its advantages and timing of use. The controlled components serve form state as the single source of truth. However, the uncontrolled components make our code more concise and performant.

React Cool Form combines these advantages and references the UX theory of Nielsen Norman Group as the basis for our API design to help you beat all kinds of forms ๐Ÿ‘Š๐Ÿป.

Requirement#

To use React Cool Form, you must use react@16.8.0 or greater which includes hooks.

Installation#

This package is distributed via npm.

$ yarn add react-cool-form
# or
$ npm install --save react-cool-form

โš ๏ธ React Cool Form supports all major browsers. For older browsers (e.g. IE11) without the async/await, Promise, and ES6+ features, you'll need to include a polyfill such as core-js.

CDN#

If you're not using a module bundler or package manager. We also provide a UMD build which is available over the unpkg.com CDN. Simply use a <script> tag to add it after React CND links as below:

<script crossorigin src="https://unpkg.com/react/umd/react.production.min.js"></script>
<script crossorigin src="https://unpkg.com/react-dom/umd/react-dom.production.min.js"></script>
<!-- react-cool-form comes here -->
<script crossorigin src="https://unpkg.com/react-cool-form/dist/index.umd.production.min.js"></script>

Then you can access it via the window.ReactCoolForm.<moduleName> variables.

Example#

Here's the basic concept of how does it rocks:

Edit RCF - Quick start

import { useForm } from "react-cool-form";
const App = () => {
const { form, getState } = useForm({
// Provide the default values just like we use "React.useState" or "React.useReducer"
defaultValues: { username: "", email: "", password: "" },
// The event only triggered when the form is valid
onSubmit: (values) => console.log("onSubmit: ", values),
});
// React Cool Form filters the error of an un-blurred field by default (via the "filterUntouchedError" option)
// Which helps the user focus on typing without being annoying
const errors = getState("errors", { filterUntouchedError: true });
return (
<form ref={form} noValidate>
<div>
{/* Support built-in validation */}
<input name="username" placeholder="Username" required />
{errors.username && <p>{errors.username}</p>}
</div>
<div>
<input name="email" type="email" placeholder="Email" required />
{errors.email && <p>{errors.email}</p>}
</div>
<div>
<input
name="password"
type="password"
placeholder="Password"
required
minLength={6}
/>
{errors.password && <p>{errors.password}</p>}
</div>
<input type="submit" />
</form>
);
};

โœจ Pretty easy right? React Cool Form is more powerful than you think. Let's keep exploring!