Skip to content

Repository files navigation

Formcarry React

React library of formcarry.

Getting Started

Run this command to install with yarn:

yarn add @formcarry/react

or with npm:

npm install --save @formcarry/react

You have to have React as a dependency in your project in order to use this library.

Also this package uses React Hooks, therefore you have to use React >= 16.8.0

Example

A simple demonstration with React library:

import{useForm}from'@formcarry/react';functionMyFormcarry(){// Call the `useForm` hook in your function componentconst{state, submit}=useForm({id: 'Your-Form-ID-From-Formcarry'});// Success messageif(state.submitted){return<div>Thank you! We received your submission.</div>;}return(<formonSubmit={submit}><labelhtmlFor="name">Name</label><inputid="name"type="text"name="text"/><labelhtmlFor="surname">Surname</label><inputid="surname"type="text"name="surname"/><labelhtmlFor="email">Email</label><inputid="email"type="email"name="email"/><labelhtmlFor="message">Message</label><textareaid="message"name="message"/><buttontype="submit">Send</button></form>);}

You have to use a <form> element and pass submit as the onSubmit handler.

Destructuring with different field name

We return state and submit from useForm by default, but you can rename it to whatever you want, like this:

import{useForm}from'@formcarry/react';functionMyFormcarry(){const{state: formcarryState,submit: formcarrySubmit}=useForm({id: 'Your-Form-ID-From-Formcarry'});if(formcarryState.submitted){return<div>Thank you! We received your submission.</div>;}return(<formonSubmit={formcarrySubmit}><labelhtmlFor="name">Name</label><inputid="name"type="text"name="text"/><labelhtmlFor="surname">Surname</label><inputid="surname"type="text"name="surname"/><labelhtmlFor="email">Email</label><inputid="email"type="email"name="email"/><labelhtmlFor="message">Message</label><textareaid="message"name="message"/><buttontype="submit">Send</button></form>);}

Example with Extra Data

import{useForm}from'@formcarry/react';functionMyFormcarry(){// Call the `useForm` hook in your function componentconst{state, submit}=useForm({id: 'Your-Form-ID-From-Formcarry',extraData: {// add whatever you wantscreenSize: `${window.screen.width}x${window.screen.height}`,language: window.navigator.language,}});
...
}

You can pass those to useForm:

KeyDescription
idYour Form ID, which you can get it from formcarry
debugBoolean, it prints out logs to the console, true by default
extraDataAccepts object, it will mix those object with form fields

The state object contains the following:

KeyDescription
submittingA Boolean indicating whether the form is currently submitting
submittedA Boolean indicating whether the form successfully submitted
responseReturns formcarry successful response
errorReturns formcarry error

Releases

Packages

Used by

Contributors

Languages