Skip to content
Advertisement

Recursive function in useImperativeHandle

My code is like below but I getting checkSome is not defined error, from inside the checkSome function, how can I call checkSome(el)

useImperativeHandle (ref, ()=> ({ 
    checkSome(prop) {
      userIds.push(prop.id);
      if (prop.children == null) return;
      prop.children.forEach((el) => {
        checkSome(el);
      });
  }}));

Advertisement

Answer

You need to use this keyword. Try this:

useImperativeHandle (ref, ()=> ({ 
    checkSome(prop) {
      userIds.push(prop.id);
      if (prop.children == null) return;
      prop.children.forEach((el) => {
        this.checkSome(el);
      });
  }}));

You can read more from here.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement