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

Unexpected behavior without useCallback hook #3

Open
DasAnurag31 opened this issue Mar 24, 2024 · 0 comments
Open

Unexpected behavior without useCallback hook #3

DasAnurag31 opened this issue Mar 24, 2024 · 0 comments

Comments

@DasAnurag31
Copy link

if (isMouseDown) {
const startBox = selectedBoxes[0];
const endBox = boxNumber;

  const startRow = Math.floor((startBox - 1) / cols);
  const startCol = (startBox - 1) % cols;

  const endRow = Math.floor((endBox - 1) / cols);
  const endCol = (endBox - 1) % cols;

  const minRow = Math.min(startRow, endRow);
  const maxRow = Math.max(startRow, endRow);
  const minCol = Math.min(startCol, endCol);
  const maxCol = Math.max(startCol, endCol);

  const selected = []; // BUG
  const selected = [startBox]; // FIX

  for (let row = minRow; row <= maxRow; row++) {
    for (let col = minCol; col <= maxCol; col++) {
      selected.push(row * cols + col + 1);
    }
  }
  console.log(selected);
  setSelectedBoxes(selected);
}

There is a bug in the code, as we are loosing the initial coordinate of the selected part, when we are re-calculating it. It is avoided when we use the callBack hook because it is not calculated when selected box changes, as we have only given the parameter as mouseDown. 

I thought of opening a PR, but i guess it's too minor of an issue, so i mentioned the fix in the issue itself. 

Great tutorial!
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