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.

Wrong type declaration in the onChange event

See original GitHub issue

Bug report

Summary

The Event received as the argument for the onChange callback has an incorrect type declared. The type should be event.nativeEvent.timestamp but the actual type declared is evt.nativeEvent.timeStamp. Note the uppercase S in timeStamp

This is not a problem since I can do what I need using the second argument on the onChange callback or even using event.nativeEvent.timestamp and disregarding the linter. Anyways, I thought maintainers would like to know this is happening. Hopefully it should be an easy fix for someone familiar with React Native types.

Reproducible sample code

This shows a linting error:

<DateTimePicker
  value={new Date()}
  mode='date'
  is24Hour={false}
  display='default'
  onChange={evt => {
    const timestamp = evt.nativeEvent.timestamp
    if (timestamp) {
      // handle the input change
    }
  }}
/>

Steps to reproduce

Using the code above the linter will complain and suggest to use evt.nativeEvent.timeStamp instead.

Describe what you expected to happen: I expected timeStamp to be the property with the value, but it was timestamp. The result was that timestamp in the code above was always undefined

Hovering over event.nativeEvent shows the following type:

React.BaseSyntheticEvent<Event, EventTarget & Readonly<{ timestamp: number; }>, EventTarget>.nativeEvent: Event

I was hoping the Readonly<{timestamp: number;}> part should fix this, but I’ve been using typescript for just a few days and I don’t really know what’s going on there

Environment info

npx react-native info output:

System:
    OS: macOS 10.15.5
    CPU: (4) x64 Intel(R) Core(TM) i5-6360U CPU @ 2.00GHz
    Memory: 32.04 MB / 8.00 GB
    Shell: 5.7.1 - /bin/zsh
  Binaries:
    Node: 12.16.1 - /usr/local/bin/node
    Yarn: 1.3.2 - /usr/local/bin/yarn
    npm: 6.13.4 - /usr/local/bin/npm
    Watchman: 4.7.0 - /usr/local/bin/watchman
  Managers:
    CocoaPods: 1.10.0 - /usr/local/bin/pod
  SDKs:
    iOS SDK: Not Found
    Android SDK:
      API Levels: 23, 24, 26, 29
      Build Tools: 23.0.1, 26.0.1, 26.0.2, 28.0.3, 29.0.2
      Android NDK: Not Found
  IDEs:
    Android Studio: Not Found
    Xcode: /undefined - /usr/bin/xcodebuild
  Languages:
    Java: 1.8.0_141 - /usr/bin/javac
    Python: 2.7.16 - /usr/bin/python
  npmPackages:
    @react-native-community/cli: Not Found
    react: 16.13.1 => 16.13.1
    react-native: ~0.63.3 => 0.63.3
    react-native-macos: Not Found
  npmGlobalPackages:
    *react-native*: Not Found

datetimepicker version: ^3.0.8

iOS / Android version: Android 11

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:12 (4 by maintainers)

github_iconTop GitHub Comments

10reactions
lexicalninjacommented, Jan 22, 2021
import RNDateTimePicker, { AndroidEvent, Event } from '@react-native-community/datetimepicker'
.....

 onChange={(event: Event | AndroidEvent, date?: Date): void => {
     ...
})

This is how I got it to work if it helps

4reactions
vonovakcommented, Mar 13, 2022

as of v6, this would look like this:

import RNDateTimePicker, { DateTimePickerEvent } from '@react-native-community/datetimepicker'
...

 onChange={(event: DateTimePickerEvent, date?: Date): void => {
     ...
})
Read more comments on GitHub >

github_iconTop Results From Across the Web

Typescript React <Select> onChange handler type error
value}); }; but I get the following error: Type '(event: ChangeEvent) => void' is not assignable to type '(event: ChangeEvent<{ name?: string | ......
Read more >
Solved: onChange error message - ServiceNow Community
Solved: I have an error message that sudent appears on my publication form, cannot identify why. The error in question is " onChange...
Read more >
Using Event Values With useState · ReasonReact - Reason ML
In the Reason version, the onChange callback won't correctly update the state. This happens because the callback passed to setName is run after...
Read more >
How to Fix ReferenceError: Event is Not Defined in JavaScript
For example, if on an onclick event, the handler does not declare the event parameter, this error is thrown. What Causes Javascript ......
Read more >
How to fix property not existing on EventTarget in TypeScript
This error occurs when you try to access a property on an event target in TypeScript. Property 'value' does not exist on type...
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