Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

TS 2339 Property 'property 1' does not exist on type 'IReturnState'.ts for custom hook

I have a custom hook like this:

interface IReturnState {
  activeItemsNumber: React.MutableRefObject<number>;
};

export const useMyState = (items): IReturnState => {
  // some code
  return {
    activeItemsNumber
  };
};

When I try to use it in the page component, I cannot return a custom variable name like this:

const { number1 } = useDndState(widgets);

There is an error on number1:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Property 'number1' does not exist on type 'IReturnState'.ts(2339)

I have to return the same variable name: activeItemsNumber, which means I cannot reuse the custom hook to return a different variable name. What I want to do is this:

const { number1 } = useDndState(widgets);
const { number2 } = useDndState(channels);

Any idea how to do the above?

>Solution :

you can rename the variables like this:

const { activeItemsNumber: number1 } = useDndState(widgets);
const { activeItemsNumber: number2 } = useDndState(channels);

or you can return a tuple instead of an object from the custom hook

export const useMyState = (items): IReturnState => {
  // some code
  return [activeItemsNumber];
};

and then you can do

const [ number1 ] = useDndState(widgets);
const [ number2 ] = useDndState(channels);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading