React obliczony hak
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;
}
}
});
Restu Wahyu Saputra