Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

react computed hook

import { useMemo } from "react";

export const useComputed = (props) => {
  const keys = Object.keys(props.computed);
  const objects = {};
  return useMemo(() => {
    for (let i in keys) {
      Object.defineProperties(objects, {
        [keys[i]]: {
          value: props.computed[keys[i]].call(),
          writable: true,
          enumerable: true
        }
      });
    }
    return objects;
  }, [props]);
};

// call custom hooks like this
const { loading, users } = useComputed({
  computed: {
    users() {
      return state.users;
    },
    loading() {
      return state.loading;
    }
  }
});
 
PREVIOUS NEXT
Tagged: #react #computed #hook
ADD COMMENT
Topic
Name
1+8 =