├── .gitignore ├── .npmignore ├── CODE_OF_CONDUCT.md ├── LICENSE ├── README.md ├── dist ├── index.d.ts └── index.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | .DS_Store 3 | build 4 | !.eslintrc 5 | yarn-error.log 6 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | src 3 | .DS_Store 4 | build 5 | !.eslintrc 6 | yarn-error.log 7 | 8 | *.js 9 | !dist/index.js 10 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as 6 | contributors and maintainers pledge to making participation in our project and 7 | our community a harassment-free experience for everyone, regardless of age, body 8 | size, disability, ethnicity, sex characteristics, gender identity and expression, 9 | level of experience, education, socio-economic status, nationality, personal 10 | appearance, race, religion, or sexual identity and orientation. 11 | 12 | ## Our Standards 13 | 14 | Examples of behavior that contributes to creating a positive environment 15 | include: 16 | 17 | * Using welcoming and inclusive language 18 | * Being respectful of differing viewpoints and experiences 19 | * Gracefully accepting constructive criticism 20 | * Focusing on what is best for the community 21 | * Showing empathy towards other community members 22 | 23 | Examples of unacceptable behavior by participants include: 24 | 25 | * The use of sexualized language or imagery and unwelcome sexual attention or 26 | advances 27 | * Trolling, insulting/derogatory comments, and personal or political attacks 28 | * Public or private harassment 29 | * Publishing others' private information, such as a physical or electronic 30 | address, without explicit permission 31 | * Other conduct which could reasonably be considered inappropriate in a 32 | professional setting 33 | 34 | ## Our Responsibilities 35 | 36 | Project maintainers are responsible for clarifying the standards of acceptable 37 | behavior and are expected to take appropriate and fair corrective action in 38 | response to any instances of unacceptable behavior. 39 | 40 | Project maintainers have the right and responsibility to remove, edit, or 41 | reject comments, commits, code, wiki edits, issues, and other contributions 42 | that are not aligned to this Code of Conduct, or to ban temporarily or 43 | permanently any contributor for other behaviors that they deem inappropriate, 44 | threatening, offensive, or harmful. 45 | 46 | ## Scope 47 | 48 | This Code of Conduct applies both within project spaces and in public spaces 49 | when an individual is representing the project or its community. Examples of 50 | representing a project or community include using an official project e-mail 51 | address, posting via an official social media account, or acting as an appointed 52 | representative at an online or offline event. Representation of a project may be 53 | further defined and clarified by project maintainers. 54 | 55 | ## Enforcement 56 | 57 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 58 | reported by contacting the project team at amr.h.labib@gmail.com. All 59 | complaints will be reviewed and investigated and will result in a response that 60 | is deemed necessary and appropriate to the circumstances. The project team is 61 | obligated to maintain confidentiality with regard to the reporter of an incident. 62 | Further details of specific enforcement policies may be posted separately. 63 | 64 | Project maintainers who do not follow or enforce the Code of Conduct in good 65 | faith may face temporary or permanent repercussions as determined by other 66 | members of the project's leadership. 67 | 68 | ## Attribution 69 | 70 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, 71 | available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html 72 | 73 | [homepage]: https://www.contributor-covenant.org 74 | 75 | For answers to common questions about this code of conduct, see 76 | https://www.contributor-covenant.org/faq 77 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Amr Labib 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-appstate-hook 2 | 3 | React Native appState hook is a custom [react hook](https://reactjs.org/docs/hooks-intro.html), built to handle iOS or Android `appState` in your react component 4 | 5 | #### Note: 6 | 7 | React hooks is available from react version 16.8.0 and react native version 0.59.0 8 | 9 | --- 10 | 11 | ## Setup 12 | 13 | `yarn add react-native-appstate-hook` 14 | 15 | OR 16 | 17 | `npm install --save react-native-appstate-hook` 18 | 19 | --- 20 | 21 | ## Example 22 | 23 | ```javascript 24 | import React from 'react'; 25 | import { Text, View } from 'react-native'; 26 | import useAppState from 'react-native-appstate-hook'; 27 | 28 | 29 | export default function App() { 30 | const { appState } = useAppState({ 31 | onChange: (newAppState) => console.warn('App state changed to ', newAppState), 32 | onForeground: () => console.warn('App went to Foreground'), 33 | onBackground: () => console.warn('App went to background'), 34 | }); 35 | 36 | return ( 37 | 38 | App State is: {appState} 39 | 40 | ); 41 | } 42 | 43 | 44 | ``` 45 | 46 | --- 47 | 48 | ## Settings 49 | 50 | | key | Type | Required | Description | 51 | | --- | --- | --- | ---- | 52 | | onChange | Function | No | callback function to be executed once `appState` is changed | 53 | | onForeground | Function | No | callback function to be executed once app go to foreground | 54 | | onBackground | Function | No | callback function to be executed once app go to background | 55 | 56 | --- 57 | 58 | ## Values 59 | 60 | | key | Type | Description | 61 | | --- | --- | ---- | 62 | | appState | string | app state it can be one of the following values `active`, `inactive`, or `background` | 63 | -------------------------------------------------------------------------------- /dist/index.d.ts: -------------------------------------------------------------------------------- 1 | declare module "react-native-appstate-hook" { 2 | import { AppStateStatus } from "react-native"; 3 | 4 | export interface AppStateHookSettings { 5 | onChange?: (status: AppStateStatus) => void; 6 | onForeground?: () => void; 7 | onBackground?: () => void; 8 | } 9 | 10 | export interface AppStateHookResult { 11 | appState: AppStateStatus; 12 | } 13 | 14 | function useAppState(settings?: AppStateHookSettings): AppStateHookResult; 15 | 16 | export default useAppState; 17 | } 18 | -------------------------------------------------------------------------------- /dist/index.js: -------------------------------------------------------------------------------- 1 | import { useState, useEffect } from 'react'; 2 | import { AppState } from 'react-native'; 3 | 4 | export default function useAppState(settings) { 5 | const { onChange, onForeground, onBackground } = settings || {}; 6 | const [appState, setAppState] = useState(AppState.currentState); 7 | 8 | useEffect(() => { 9 | function handleAppStateChange(nextAppState) { 10 | if (nextAppState === 'active' && appState !== 'active') { 11 | isValidFunction(onForeground) && onForeground(); 12 | } else if (appState === 'active' && nextAppState.match(/inactive|background/)) { 13 | isValidFunction(onBackground) && onBackground(); 14 | } 15 | setAppState(nextAppState); 16 | isValidFunction(onChange) && onChange(nextAppState); 17 | } 18 | const appState = AppState.addEventListener('change', handleAppStateChange); 19 | 20 | return () => appState.remove(); 21 | }, [onChange, onForeground, onBackground, appState]); 22 | 23 | // settings validation 24 | function isValidFunction(func) { 25 | return func && typeof func === 'function'; 26 | } 27 | return { appState }; 28 | } 29 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-native-appstate-hook", 3 | "version": "1.0.6", 4 | "description": "React Native appState hook", 5 | "main": "dist/index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/amrlabib/react-native-appstate-hook.git" 12 | }, 13 | "keywords": [ 14 | "react", 15 | "react-native", 16 | "hook", 17 | "component", 18 | "hooks", 19 | "appState", 20 | "app", 21 | "state", 22 | "iOS", 23 | "Android" 24 | ], 25 | "author": "amrlabib", 26 | "license": "ISC", 27 | "bugs": { 28 | "url": "https://github.com/amrlabib/react-native-appstate-hook/issues" 29 | }, 30 | "homepage": "https://github.com/amrlabib/react-native-appstate-hook#readme", 31 | "peerDependencies": { 32 | "react": "^16.8.0", 33 | "react-native": "^0.59.0" 34 | } 35 | } 36 | --------------------------------------------------------------------------------