how to get current react component name inside custom react hook?

Viewed 936

I have a custom hook

function myCustomHook() {
   const currentComponentName = //? 
   return `currentComponentName${customSuffix}`
}

function Sample() {
  const name = myCustomHook()
}

function Component2() {
  const name = myCustomHook()
}

is it possible to get the unique name of a component? or any other alternative for this use case?

2 Answers
const getName = () => {
    const stack = new Error().stack;
    const lines = stack.split("\n");
    const line = lines[3];
    const match = line.match(/at (.*) \(/);
    const name = match[1];
    return name;
  };

I got it using copilot...

Related