├── .all-contributorsrc ├── .prettierrc.json ├── LICENSE ├── README.md ├── demo ├── .buckconfig ├── .flowconfig ├── .gitattributes ├── .gitignore ├── .watchmanconfig ├── App.js ├── __tests__ │ └── App-test.js ├── android │ ├── app │ │ ├── BUCK │ │ ├── build.gradle │ │ ├── build_defs.bzl │ │ ├── proguard-rules.pro │ │ └── src │ │ │ ├── debug │ │ │ └── AndroidManifest.xml │ │ │ └── main │ │ │ ├── AndroidManifest.xml │ │ │ ├── java │ │ │ └── com │ │ │ │ └── demo │ │ │ │ ├── MainActivity.java │ │ │ │ └── MainApplication.java │ │ │ └── res │ │ │ ├── mipmap-hdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-mdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-xhdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-xxhdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-xxxhdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ │ └── values │ │ │ ├── strings.xml │ │ │ └── styles.xml │ ├── build.gradle │ ├── gradle.properties │ ├── gradle │ │ └── wrapper │ │ │ ├── gradle-wrapper.jar │ │ │ └── gradle-wrapper.properties │ ├── gradlew │ ├── gradlew.bat │ ├── keystores │ │ ├── BUCK │ │ └── debug.keystore.properties │ └── settings.gradle ├── app.json ├── babel.config.js ├── images │ ├── hook.png │ ├── scrsht01.png │ └── scrshtdebug.png ├── index.js ├── ios │ ├── demo-tvOS │ │ └── Info.plist │ ├── demo-tvOSTests │ │ └── Info.plist │ ├── demo.xcodeproj │ │ ├── project.pbxproj │ │ └── xcshareddata │ │ │ └── xcschemes │ │ │ ├── demo-tvOS.xcscheme │ │ │ └── demo.xcscheme │ ├── demo │ │ ├── AppDelegate.h │ │ ├── AppDelegate.m │ │ ├── Base.lproj │ │ │ └── LaunchScreen.xib │ │ ├── Images.xcassets │ │ │ ├── AppIcon.appiconset │ │ │ │ └── Contents.json │ │ │ └── Contents.json │ │ ├── Info.plist │ │ └── main.m │ └── demoTests │ │ ├── Info.plist │ │ └── demoTests.m ├── metro.config.js ├── package.json └── yarn.lock ├── index.d.ts ├── index.js ├── package.json └── react-native-hooks-persist.podspec /.all-contributorsrc: -------------------------------------------------------------------------------- 1 | { 2 | "files": [ 3 | "README.md" 4 | ], 5 | "imageSize": 100, 6 | "commit": false, 7 | "contributors": [ 8 | { 9 | "login": "Yaxian", 10 | "name": "Yaxian", 11 | "avatar_url": "https://avatars1.githubusercontent.com/u/9707805?v=4", 12 | "profile": "https://github.com/Yaxian", 13 | "contributions": [ 14 | "code" 15 | ] 16 | } 17 | ], 18 | "contributorsPerLine": 7, 19 | "projectName": "react-native-hooks-persist", 20 | "projectOwner": "rcerrejon", 21 | "repoType": "github", 22 | "repoHost": "https://github.com" 23 | } 24 | -------------------------------------------------------------------------------- /.prettierrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "trailingComma": "es5", 3 | "spaceWidth": 2, 4 | "semi": true, 5 | "singleQuote": true 6 | } 7 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Rafael Cerrejon 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # React-Native Hooks Persist 2 | 3 | 4 | 5 | React Native Hooks Persist with Async Storage 6 | 7 | ![language](https://img.shields.io/github/languages/top/rcerrejon/react-native-hooks-persist.svg) 8 | 9 | [![All Contributors](https://img.shields.io/badge/all_contributors-1-orange.svg?style=flat-square)](#contributors) 10 | 11 | ![NPM](https://img.shields.io/npm/l/react-native-hooks-persist.svg) 12 | 13 | ![GitHub issues](https://img.shields.io/github/issues-raw/rcerrejon/react-native-hooks-persist.svg) 14 | 15 | ![GitHub stars](https://img.shields.io/github/stars/rcerrejon/react-native-hooks-persist.svg?style=social) 16 | 17 | ## Getting Started 18 | 19 | ### Install 20 | 21 | `yarn add react-native-hooks-persist` 22 | 23 | or 24 | 25 | `npm install react-native-hooks-persist` 26 | 27 | ### Methods 28 | 29 | | Name | Description | 30 | | :----------------- | :------------------------ | 31 | | useStatePersist | Main hook persist method | 32 | | removeStatePersist | Remove an item from Store | 33 | | debugStatePersist | Debug all items on Store | 34 | 35 | ## Basic usage 36 | 37 | ### imports 38 | 39 | ``` 40 | import { 41 | useStatePersist, 42 | removeStatePersist, 43 | debugStatePersist, 44 | } from 'react-native-hooks-persist'; 45 | 46 | const DB_NAME = 'globalStorage'; 47 | const FIELD_KEY = 'selectedColor'; 48 | const INITIAL_VALUE = 'Red'; 49 | ``` 50 | 51 | ### Create 52 | 53 | `const [color, setColor] = useStatePersist(DB_NAME, FIELD_KEY, INITIAL_VALUE);` 54 | 55 | ### Read 56 | 57 | `{color.selectedColor} Selected` 58 | 59 | ### Update 60 | 61 | `setColor(DB_NAME, FIELD_KEY, 'Blue');` 62 | 63 | ### Update with an Object (MultiSet) 64 | 65 | `setValue(DB_NAME, { [FIELD_KEY]: 4, [FIELD_KEY_COLOR]: 'Black' });` 66 | 67 | ### Delete 68 | 69 | `removeStatePersist(DB_NAME, FIELD_KEY);` 70 | 71 | ### Debug all Items 72 | 73 | `debugStatePersist(DB_NAME);` 74 | 75 | 76 | 77 | ## Example in `/demo` 78 | 79 | ### Run in demo folder: 80 | 81 | - `yarn` or `npm install` 82 | 83 | - `react-native start` 84 | 85 | - `react-native run-ios` or `react-native run-android` 86 | 87 | 88 | 89 | ## Contributing 90 | 91 | - Clone this Repo 92 | - Install dependencies 93 | - Checkout develop branch 94 | - Add your feature or fix 95 | - Make sure your improvement works 96 | - Open a pull request 97 | 98 | --- 99 | 100 | Baked with Love ♥ from Cologne & Huelva 101 | 102 | ## Contributors ✨ 103 | 104 | Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)): 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 |
Yaxian
Yaxian

