View context_example.js
import { createContext, useContext } from 'react'; | |
import PropTypes from 'prop-types'; | |
const ExampleContext = createContext(); | |
function ExampleProvider({ children }) { | |
return ( | |
<ExampleContext.Provider value={}> | |
{children} |
View checkBrowser.js
const inBrowser = typeof window !== 'undefined'; | |
// CHECK USER AGENT | |
const UA = inBrowser && window.navigator.userAgent.toLowerCase(); | |
const isIE = UA && /msie|trident/.test(UA); | |
const isIE9 = UA && UA.indexOf('msie 9.0') > 0; | |
const isEdge = UA && UA.indexOf('edge/') > 0; | |
const isAndroid = UA && UA.indexOf('android') > 0; |
View useClikcOutside.tsx
import { useEffect } from 'react'; | |
const useClickOutside = ( | |
ref: React.MutableRefObject<HTMLElement>, | |
handler: (event: MouseEvent | TouchEvent) => void) => { | |
useEffect(()=>{ | |
const listener = (event: MouseEvent | TouchEvent) => { | |
if(!ref.current || ref.current.contains(event.target as Node)) { |
View usePrevious.js
import { useRef, useEffect } from 'react'; | |
/** | |
* Custom React Hook usePrevious | |
* @returns ref.current {current: "ANY"} | |
* @default undefined | |
* @description It store the previous state when state changes. | |
* @example const previousState = usePrevios(state) | |
*/ |
View useWindowSize.js
import { useState, useEffect } from 'react'; | |
/** | |
* Custom React Hook useWindowSize | |
* @returns width: {Number}, height: {Number} | |
*/ | |
export function useWindowSize() { | |
const isClient = typeof window === 'object'; |