0.5.0 • Published 3 months ago

react-autosave v0.5.0

Weekly downloads
11
License
MIT
Repository
github
Last release
3 months ago

react-autosave

Tests codecov npm minified size

Auto save controlled form values as they are updated.

react-autosave is a lightweight solution for auto saving controlled form values. It handles many edge cases and guarantees a good user experience.

Examples

import React from 'react';
import axios from 'axios';

import { Autosave, useAutosave } from 'react-autosave';

const updateBlog = (data) => axios.post('myapi/blog/123', { text: data });

// Via hook
const EditBlogFormWithHook = () => {
  const [blogText, setBlogText] = React.useState('hello world');
  useAutosave({ data: blogText, onSave: updateBlog });
  return (
    <div>
      <input
        type="text"
        value={blogText}
        onChange={(e) => setBlogText(e.target.value)}
      />
    </div>
  );
};

// Via component
const EditBlogForm = () => {
  const [blogText, setBlogText] = React.useState('hello world');
  return (
    <div>
      <input
        type="text"
        value={blogText}
        onChange={(e) => setBlogText(e.target.value)}
      />
      <Autosave data={blogText} onSave={updateBlog} />
    </div>
  );
};

Installation

yarn add react-autosave

# or with npm...
npm i react-autosave

Features

  1. Written in typescript.

  2. Lightweight and simple.

  3. No dependencies.

API

PropTypeDescription
dataTDataThe controlled form value to be auto saved
onSave(data: TData) => anyThe callback function to save your data
interval (optional)numberThe number of milliseconds between save attempts. Defaults to 2000
saveOnUnmount (optional)booleanDefaults to true. Set to false to prevent saving on unmount

Contributing

Issues and PRs are more than welcome. Please clone the repo and setup your environment with:

pnpm

The test suite can be run with pnpm test Buid the library with pnpm build A demo page can be viewed with pnpm build && pnpm dev

0.5.0

3 months ago

0.4.4

1 year ago

0.4.3

2 years ago

0.4.1

3 years ago

0.4.2

3 years ago

0.4.0

3 years ago

0.3.1

3 years ago

0.3.0

4 years ago

0.2.1

4 years ago

0.2.0

4 years ago

0.2.2

4 years ago

0.1.4

5 years ago

0.1.5

5 years ago

0.1.3

5 years ago

0.1.2

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago