Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Passing reference or child component with ref, invariant error #131

Open
paulm17 opened this issue Oct 2, 2024 · 0 comments
Open

Passing reference or child component with ref, invariant error #131

paulm17 opened this issue Oct 2, 2024 · 0 comments

Comments

@paulm17
Copy link

paulm17 commented Oct 2, 2024

I'm trying to have the draghandle in a child component.

parent component:

const elementRef = useRef<HTMLDivElement>(null);
const dragHandleRef = useRef<HTMLButtonElement>(null);

useEffect(() => {
      const element = elementRef.current;
      const dragHandle = dragHandleRef.current;

      invariant(element);
      invariant(dragHandle);

      return combine(
        draggable({
          element: dragHandle,
          getInitialData: () => item,
          onDragStart() {
          },
          onDrop() {
          },
        })
      )
    }, [item, index]);

Passing in as children (to the child component):

<Toolbar id={item.id} parentId={parentId} index={index} count={count}>
   <UnstyledButton ref={dragHandleRef} className="text-white text-[16px] cursor-move py-[2px] px-[7px] bg-[rgba(0,93,159)] hover:bg-[rgb(0,46,79)]">
      <Icon icon="arrows" type="fas" />
   </UnstyledButton>
</Toolbar>

Or Pass by forward ref (to the child component):

<Toolbar ref={dragHandleRef} id={item.id} parentId={parentId} index={index} count={count} />

Toolbar component:

export const Toolbar = forwardRef<HTMLButtonElement, toolbarProps>(
  ({ id, parentId, index, count }, ref) => {
  
   <UnstyledButton ref={ref} className="text-white text-[16px] cursor-move py-[2px] px-[7px] bg-[rgba(0,93,159)] hover:bg-[rgb(0,46,79)]">
      <Icon icon="arrows" type="fas" />
   </UnstyledButton> 

My actual use case is pass by forward ref...

But in either case I get:

Error: Invariant failed

I hope this is enough to convey the issue. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant