Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/antonybudianto/react-save-localstorage

🗄 React component to save data to localStorage on render phase safely
https://github.com/antonybudianto/react-save-localstorage

local-storage localstorage react reactjs render-props

Last synced: 3 months ago
JSON representation

🗄 React component to save data to localStorage on render phase safely

Awesome Lists containing this project

README

        

# react-save-localstorage

[![npm version](https://badge.fury.io/js/react-save-localstorage.svg)](https://badge.fury.io/js/react-save-localstorage)
[![Build Status](https://travis-ci.org/antonybudianto/react-save-localstorage.svg?branch=master)](https://travis-ci.org/antonybudianto/react-save-localstorage)



Save and sync your data on render phase to localStorage safely

> Try it [live at StackBlitz](https://stackblitz.com/edit/demo-react-save-localstorage)

```js
import React, { Component } from 'react';
import SaveLocalStorage from 'react-save-localstorage';

class Home extends Component {
state = {
email: ''
};
render() {
return (


{/* Mode 1: read only */}

{lang =>
Language: {lang || '-'}
}

{/* Mode 2: write only */}

{/* Mode 3: dual */}

{v => (



this.setState({
email: e.target.value
})
}
/>
Welcome, {v || 'Guest'}


)}


);
}
}
```

## Props

- **lsKey** (string, required)
- **value** (string)
- **sync** (bool)

To sync after value updates, default is `true`

- **children** (func)

Function that accept loaded **value** from localStorage

- **onSave** (func)

Function that accept object containing:

- `init` (boolean that indicated it's the first mount)
- `value` (the value being saved)

This is called for every value updates.

## License

MIT