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

Issue with isDraggablePiece update #123

Open
srdi opened this issue Jan 18, 2024 · 2 comments
Open

Issue with isDraggablePiece update #123

srdi opened this issue Jan 18, 2024 · 2 comments

Comments

@srdi
Copy link

srdi commented Jan 18, 2024

I have a custom isDraggablePiece function, which relies on data I get from the server.
And it is wrapped with useCallback. I see that package uses multiple version of that callback.

I think need to add isDraggablePiece to dependencies of useDrag, here:

  const [{ canDrag, isDragging }, drag, dragPreview] = useDrag(
    () => ({
      type: "piece",
      item: () => {
        onPieceDragBegin(piece, square);
        return { piece, square, id };
      },
      end: () => onPieceDragEnd(piece, square),
      collect: (monitor) => ({
        canDrag: isDraggablePiece({ piece, sourceSquare: square }),
        isDragging: !!monitor.isDragging(),
      }),
    }),
    [piece, square, currentPosition, id]
  );

P.S:

A simple fix for this currently is to update id each time when isDraggablePiece callback updated.

@Clariity
Copy link
Owner

Clariity commented Jan 23, 2024

I'm struggling to understand what the issue may be here

If your isDraggablePiece callback updates through a re-render when something in your dep array changes, then in turn the Chessboard component should re-render with the new value of your isDraggablePiece callback which will then be correctly evaluated

Are you able to explain in more detail if possible please as I may be missing something?

@GuillaumeSD
Copy link

I have run into the same issue. I'll try to explain it in other words @Clariity.

Issue :
At the beginning, my isDraggablePiece function that I give to react-chessboard returns false for every piece.
Then, in the lifetime of my component, isDraggablePiece function gets updated and starts to return true for some pieces but that doesn't show up on the board, all pieces are still not draggable.

Solution :
I have looked into the implementation and I believe @srdi is right. The issue is the dependencies array of the useDrag hook, isDraggablePiece is missing in there. So when isDraggablePiece updates, the hook doesn't follow up its update. Adding isDraggablePiece in the dependencies array should fix the issue properly.

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

3 participants