MCPcopy
hub / github.com/piotrwitek/react-redux-typescript-guide

github.com/piotrwitek/react-redux-typescript-guide @main sqlite

repository ↗ · DeepWiki ↗
147 symbols 308 edges 117 files 3 documented · 2%
README

React & Redux in TypeScript - Complete Guide

"This guide is a living compendium documenting the most important patterns and recipes on how to use React (and its Ecosystem) in a functional style using TypeScript. It will help you make your code completely type-safe while focusing on inferring the types from implementation so there is less noise coming from excessive type annotations and it's easier to write and maintain correct types in the long run."

Join the community on Spectrum Join the chat at https://gitter.im/react-redux-typescript-guide/Lobby

Found it useful? Want more updates?

Show your support by giving a :star:

Buy Me a Coffee Become a Patron


What's new?

:tada: Now updated to support TypeScript v4.6 :tada: :rocket: _Updated to typesafe-actions@5.x :rocket:


Goals

  • Complete type safety (with --strict flag) without losing type information downstream through all the layers of our application (e.g. no type assertions or hacking with any type)
  • Make type annotations concise by eliminating redundancy in types using advanced TypeScript Language features like Type Inference and Control flow analysis
  • Reduce repetition and complexity of types with TypeScript focused complementary libraries

React, Redux, Typescript Ecosystem

  • typesafe-actions - Typesafe utilities for "action-creators" in Redux / Flux Architecture
  • utility-types - Collection of generic types for TypeScript, complementing built-in mapped types and aliases - think lodash for reusable types.
  • react-redux-typescript-scripts - dev-tools configuration files shared between projects based on this guide

Examples

Playground Project

Build Status

