https://github.com/henrytao-me/redux-persist-model
Persist Model for redux-persist
https://github.com/henrytao-me/redux-persist-model
persistent react react-native redux redux-persist
Last synced: 3 months ago
JSON representation
Persist Model for redux-persist
- Host: GitHub
- URL: https://github.com/henrytao-me/redux-persist-model
- Owner: henrytao-me
- License: apache-2.0
- Created: 2017-04-14T16:06:15.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2017-06-26T00:41:48.000Z (almost 8 years ago)
- Last Synced: 2024-10-29T21:03:08.641Z (7 months ago)
- Topics: persistent, react, react-native, redux, redux-persist
- Language: JavaScript
- Homepage:
- Size: 16.6 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
### [DEPRECATED] Use [redux-persist-transform-immutable](https://github.com/rt2zz/redux-persist-transform-immutable) instead
# redux-persist-model
Immutable Model for Redux Persist - backed by [Record Immutable](https://facebook.github.io/immutable-js/docs/#/Record)
## Installation
```js
npm install redux-persist-model --save
```## Sample app
Checkout [https://github.com/henrytao-me/react-native-workshop](https://github.com/henrytao-me/react-native-workshop)
## Usages
### Define Model
```js
// user.js
import { Model } from 'redux-persist-model'const Base = Model.create('User', {
id: undefined,
firstName: 'default-value',
lastName: 'default-value',
username: 'default-value'
})export default class User extends Base {
getFullName() {
return `${this.firstName} ${this.lastName}`
}
}
```### Use Model
```js
const user = new User({
id: 'some-user-id',
firstName: 'user-first-name',
lastName: 'user-last-name',
username: 'user-name'
})user.firstName
user.lastName
user.getFullName()
user.get('firstName')
user.get('firstName', 'default-value')
```### Integrate with redux-persist using applyModelTransform
```js
// store.js
import { AsyncStorage } from 'react-native'
import { persistStore } from 'redux-persist'
import { applyModelTransform } from 'redux-persist-model'import UserModel from './user'
const MODELS = [UserModel, ...SomeOtherModels]
const STORE = ...persistStore(STORE, {
storage: AsyncStorage,
transforms: [applyModelTransform(MODELS)]
})
```### Lazy load component with PersistModelProvider
If you want to wait until redux-persist is rehydrated before rendering components to UI, you can use `PersistModelProvider`
```js
// store.js
import { AsyncStorage } from 'react-native'
import { persistStore } from 'redux-persist'
import { applyModelTransform, PersistModelProvider } from 'redux-persist-model'import UserModel from './user'
const MODELS = [UserModel, ...SomeOtherModels]
const STORE = ...persistStore(STORE, {
storage: AsyncStorage,
transforms: [applyModelTransform(MODELS)]
}, () => PersistModelProvider.rehydrated())// main.js
import { Provider } from 'react-redux'
import { PersistModelProvider } from 'redux-persist-model'import HomeComponent './home'
export default class Main extends PureComponent {
render() {
return (
)
}
}
```## License
Copyright 2017 "Henry Tao "
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License athttp://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.