0.2.9 • Published 2 years ago

bresnow_utility-react-hooks v0.2.9

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
2 years ago

bresnow_utility-react-hooks

Custom react hooks I have built and use regularly.

Image

yarn add bresnow_utility-react-hooks

useIf

If statement hook that returns statement if condition is true. Useful when a normal if statement is causing infinite render errors. Conditions are listed as an array dependencies and eliminates the need for && operators. Optional else callback parameter to run when condition is false. Note: else option runs on every render the conditions !== true.

{else: ()=>void }
import { useIf } from "bresnow_utility-react-hooks";

 useIf( [auth.keys, auth.isLoggedIn === true ],
  () => {

  nodepathSet(`~${auth.keys?.pub}.${path}`);

  }, { else: 
  () => {

      log("condition is still not true");

  }});

  log(nodepath, "nodepath");   
  //  ['~YQus5nDLVi5PG5BJXGTLoizIWbnrNN9NRER3-0RbqV0.eaV67IswAG3zCf5C5qqR7mF7EwgfmqIsjgf1MDhSNPA.pages.index', 'nodepath']

useContextReducer

Create contexts and providers as if using "useReducer".

import { useContextReducer } from "bresnow_utility-react-hooks";

const initialState = {
  hidden: true,
  cartItems: [],
  address: null,
};

  function cartReducer(state: State, action: Action): State {
  console.log(state, 'context state');

  switch (action.type) {
    case 'ADD_ITEM':
      return {
        ...state,
        cartItems: addItemToCart(state.cartItems, action.payload),
      };
    case 'CLEAR_CART':
      return {
        ...state,
        cartItems: [],
      };
    default: {
       throw new Error(`Unhandled action type: ${action.type}`);
     }
  }
  

  const [useCartState, useCartDispatch, CartProvider] = useContextReducer(
  cartReducer,
  initialState
);

useSafeEffect

runs useEffect when window is undefined and useLayoutEffect when window is not undefined.

useSafeCallback

useCallback that runs only when mounted and takes no dependencies.

useSafeReducer

useReducer but the dispatch runs only when mounted.

0.2.9

2 years ago

0.2.7

2 years ago

0.2.6

2 years ago

0.2.5

2 years ago

0.2.4

2 years ago

0.2.3

2 years ago

0.2.2

2 years ago

0.2.1

2 years ago

0.2.0

2 years ago

0.1.9

2 years ago

0.1.8

2 years ago

0.1.73

2 years ago

0.1.72

2 years ago

0.1.71

2 years ago

0.1.7

2 years ago

0.1.6

2 years ago

0.1.5

2 years ago

0.1.4

2 years ago

0.1.35

2 years ago

0.1.34

2 years ago

0.1.33

2 years ago

0.1.32

2 years ago

0.1.31

2 years ago

0.1.3

2 years ago

0.1.2

2 years ago

0.1.1

2 years ago