ESLint autofix
This commit is contained in:
@@ -1,23 +1,17 @@
|
||||
import { useCallback, useRef, useState } from "react";
|
||||
import { useCallback, useRef, useState } from 'react';
|
||||
|
||||
/**
|
||||
* Same as `useState`, but swallow all calls to `setState` if the value didn't change (uses `===` comparator per default)
|
||||
* @param initialValue The initial state
|
||||
* @param comparator A custom comparator, useful if you want to only round numbers or use string locale for comparison. Make sure this function is memoized!
|
||||
*/
|
||||
export const useCachedState = <T>(
|
||||
initialValue: T,
|
||||
comparator?: (oldState: T, newState: T) => boolean
|
||||
): [T, (newState: T) => void] => {
|
||||
export const useCachedState = <T>(initialValue: T, comparator?: (oldState: T, newState: T) => boolean): [T, (newState: T) => void] => {
|
||||
const [state, setState] = useState(initialValue);
|
||||
const cachedState = useRef(initialValue);
|
||||
|
||||
const dispatchState = useCallback(
|
||||
(newState: T) => {
|
||||
const areEqual =
|
||||
comparator == null
|
||||
? cachedState.current === newState
|
||||
: comparator(cachedState.current, newState);
|
||||
const areEqual = comparator == null ? cachedState.current === newState : comparator(cachedState.current, newState);
|
||||
if (areEqual) {
|
||||
return;
|
||||
} else {
|
||||
@@ -25,7 +19,7 @@ export const useCachedState = <T>(
|
||||
setState(newState);
|
||||
}
|
||||
},
|
||||
[comparator]
|
||||
[comparator],
|
||||
);
|
||||
|
||||
return [state, dispatchState];
|
||||
|
@@ -1,17 +1,17 @@
|
||||
import { useEffect } from "react"
|
||||
import { AppState, AppStateStatus } from "react-native";
|
||||
import { useCachedState } from "./useCachedState";
|
||||
import { useEffect } from 'react';
|
||||
import { AppState, AppStateStatus } from 'react-native';
|
||||
import { useCachedState } from './useCachedState';
|
||||
|
||||
export const useIsForeground = (): boolean => {
|
||||
const [isForeground, setIsForeground] = useCachedState(true);
|
||||
|
||||
useEffect(() => {
|
||||
const onChange = (state: AppStateStatus) => {
|
||||
setIsForeground(state === "active");
|
||||
setIsForeground(state === 'active');
|
||||
};
|
||||
AppState.addEventListener("change", onChange);
|
||||
return () => AppState.removeEventListener("change", onChange);
|
||||
AppState.addEventListener('change', onChange);
|
||||
return () => AppState.removeEventListener('change', onChange);
|
||||
}, [setIsForeground]);
|
||||
|
||||
return isForeground;
|
||||
}
|
||||
};
|
||||
|
@@ -1,23 +1,23 @@
|
||||
import { useEffect, useMemo, useReducer } from "react";
|
||||
import { Navigation } from "react-native-navigation";
|
||||
import { useEffect, useMemo, useReducer } from 'react';
|
||||
import { Navigation } from 'react-native-navigation';
|
||||
|
||||
type Action =
|
||||
| {
|
||||
action: "push";
|
||||
action: 'push';
|
||||
componentId: string;
|
||||
}
|
||||
| {
|
||||
action: "pop";
|
||||
action: 'pop';
|
||||
componentId: string;
|
||||
};
|
||||
|
||||
const reducer = (stack: string[], action: Action): string[] => {
|
||||
switch (action.action) {
|
||||
case "push": {
|
||||
case 'push': {
|
||||
stack.push(action.componentId);
|
||||
break;
|
||||
}
|
||||
case "pop": {
|
||||
case 'pop': {
|
||||
const index = stack.indexOf(action.componentId);
|
||||
if (index > -1) stack.splice(index, 1);
|
||||
break;
|
||||
@@ -28,34 +28,27 @@ const reducer = (stack: string[], action: Action): string[] => {
|
||||
|
||||
export const useIsScreenFocused = (componentId: string): boolean => {
|
||||
const [componentStack, dispatch] = useReducer(reducer, [componentId]);
|
||||
const isFocussed = useMemo(
|
||||
() => componentStack[componentStack.length - 1] === componentId,
|
||||
[componentStack, componentId]
|
||||
);
|
||||
const isFocussed = useMemo(() => componentStack[componentStack.length - 1] === componentId, [componentStack, componentId]);
|
||||
|
||||
useEffect(() => {
|
||||
const listener = Navigation.events().registerComponentDidAppearListener(
|
||||
(event) => {
|
||||
if (event.componentType !== "Component") return;
|
||||
dispatch({
|
||||
action: "push",
|
||||
componentId: event.componentId,
|
||||
});
|
||||
}
|
||||
);
|
||||
const listener = Navigation.events().registerComponentDidAppearListener((event) => {
|
||||
if (event.componentType !== 'Component') return;
|
||||
dispatch({
|
||||
action: 'push',
|
||||
componentId: event.componentId,
|
||||
});
|
||||
});
|
||||
|
||||
return () => listener.remove();
|
||||
}, []);
|
||||
useEffect(() => {
|
||||
const listener = Navigation.events().registerComponentDidDisappearListener(
|
||||
(event) => {
|
||||
if (event.componentType !== "Component") return;
|
||||
dispatch({
|
||||
action: "pop",
|
||||
componentId: event.componentId,
|
||||
});
|
||||
}
|
||||
);
|
||||
const listener = Navigation.events().registerComponentDidDisappearListener((event) => {
|
||||
if (event.componentType !== 'Component') return;
|
||||
dispatch({
|
||||
action: 'pop',
|
||||
componentId: event.componentId,
|
||||
});
|
||||
});
|
||||
|
||||
return () => listener.remove();
|
||||
}, []);
|
||||
|
Reference in New Issue
Block a user