From aa3434fc2bec1d8f3ab83f2870618cd61b86eeeb Mon Sep 17 00:00:00 2001 From: Piotr Witek Date: Fri, 17 May 2019 21:03:01 +0200 Subject: [PATCH] Updated React CheatSheet- Added new react type example HTMLProps- Added note about ComponentProps shortcomingsRelated #170 --- README.md | 40 ++++++++++++++++++++++++---------------- README_SOURCE.md | 40 ++++++++++++++++++++++++---------------- 2 files changed, 48 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index 0d9e914..bed2aaf 100644 --- a/README.md +++ b/README.md @@ -123,33 +123,33 @@ npm i -D @types/react @types/react-dom @types/react-redux # React - Type-Definitions Cheatsheet -#### `React.FunctionComponent

` or `React.FC

` +#### `React.FC` | `React.FunctionComponent` Type representing a functional component ```tsx const MyComponent: React.FC = ... ``` -#### `React.Component` +#### `React.Component` Type representing a class component ```tsx class MyComponent extends React.Component { ... ``` -#### `React.ComponentProps` -Gets type of Component Props, so you don't need to export Props from your component ever! (Works for both FC and Class components) -```tsx -type MyComponentProps = React.ComponentProps; -``` - -#### `React.ComponentType

` -Type representing union type of (React.FC | React.Component) +#### `React.ComponentType` +Type representing union of (React.FC | React.Component) - used in HOC ```tsx const withState =

( WrappedComponent: React.ComponentType

, ) => { ... ``` -#### `React.ReactElement` or `JSX.Element` +#### `React.ComponentProps` +Gets Props type of a specified component XXX (WARNING: does not work with statically declared default props and generic props) +```tsx +type MyComponentProps = React.ComponentProps; +``` + +#### `React.ReactElement` | `JSX.Element` Type representing a concept of React Element - representation of a native DOM component (e.g. `

`), or a user-defined composite component (e.g. ``) ```tsx const elementOnly: React.ReactElement =
|| ; @@ -163,22 +163,30 @@ const Component = ({ children: React.ReactNode }) => ... ``` #### `React.CSSProperties` -Type representing style object in JSX (useful for css-in-js styles) +Type representing style object in JSX - for css-in-js styles ```tsx const styles: React.CSSProperties = { flexDirection: 'row', ... const element =
` -Type representing generic event handler +#### `React.HTMLProps` +Type representing Props of specified HTML Element - for extending HTML Elements +```tsx +const Input: React.FC> = props => { ... } + + +``` + +#### `React.ReactEventHandler` +Type representing generic event handler - for declaring event handlers ```tsx const handleChange: React.ReactEventHandler = (ev) => { ... } ``` -#### `React.MouseEvent` | `React.KeyboardEvent` | `React.TouchEvent` etc... -Type representing more specific event handler +#### `React.XXXEvent` +Type representing more specific event handler. Some common event examples: `ChangeEvent, FormEvent, FocusEvent, KeyboardEvent, MouseEvent, DragEvent, PointerEvent, WheelEvent, TouchEvent`. ```tsx const handleChange = (ev: React.MouseEvent) => { ... } diff --git a/README_SOURCE.md b/README_SOURCE.md index fcfc652..6f25cfd 100644 --- a/README_SOURCE.md +++ b/README_SOURCE.md @@ -123,33 +123,33 @@ npm i -D @types/react @types/react-dom @types/react-redux # React - Type-Definitions Cheatsheet -#### `React.FunctionComponent

` or `React.FC

` +#### `React.FC` | `React.FunctionComponent` Type representing a functional component ```tsx const MyComponent: React.FC = ... ``` -#### `React.Component` +#### `React.Component` Type representing a class component ```tsx class MyComponent extends React.Component { ... ``` -#### `React.ComponentProps` -Gets type of Component Props, so you don't need to export Props from your component ever! (Works for both FC and Class components) -```tsx -type MyComponentProps = React.ComponentProps; -``` - -#### `React.ComponentType

` -Type representing union type of (React.FC | React.Component) +#### `React.ComponentType` +Type representing union of (React.FC | React.Component) - used in HOC ```tsx const withState =

( WrappedComponent: React.ComponentType

, ) => { ... ``` -#### `React.ReactElement` or `JSX.Element` +#### `React.ComponentProps` +Gets Props type of a specified component XXX (WARNING: does not work with statically declared default props and generic props) +```tsx +type MyComponentProps = React.ComponentProps; +``` + +#### `React.ReactElement` | `JSX.Element` Type representing a concept of React Element - representation of a native DOM component (e.g. `

`), or a user-defined composite component (e.g. ``) ```tsx const elementOnly: React.ReactElement =
|| ; @@ -163,22 +163,30 @@ const Component = ({ children: React.ReactNode }) => ... ``` #### `React.CSSProperties` -Type representing style object in JSX (useful for css-in-js styles) +Type representing style object in JSX - for css-in-js styles ```tsx const styles: React.CSSProperties = { flexDirection: 'row', ... const element =
` -Type representing generic event handler +#### `React.HTMLProps` +Type representing Props of specified HTML Element - for extending HTML Elements +```tsx +const Input: React.FC> = props => { ... } + + +``` + +#### `React.ReactEventHandler` +Type representing generic event handler - for declaring event handlers ```tsx const handleChange: React.ReactEventHandler = (ev) => { ... } ``` -#### `React.MouseEvent` | `React.KeyboardEvent` | `React.TouchEvent` etc... -Type representing more specific event handler +#### `React.XXXEvent` +Type representing more specific event handler. Some common event examples: `ChangeEvent, FormEvent, FocusEvent, KeyboardEvent, MouseEvent, DragEvent, PointerEvent, WheelEvent, TouchEvent`. ```tsx const handleChange = (ev: React.MouseEvent) => { ... }