-
Notifications
You must be signed in to change notification settings - Fork 391
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
Update README.md #588
Update README.md #588
Conversation
Adding optional chaining as isConnected may be null causing an error: >Uncaught TypeError: Cannot read properties of null (reading 'toString')
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can't accept this, isConnected is not nullable:
isConnected: true; |
If there's a case where it's null, some portion of the code has an error, and if you can reproduce that, it's really important to fix the root cause, which is that something is generating a NetInfoConnectedState without the required parameters
Thanks @mikehardy, For context, I've written a custom hook which I call on initialisation of my app. It makes use of the i.e initially this is returned:
There does also seem to be multiple re-renders coming back from the Here is the custom hook I wrote: function useNetInfoStatus() {
const previouslyOfflineRef = useRef(false)
const netInfoState = useNetInfo()
useEffect(() => {
const { type, details } = netInfoState
const online = netInfoState.isConnected && netInfoState.isInternetReachable
if (online === false && !previouslyOfflineRef.current) {
Snackbar.show({
text: 'Your device is currently offline',
duration: Snackbar.LENGTH_SHORT,
})
previouslyOfflineRef.current = true
}
if (online && previouslyOfflineRef.current) {
Snackbar.show({
text: 'Back online',
duration: Snackbar.LENGTH_SHORT,
})
previouslyOfflineRef.current = false
}
if (type === NetInfoStateType.wifi && details.strength && details.strength < 3) {
Snackbar.show({
text: `Your internet connection is weak \n${glossary.organization} may not function properly`,
duration: Snackbar.LENGTH_LONG,
})
}
}, [netInfoState])
}
export default useNetInfoStatus |
@mikehardy I think this is intended behaviour, digging into the NetInfo source code you can see that the hook is initialised with isConnected as react-native-netinfo/src/index.ts Line 105 in f05f7dd
Commit responsible: 4d84f14
|
@mikehardy, The type you linked is not the returned type for but rather: react-native-netinfo/src/internal/types.ts Line 105 in f05f7dd
This type is in fact correct, i.e no underlying issue, and I see now that I wasn't actually getting a warning in my console for the null case however, as null and undefined are subtypes of all other types and I had not enabled strictNullChecks ( This depends on your typescript version
Details here |
Ahh - definitely need to queue this for review again, thanks for the extra information and taking the time to make a PR in the first place, much appreciated |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One of my other repos exploded with activity and I've been swamped, sorry for the delay.
This looks great on re-review, and I really appreciate the digging / details and that you posted it. Cheers
🎉 This PR is included in version 8.3.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
Overview
Adding optional chaining as isConnected may be null causing an error: