aidenybai / aidenybai/react-scan

Bug: useScan() ignores `enabled: false` and always starts scanning

Đang mở
#460 1 bình luận 0 reaction 0 người được giao Xem trên GitHub
Ngôn ngữ chính
TypeScript
Star
21.8k
Fork
390
Merge trung bình
23 phút
Pull request đã merge (30 ngày)
1

Mô tả

## Description

`useScan()` unconditionally calls `start()` regardless of the `enabled` option, unlike `scan()` which properly guards against it.

```ts
// scan() — respects enabled ✅
export const scan = (options: Options = {}) => {
setOptions(options);
if (options.enabled === false && options.showToolbar !== true) {
return;
}
start();
};

// useScan() — ignores enabled ❌
export const useScan = (options: Options = {}) => {
setOptions(options);
start(); // always runs regardless of enabled
};
```

## Reproduction

```tsx
import { useScan } from "react-scan";

function App() {
useScan({ enabled: false }); // scanning still activates
return

Hello
;
}
```

## Expected Behavior

When `enabled: false` is passed to `useScan()`, scanning and the toolbar should not activate — matching the behavior of `scan()`.

## Related

- #269 — "scan is always enabled as long as showToolbar is true" (closed but a commenter confirmed still broken in 0.3.3)
- #101 — Added the `enabled` guard to `scan()` but never applied it to `useScan()`

## Proposed Fix

Apply the same iframe and `enabled` guards from `scan()` to `useScan()`:

```ts
export const useScan = (options: Options = {}) => {
setOptions(options);
const isInIframe = Store.isInIframe.value;

if (
isInIframe &&
!ReactScanInternals.options.value.allowInIframe &&
!ReactScanInternals.runInAllEnvironments
) {
return;
}

if (options.enabled === false && options.showToolbar !== true) {
return;
}

start();
};
```

Branch with the fix: https://github.com/jalbarrang/react-scan/tree/fix/usescan-respects-enabled

> _Note: Unable to open a PR since the repo restricts PRs to collaborators._

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.