Check out our Playground Project located in the /playground folder. It contains all source files of the code examples found in the guide. They are all tested with the most recent version of TypeScript and 3rd party type-definitions (like @types/react or @types/react-redux) to ensure the examples are up-to-date and not broken with updated definitions (It's based on create-react-app --typescript).

Playground project was created so that you can simply clone the repository locally and immediately play around with all the component patterns found in the guide. It will help you to learn all the examples from this guide in a real project environment without the need to create complicated environment setup by yourself.

Contributing Guide

You can help make this project better by contributing. If you're planning to contribute please make sure to check our contributing guide: CONTRIBUTING.md

Funding

You can also help by funding issues. Issues like bug fixes or feature requests can be very quickly resolved when funded through the IssueHunt platform.

I highly recommend to add a bounty to the issue that you're waiting for to increase priority and attract contributors willing to work on it.

Let's fund issues in this repository


🌟 - New or updated section

Table of Contents


Installation

Types for React & Redux

npm i -D @types/react @types/react-dom @types/react-redux

"react" - @types/react
"react-dom" - @types/react-dom
"redux" - (types included with npm package)*
"react-redux" - @types/react-redux

*NB: Guide is based on types for Redux >= v4.x.x.

⇧ back to top


React Types Cheatsheet

React.FC<Props> | React.FunctionComponent<Props>

Type representing a functional component

const MyComponent: React.FC<Props> = ...

React.Component<Props, State>

Type representing a class component

class MyComponent extends React.Component<Props, State> { ...

React.ComponentType<Props>

Type representing union of (React.FC<Props> | React.Component<Props>) - used in HOC

const withState = 

(
  WrappedComponent: React.ComponentType

,
) => { ...

React.ComponentProps<typeof XXX>

Gets Props type of a specified component XXX (WARNING: does not work with statically declared default props and generic props)

type MyComponentProps = React.ComponentProps<typeof MyComponent>;

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.`)

const elementOnly: React.ReactElement = 

 || <MyComponent />;

React.ReactNode

Type representing any possible type of React node (basically ReactElement (including Fragments and Portals) + primitive JS types)

const elementOrPrimitive: React.ReactNode = 'string' || 0 || false || null || undefined || 

 || <MyComponent />;
const Component = ({ children: React.ReactNode }) => ...

React.CSSProperties

Type representing style object in JSX - for css-in-js styles

const styles: React.CSSProperties = { flexDirection: 'row', ...
const element = 

`

Type representing HTML attributes of specified HTML Element - for extending HTML Elements

```tsx
const Input: React.FC<Props & React.InputHTMLAttributes<HTMLInputElement>> = props => { ... }

<Input about={...} accept={...} alt={...} ... />

React.ReactEventHandler<HTMLXXXElement>

Type representing generic event handler - for declaring event handlers

const handleChange: React.ReactEventHandler<HTMLInputElement> = (ev) => { ... } 

<input onChange={handleChange} ... />

React.XXXEvent<HTMLXXXElement>

Type representing more specific event. Some common event examples: ChangeEvent, FormEvent, FocusEvent, KeyboardEvent, MouseEvent, DragEvent, PointerEvent, WheelEvent, TouchEvent.

const handleChange = (ev: React.MouseEvent<HTMLDivElement>) => { ... }




In code above React.MouseEvent<HTMLDivElement> is type of mouse event, and this event happened on HTMLDivElement

⇧ back to top


React

Function Components - FC

- Counter Component

::codeblock='playground/src/components/fc-counter.tsx'::

⟩⟩⟩ demo

⇧ back to top

- Counter Component with default props

::codeblock='playground/src/components/fc-counter-with-default-props.tsx'::

⟩⟩⟩ demo

⇧ back to top

- Spreading attributes in Component

::codeblock='playground/src/components/fc-spread-attributes.tsx'::

[⟩⟩⟩ demo](https://piotrwitek.github.io/react-redux-typescr

Extension points exported contracts — how you extend this code

IUser (Interface)
(no doc) [2 implementers]
playground/src/models/user.ts
Window (Interface)
(no doc)
playground/typings/globals.d.ts
GenericListProps (Interface)
(no doc)
playground/src/components/generic-list.tsx
NodeModule (Interface)
(no doc)
playground/typings/globals.d.ts
NameProviderProps (Interface)
(no doc)
playground/src/components/name-provider.tsx
System (Interface)
(no doc)
playground/typings/globals.d.ts
NameProviderState (Interface)
(no doc)
playground/src/components/name-provider.tsx
Action (Interface)
(no doc)
playground/typings/redux/index.d.ts

Core symbols most depended-on inside this repo

resolveWithDelay
called by 5
playground/src/api/utils.ts
render
called by 3
playground/src/hoc/with-state.tsx
reducer
called by 3
playground/src/hooks/use-reducer.tsx
createMatchReplacer
called by 2
generate-readme.js
registerValidSW
called by 2
playground/src/serviceWorker.ts
formatToken
called by 2
playground/src/api/agent.ts
createName
called by 2
playground/src/models/nominal-types.ts
createSurname
called by 2
playground/src/models/nominal-types.ts

Shape

Function 67
Class 28
Interface 28
Method 22
Enum 2

Languages

TypeScript100%

Modules by API surface

playground/typings/redux/index.d.ts13 symbols
playground/src/models/user.ts9 symbols
playground/src/hoc/with-connected-count.tsx6 symbols
playground/src/hoc/with-state.tsx5 symbols
playground/src/hoc/with-error-boundary.tsx5 symbols
playground/src/connected/fc-counter-connected-bind-action-creators.tsx5 symbols
playground/src/components/name-provider.tsx5 symbols
playground/src/components/mouse-provider.tsx5 symbols
generate-readme.js5 symbols
playground/typings/globals.d.ts4 symbols
playground/src/serviceWorker.ts4 symbols
playground/src/hooks/use-reducer.tsx4 symbols

Dependencies from manifests, versioned

@lagunovsky/redux-react-router2.2.0 · 1×
@storybook/addon-actions5.2.5 · 1×
@storybook/addon-links5.2.5 · 1×
@storybook/addon-storyshots5.2.5 · 1×
@storybook/addons5.2.5 · 1×
@storybook/react5.2.5 · 1×
@testing-library/jest-dom5.16.4 · 1×
@testing-library/react13.1.1 · 1×
@testing-library/user-event13.5.0 · 1×
@types/jest27.5.0 · 1×
@types/node16.11.33 · 1×
@types/react18.0.8 · 1×

For agents

$ claude mcp add react-redux-typescript-guide \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact