包详细信息

react-autosave

jollyjerr25kMIT0.5.0

A lightweight solution for auto saving controlled form values

react, autosave, form

自述文件

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

Prop Type Description
data TData The controlled form value to be auto saved
onSave (data: TData) => any The callback function to save your data
interval (optional) number The number of milliseconds between save attempts. Defaults to 2000
saveOnUnmount (optional) boolean Defaults 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

更新日志

Changelog

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog, and this project adheres to Semantic Versioning.

[0.5.0]

Changed

  • Prevent unnecessary save calls in react dev mode.
  • Support react 19

[0.4.4]

Changed

  • fix: resolve react and react-dom dependency conflicts

[0.4.3]

Changed

  • Fix type definitions for ESM and formally remove cjs support
  • Updated all dev dependencies and tooling

[0.4.2]

Changed

  • Fixed export paths for production build

[0.4.1] - 10-23-22

Added

  • Prettier code formatting

Changed

  • useAutosave will not fire the onSave callback if the definition of onSave changes

[0.4.0] - 5-15-22

Added

  • A toggle to cancel saving on unmount saveOnUnmount
  • The ability to save falsy values
  • Dev page using vite to preview current build
  • React 18 support

Changed

  • Package builds using vite
  • Switched to PNPM
  • Test run using vitest
  • All dependencies bumped to latest version

[0.3.1] - 12-21-21

Added

  • A tool-versions file to track node version with asdf

Changed

  • Bumped the project to node 16.13.0
  • Fixed "Main" and "Module" path in package.json to prevent warnings in node 16

[0.3.0] - 10-16-21

Added

  • Linting and format checking.

Changed

  • Hook and component both fire on unmount with current, not debounced, value.

[0.2.2] - 5-18-21

Added

  • Test coverage report.

Removed

  • lodash dependency.

[0.2.0] - 5-16-21

Added

  • useAutosave hook.

[0.1.5] - 10-10-20

Added

  • <Autosave> component.
  • Readme.