Bug Report userState React Hooks
See original GitHub issueExpected Behavior
When using “const [devices, setDevices] = useState([])” or any statefunction inside the scaning from ‘startDeviceScan’ the event stops
Current Behavior
The event stop and no more devices are found
Context
[CoreBluetooth] API MISUSE: <CBCentralManager: 0x281ab8af0> has no restore identifier but the delegate implements the centralManager:willRestoreState: method. Restoring will not be supported 2020-01-19 20:06:06.293648+0100 Zipforce[23222:18432691] [CoreBluetooth] XPC connection invalid [RxBLEKit|DEBG|19:06:06.301]: CentralManager(10765437680) didUpdateState(state: poweredOn)
- Library version: 1.1.1
- Platform: OS.
- Platform logs (XCode):
const bleManager = new BleManager();
const [deviceScan, setDeviceScan] = useState(false)
const [devices, setDevices] = useState([])
const stopDeviceScan = () => {
console.trace('stopDeviceScan')
bleManager.stopDeviceScan()
setDeviceScan(false)
}
const addDevice = (device) => {
console.trace('addDevice')
console.log(device.id, device.name, device.localName)
if (device.isConnectable &&
device.localName &&
devices.findIndex((x) => x.id == device.id) === -1) {
setDevices([...devices, {
id: device.id,
name: device.name,
localName: device.localName,
isConnectable: device.isConnectable,
rssi: device.rssi
}])
}
}
const startDeviceScan = () => {
console.trace('startDeviceScan')
bleManager.startDeviceScan(null, { allowDuplicates: false }, (error, device) => {
if (error) {
console.error(error)
} else {
addDevice(device)
}
})
}
useEffect(() => {
console.trace('Init - Timer')
let timerId = setTimeout(() => {
stopDeviceScan()
}, 15000)
return () => clearTimeout(timerId);
}, [])
useEffect(() => {
console.trace('Init')
setDevices([])
setDeviceScan(true)
const subscription = bleManager.onStateChange((state) => {
switch (state) {
case 'PoweredOn':
console.trace('PoweredOn')
subscription.remove();
startDeviceScan()
break;
default:
break;
}
})
}, [])
useEffect(() =>{
console.log('useEffect',devices)
},[devices])
Issue Analytics
- State:
- Created 4 years ago
- Reactions:4
- Comments:26 (3 by maintainers)
Top Results From Across the Web
Bug Report userState React Hooks · Issue #603
i believe the problem arrises from the state hook causing a re-render. i fixed it by useRef for the manager and useReducer, rather...
Read more >I am a react beginner. I use hooks in the class and ...
I use hooks in the class and the program reports errors. src\page\App.js Line 41:35: React Hook "React.useState" cannot be called in a class...
Read more >React Hooks Common Mistakes
React Hooks is a new addition to React which enables you to use state and other features of the library without having to...
Read more >The noob's guide to useState
Using React Hooks, like useState, allows you to ditch class-based components, but are you cultivating bad practice? Find out here.
Read more >Avoiding hydration mismatch when using React hooks
Bugs caused by hydration mismatches can be of those type: the website works when you navigate from one page to another, but misbehaves...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@Cierpliwy Could you explain why the
BleManager
cannot be instantiated inside the component?Also, it would be extremely helpful to have an example
react-native-ble-plx
implementation using functional components. Do you know if this exists?I’ve noticed the same thing when using monitorCharacteristicForService(). If I try to update state inside of my listener function, it only runs once and then stops.