This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useReducer } from 'react' | |
function arrayReducer(array, action) { | |
switch (action.type) { | |
case 'push': | |
return [...array, action.value] | |
case 'pushAll': | |
return [...array, ...action.value] | |
case 'deleteByIndex': | |
let deleteByIndex = array.filter((x, i) => i != action.index) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { AsyncStorage } from 'react-native' // change this to @react-native-community/async-storage when this issue is solved https://github.com/react-native-community/react-native-async-storage/issues/14 | |
export default Storage = { | |
async getItem(itemKey) { | |
let result = { | |
key: itemKey, | |
value: null, | |
error: null, | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://overreacted.io/making-setinterval-declarative-with-react-hooks/ | |
import { useEffect, useRef } from 'react' | |
function useInterval(callback, delay) { | |
const savedCallback = useRef() | |
// Remember the latest callback. | |
useEffect(() => { | |
savedCallback.current = callback | |
}, [callback]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useState, useEffect } from 'react' | |
import PropTypes from 'prop-types' | |
import { checkIsAuthenticated, authSignUp, authLogin, authLogout } from '../../services/auth' | |
export const AuthContext = React.createContext({}) | |
export default function Auth({ children }) { | |
const [isAuthenticated, setIsAuthenticated] = useState(false) | |
const [isLoading, setIsLoading] = useState(true) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.li { | |
max-height: 0px; | |
transition: max-height 0.35s ease-out; | |
} | |
.ul { | |
overflow: hidden; | |
} | |
.ul.opened .li { | |
max-height: 50px; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useState, useEffect } from 'react' | |
import { Dimensions } from 'react-native' | |
const useDimensions = getter => { | |
const [dimensions, setDimensions] = useState(Dimensions.get(getter)) | |
useEffect(() => { | |
const widthHandler = d => setDimensions(d[getter]) | |
Dimensions.addEventListener('change', widthHandler) | |
return () => Dimensions.removeEventListener('change', widthHandler) | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useEffect, useRef, useState } from 'react' | |
// The callback should setState so it will re trigger the timeout | |
function useTimeout(callback, delay) { | |
const savedCallback = useRef() | |
const [callBackCleanUp, setCallBackCleanUp] = useState(null) | |
// Remember the latest callback. | |
useEffect(() => { | |
savedCallback.current = callback | |
}, [callback]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useState, useEffect } from 'react' | |
import Realm from '../database/migrations' | |
function reducer(action, { realm, name, queryObjects }) { | |
switch (action.type) { | |
case 'create': | |
return new Promise((resolve, reject) => { | |
try { | |
realm.write(() => { | |
resolve(realm.create(name, action.data)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useState, useEffect } from 'react' | |
import AsyncStorage from '@react-native-community/async-storage' | |
const useAsyncStorage = (key, initialValue) => { | |
const [hasLoad, setHasLoad] = useState(false) | |
const [data, setData] = useState(initialValue) | |
const set = async newData => { | |
setData(newData) | |
return newData === null ? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useState } from 'react' | |
export default function useToggle(defaultValue){ | |
const [state, setState] = useState(defaultValue) | |
const toggle = (value = null) => { | |
if(value == null) | |
setState(!state) | |
else | |
setState(!!value) |
OlderNewer