question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[addEventListener][iOS] addEventListener only triggers for the first time(s).

See original GitHub issue

Environment

System:
    OS: macOS 12.3.1
    CPU: (8) x64 Intel(R) Core(TM) i7-4770HQ CPU @ 2.20GHz
    Memory: 294.72 MB / 16.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 14.17.0 - /usr/local/bin/node
    Yarn: 1.22.18 - /usr/local/bin/yarn
    npm: 8.6.0 - /usr/local/bin/npm
    Watchman: 2022.03.21.00 - /usr/local/bin/watchman
  Managers:
    CocoaPods: 1.11.3 - /Users/mo/.rvm/rubies/ruby-2.7.4/bin/pod
  SDKs:
    iOS SDK:
      Platforms: DriverKit 21.2, iOS 15.2, macOS 12.1, tvOS 15.2, watchOS 8.3
    Android SDK: Not Found
  IDEs:
    Android Studio: 2021.1 AI-211.7628.21.2111.8193401
    Xcode: 13.2.1/13C100 - /usr/bin/xcodebuild
  Languages:
    Java: 11.0.13 - /usr/bin/javac
  npmPackages:
    @react-native-community/cli: Not Found
    react: 18.1.0 => 18.1.0 
    react-native: 0.68.2 => 0.68.2 
    react-native-macos: Not Found
  npmGlobalPackages:
    *react-native*: Not Found

Platforms

iOS

Versions

  • iOS: 15.4.1
  • react-native-netinfo: 8.3.0
  • react-native: 0.68.2
  • react: 18.1.0

Description

addEventListener triggers only for the first time/ first couple times that connection changes. After that it doesn’t trigger the event.

Reproducible Demo

Use following code:

  useEffect(() => {
     NetInfo.addEventListener((networkState) => {console.log(networkState)}
  }, [])

Try to switch the wifi on and off for more than 4-5 times. See if it triggers all the events.

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
DrOverbuildcommented, May 31, 2022

@mhatami-ecotrak Are you able to reproduce this bug on a physical device? I tried your demo on a simulator, and it was buggy, but I could not reproduce it on a physical device.

(You will have to run the app from Xcode and watch the logs there because you’ll get disconnected from the Metro server doing this test)

0reactions
lvlrSajjadcommented, May 11, 2022

@mikehardy V6 didn’t work either.

Read more comments on GitHub >

github_iconTop Results From Across the Web

javascript - AddEventListener only triggers once - Stack Overflow
The problem is in your condition. "green" always evaluates to true as it is a non-empty string. What you want to do is...
Read more >
EventTarget.addEventListener() - Web APIs | MDN
The addEventListener() method of the EventTarget interface sets up a function that will be called whenever the specified event is delivered ...
Read more >
How to ensure an event listener is only fired once in JavaScript
1. Using the once option. We can pass an object as an argument to the addEventListener method and specify that the event is...
Read more >
JavaScript/TypeScript: addEventListener triggers immediately
By adding the { capture: true } option to the new event listener, it will only trigger in the capturing phase, and by...
Read more >
One-off event listeners in JavaScript | by Ashwani Gupta
Before explaining what one time event listeners are, we have to first ... The second way is to call the addEventListener() method on...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found