💻
113 | 114 | 115 | 116 | This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome! 117 | -------------------------------------------------------------------------------- /demo/.buckconfig: -------------------------------------------------------------------------------- 1 | 2 | [android] 3 | target = Google Inc.:Google APIs:23 4 | 5 | [maven_repositories] 6 | central = https://repo1.maven.org/maven2 7 | -------------------------------------------------------------------------------- /demo/.flowconfig: -------------------------------------------------------------------------------- 1 | [ignore] 2 | ; We fork some components by platform 3 | .*/*[.]android.js 4 | 5 | ; Ignore "BUCK" generated dirs 6 | /\.buckd/ 7 | 8 | ; Ignore unexpected extra "@providesModule" 9 | .*/node_modules/.*/node_modules/fbjs/.* 10 | 11 | ; Ignore duplicate module providers 12 | ; For RN Apps installed via npm, "Libraries" folder is inside 13 | ; "node_modules/react-native" but in the source repo it is in the root 14 | .*/Libraries/react-native/React.js 15 | 16 | ; Ignore polyfills 17 | .*/Libraries/polyfills/.* 18 | 19 | ; Ignore metro 20 | .*/node_modules/metro/.* 21 | 22 | [include] 23 | 24 | [libs] 25 | node_modules/react-native/Libraries/react-native/react-native-interface.js 26 | node_modules/react-native/flow/ 27 | 28 | [options] 29 | emoji=true 30 | 31 | esproposal.optional_chaining=enable 32 | esproposal.nullish_coalescing=enable 33 | 34 | module.system=haste 35 | module.system.haste.use_name_reducers=true 36 | # get basename 37 | module.system.haste.name_reducers='^.*/\([a-zA-Z0-9$_.-]+\.js\(\.flow\)?\)$' -> '\1' 38 | # strip .js or .js.flow suffix 39 | module.system.haste.name_reducers='^\(.*\)\.js\(\.flow\)?$' -> '\1' 40 | # strip .ios suffix 41 | module.system.haste.name_reducers='^\(.*\)\.ios$' -> '\1' 42 | module.system.haste.name_reducers='^\(.*\)\.android$' -> '\1' 43 | module.system.haste.name_reducers='^\(.*\)\.native$' -> '\1' 44 | module.system.haste.paths.blacklist=.*/__tests__/.* 45 | module.system.haste.paths.blacklist=.*/__mocks__/.* 46 | module.system.haste.paths.blacklist=/node_modules/react-native/Libraries/Animated/src/polyfills/.* 47 | module.system.haste.paths.whitelist=/node_modules/react-native/Libraries/.* 48 | 49 | munge_underscores=true 50 | 51 | module.name_mapper='^[./a-zA-Z0-9$_-]+\.\(bmp\|gif\|jpg\|jpeg\|png\|psd\|svg\|webp\|m4v\|mov\|mp4\|mpeg\|mpg\|webm\|aac\|aiff\|caf\|m4a\|mp3\|wav\|html\|pdf\)$' -> 'RelativeImageStub' 52 | 53 | module.file_ext=.js 54 | module.file_ext=.jsx 55 | module.file_ext=.json 56 | module.file_ext=.native.js 57 | 58 | suppress_type=$FlowIssue 59 | suppress_type=$FlowFixMe 60 | suppress_type=$FlowFixMeProps 61 | suppress_type=$FlowFixMeState 62 | 63 | suppress_comment=\\(.\\|\n\\)*\\$FlowFixMe\\($\\|[^(]\\|(\\(\\)? *\\(site=[a-z,_]*react_native[a-z,_]*\\)?)\\) 64 | suppress_comment=\\(.\\|\n\\)*\\$FlowIssue\\((\\(\\)? *\\(site=[a-z,_]*react_native[a-z,_]*\\)?)\\)?:? #[0-9]+ 65 | suppress_comment=\\(.\\|\n\\)*\\$FlowFixedInNextDeploy 66 | suppress_comment=\\(.\\|\n\\)*\\$FlowExpectedError 67 | 68 | [version] 69 | ^0.107.0 70 | -------------------------------------------------------------------------------- /demo/.gitattributes: -------------------------------------------------------------------------------- 1 | *.pbxproj -text 2 | -------------------------------------------------------------------------------- /demo/.gitignore: -------------------------------------------------------------------------------- 1 | # OSX 2 | # 3 | .DS_Store 4 | 5 | # Xcode 6 | # 7 | build/ 8 | *.pbxuser 9 | !default.pbxuser 10 | *.mode1v3 11 | !default.mode1v3 12 | *.mode2v3 13 | !default.mode2v3 14 | *.perspectivev3 15 | !default.perspectivev3 16 | xcuserdata 17 | *.xccheckout 18 | *.moved-aside 19 | DerivedData 20 | *.hmap 21 | *.ipa 22 | *.xcuserstate 23 | project.xcworkspace 24 | 25 | # Android/IntelliJ 26 | # 27 | build/ 28 | .idea 29 | .gradle 30 | local.properties 31 | *.iml 32 | 33 | # node.js 34 | # 35 | node_modules/ 36 | npm-debug.log 37 | yarn-error.log 38 | 39 | # BUCK 40 | buck-out/ 41 | \.buckd/ 42 | *.keystore 43 | 44 | # fastlane 45 | # 46 | # It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the 47 | # screenshots whenever they are needed. 48 | # For more information about the recommended setup visit: 49 | # https://docs.fastlane.tools/best-practices/source-control/ 50 | 51 | */fastlane/report.xml 52 | */fastlane/Preview.html 53 | */fastlane/screenshots 54 | 55 | # Bundle artifact 56 | *.jsbundle 57 | -------------------------------------------------------------------------------- /demo/.watchmanconfig: -------------------------------------------------------------------------------- 1 | {} -------------------------------------------------------------------------------- /demo/App.js: -------------------------------------------------------------------------------- 1 | /** 2 | * React-Native Hooks Persist Demo 3 | * by: Rafael Cerrejon 4 | * https://github.com/facebook/react-native 5 | * 6 | * @format 7 | * @flow 8 | */ 9 | 10 | import React from 'react'; 11 | import { StyleSheet, Text, View, Button } from 'react-native'; 12 | import { 13 | useStatePersist, 14 | removeStatePersist, 15 | debugStatePersist, 16 | } from 'react-native-hooks-persist'; 17 | 18 | const App = (props: any) => { 19 | const DB_NAME = 'globalStorage'; 20 | 21 | const FIELD_KEY = 'buttonSelected'; 22 | const INITIAL_VALUE = 'No'; 23 | 24 | const [value, setValue] = useStatePersist(DB_NAME, FIELD_KEY, INITIAL_VALUE); 25 | 26 | const FIELD_KEY_COLOR = 'color'; 27 | 28 | return ( 29 | 30 | Welcome to React Native Hooks Persist! 31 | 32 |