Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeScript type for onLayout event in React Native?

How can I specify a type in TypeScript for React Native's onLayout event?

  const handleOnLayout = (e: SomeTypeHere) => {
    // stuff
  };

  <View onLayout={handleOnLayout}>
    <Text>Hi</Text>
  </View>
like image 930
Evanss Avatar asked Mar 27 '20 15:03

Evanss


People also ask

Why can't I use typescript event handlers in react?

When we add event handlers in React, we can't use the types that TypeScript includes in the DOM library because React adds a wrapper around these events. Instead, we need to use the type definitions that React provides. In JSX, you can easily see the difference between native and synthetic events by looking at the attribute.

What is typescript in React Native?

Using TypeScript TypeScript is a language which extends JavaScript by adding type definitions, much like Flow. While React Native is built in Flow, it supports both TypeScript and Flow by default. Getting Started with TypeScript

What are the event types available in react?

TypeScript types for this event system are available in the @types/react npm package. These types can be used to strongly-type event parameters. Some of the common ones are: ChangeEvent<T>. KeyboardEvent<T>. MouseEvent<T>. FormEvent<T>. These types are all derived from SyntheticEvent<T>. All the event types are generic and take in the type for ...

What is the difference between native and synthetic events in react?

Native attributes are written in lower-case ( onclick ), while synthetic events use camelCase ( onClick ). In this article you'll find: What if there is no matching type definition? We can't use TypeScript's type definitions for events, but the good thing is that React has equivalent type definitions for all its synthetic events.


1 Answers

This method returns a LayoutChangeEvent with these properties {nativeEvent: { layout: {x, y, width, height}}}

Import with:

import { LayoutChangeEvent } from "react-native";

Source

like image 113
Murmeltier Avatar answered Oct 09 '22 16:10

Murmeltier