DEV Community

Discussion on: Custom Hooks vs Class vs Other... What is your choice?

Collapse
 
crtdaniele profile image
Daniele Carta • Edited

Thanks for your answer!
Personally, in this moment I prefer to use only functional component with hooks (in my opinion hooks are the future in reactjs).

For example, in general I create a custom hooks with the business logic:

// useCategory

const dispatch = useDispatch();
const { list, isLoading, isError } = useSelector<RootState, ICategory>(state => state.category);

const getAll = () => {
    if(list && list.length === 0){
        dispatch(fetchCategory());
    }else{
        list && sortByName(list);
    }

    return { list, isLoading, isError };
}

const getSubCategory = () => {

}

return {
    getAll,
    getSubCategory
};

And in the FC I have only this:

const category = useCategory();

With this method, I can reuse useCategory in all of my components, and I think is better in case of re-factoring (coding/HTML).

Collapse
 
paritosh_pundir profile image
Paritosh Pundir

Yes, for sure it works perfectly fine! And the usage for sure depends on the scale as well, so for me, I always start with functional and the project itself tells me when to go the other way around. That is mostly to adapt to a plugins coding pattern or any other reason. 🖖