user18113903
user18113903

Reputation:

How to move some hook in separate file ? React hook

I want to separate my function with hook because i have a lot of in one file....

example of code:

  const collectOrganisationUnitNode = useCallback((treeData) => {
       // someLogic
  }, []);

  const handleCancelClick = useCallback(() => {
     .../some logic
  }, [hideModal, selected, multiselect, tree]);

I want to remove from index.js and set to another file then just import and using inside index.js

is that possible ?

Upvotes: 0

Views: 1711

Answers (1)

Leon
Leon

Reputation: 21

You could make a custom hook that returns these functions.

const customHook = () => {
  const collectOrganisationUnitNode = useCallback((treeData) => {
    // someLogic
  }, []);

  const handleCancelClick = useCallback(() => {
    //some logic
  }, [hideModal, selected, multiselect, tree]);
  return {
    collectOrganisationUnitNode,
    handleCancelClick
  }
};

Then used like so inside your component

  const { collectOrganisationUnitNode, handleCancelClick } = customHook();

Upvotes: 2

Related Questions