From 36189a1674ba29c396c2536fdee1d5290cc3c152 Mon Sep 17 00:00:00 2001 From: Piotr Witek Date: Thu, 24 Jan 2019 14:42:50 +0100 Subject: [PATCH 01/20] Added `React.ComponentProps --- README.md | 18 +++++++++--------- README_SOURCE.md | 18 +++++++++--------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 6cd28f3..5f02174 100644 --- a/README.md +++ b/README.md @@ -109,16 +109,20 @@ npm i -D @types/react @types/react-dom @types/react-redux #### `React.FunctionComponent

` or `React.FC

` Type representing a functional component ```tsx -const MyComponent: React.FC = ... +const MyComponent: React.FC = ... ``` -[⇧ back to top](#table-of-contents) #### `React.Component` Type representing a class component ```tsx -class MyComponent extends React.Component { ... +class MyComponent extends React.Component { ... +``` + +#### `React.ComponentProps +Gets component Props type. You don't no need to export Props from your component ever! +```tsx +type MyComponentProps = React.ComponentProps; ``` -[⇧ back to top](#table-of-contents) #### `React.ComponentType

` Type representing union type of (FC | Component) @@ -127,14 +131,12 @@ const withState =

( WrappedComponent: React.ComponentType

, ) => { ... ``` -[⇧ back to top](#table-of-contents) #### `React.ReactElement

` or `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 =
|| ; ``` -[⇧ back to top](#table-of-contents) #### `React.ReactNode` Type representing any possible type of React node (basically ReactElement (including Fragments and Portals) + primitive JS types) @@ -142,7 +144,6 @@ Type representing any possible type of React node (basically ReactElement (inclu const elementOrPrimitive: React.ReactNode = 'string' || 0 || false || null || undefined ||
|| ; const Component = ({ children: React.ReactNode }) => ... ``` -[⇧ back to top](#table-of-contents) #### `React.CSSProperties` Type representing style object in JSX (usefull for css-in-js styles) @@ -150,7 +151,6 @@ Type representing style object in JSX (usefull for css-in-js styles) const styles: React.CSSProperties = { flexDirection: 'row', ... const element =
` Type representing generic event handler @@ -159,7 +159,6 @@ const handleChange: React.ReactEventHandler = (ev) => { ... } ``` -[⇧ back to top](#table-of-contents) #### `React.MouseEvent` | `React.KeyboardEvent` | `React.TouchEvent` etc... Type representing more specific event handler @@ -168,6 +167,7 @@ const handleChange = (ev: React.MouseEvent) => { ... }
``` + [⇧ back to top](#table-of-contents) --- diff --git a/README_SOURCE.md b/README_SOURCE.md index 1db0fdc..6913c41 100644 --- a/README_SOURCE.md +++ b/README_SOURCE.md @@ -109,16 +109,20 @@ npm i -D @types/react @types/react-dom @types/react-redux #### `React.FunctionComponent

` or `React.FC

` Type representing a functional component ```tsx -const MyComponent: React.FC = ... +const MyComponent: React.FC = ... ``` -[⇧ back to top](#table-of-contents) #### `React.Component` Type representing a class component ```tsx -class MyComponent extends React.Component { ... +class MyComponent extends React.Component { ... +``` + +#### `React.ComponentProps +Gets component Props type. You don't no need to export Props from your component ever! +```tsx +type MyComponentProps = React.ComponentProps; ``` -[⇧ back to top](#table-of-contents) #### `React.ComponentType

` Type representing union type of (FC | Component) @@ -127,14 +131,12 @@ const withState =

( WrappedComponent: React.ComponentType

, ) => { ... ``` -[⇧ back to top](#table-of-contents) #### `React.ReactElement

` or `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 =
|| ; ``` -[⇧ back to top](#table-of-contents) #### `React.ReactNode` Type representing any possible type of React node (basically ReactElement (including Fragments and Portals) + primitive JS types) @@ -142,7 +144,6 @@ Type representing any possible type of React node (basically ReactElement (inclu const elementOrPrimitive: React.ReactNode = 'string' || 0 || false || null || undefined ||
|| ; const Component = ({ children: React.ReactNode }) => ... ``` -[⇧ back to top](#table-of-contents) #### `React.CSSProperties` Type representing style object in JSX (usefull for css-in-js styles) @@ -150,7 +151,6 @@ Type representing style object in JSX (usefull for css-in-js styles) const styles: React.CSSProperties = { flexDirection: 'row', ... const element =
` Type representing generic event handler @@ -159,7 +159,6 @@ const handleChange: React.ReactEventHandler = (ev) => { ... } ``` -[⇧ back to top](#table-of-contents) #### `React.MouseEvent` | `React.KeyboardEvent` | `React.TouchEvent` etc... Type representing more specific event handler @@ -168,6 +167,7 @@ const handleChange = (ev: React.MouseEvent) => { ... }
``` + [⇧ back to top](#table-of-contents) --- From 533ea05697e008069ecefc54e692f91b8e130aee Mon Sep 17 00:00:00 2001 From: Piotr Witek Date: Thu, 24 Jan 2019 14:44:45 +0100 Subject: [PATCH 02/20] Added missing quote --- README.md | 2 +- README_SOURCE.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 5f02174..173ab24 100644 --- a/README.md +++ b/README.md @@ -118,7 +118,7 @@ Type representing a class component class MyComponent extends React.Component { ... ``` -#### `React.ComponentProps +#### `React.ComponentProps` Gets component Props type. You don't no need to export Props from your component ever! ```tsx type MyComponentProps = React.ComponentProps; diff --git a/README_SOURCE.md b/README_SOURCE.md index 6913c41..fa2a059 100644 --- a/README_SOURCE.md +++ b/README_SOURCE.md @@ -118,7 +118,7 @@ Type representing a class component class MyComponent extends React.Component { ... ``` -#### `React.ComponentProps +#### `React.ComponentProps` Gets component Props type. You don't no need to export Props from your component ever! ```tsx type MyComponentProps = React.ComponentProps; From 92c9a5ce03ad852e8421ddd55a4bca42375d5e45 Mon Sep 17 00:00:00 2001 From: Sosuke Suzuki Date: Sun, 27 Jan 2019 04:34:06 +0900 Subject: [PATCH 03/20] Add the example for useContext (#127) resolve https://github.com/piotrwitek/react-redux-typescript-guide/issues/120#issuecomment-457159038 - divide `playground/src/context/theme-provider.tsx` to `theme-provider.tsx`, `theme-consumer.tsx` and `theme-context.ts`. - add the example for `useContext` to `playground/src/hooks/use-theme-context.tsx`. --- README.md | 23 +++++++++++++++ README_SOURCE.md | 8 ++++++ playground/src/context/theme-consumer.tsx | 12 ++++++++ playground/src/context/theme-context.ts | 24 ++++++++++++++++ playground/src/context/theme-provider.tsx | 33 +++------------------- playground/src/hooks/use-theme-context.tsx | 13 +++++++++ 6 files changed, 84 insertions(+), 29 deletions(-) create mode 100644 playground/src/context/theme-consumer.tsx create mode 100644 playground/src/context/theme-context.ts create mode 100644 playground/src/hooks/use-theme-context.tsx diff --git a/README.md b/README.md index 173ab24..e5986b8 100644 --- a/README.md +++ b/README.md @@ -826,6 +826,29 @@ export default Counter; [⇧ back to top](#table-of-contents) +#### - useContext + +> https://reactjs.org/docs/hooks-reference.html#usecontext + +```tsx +import * as React from 'react'; +import ThemeContext from '../context/theme-context'; + +type Props = {}; + +export default function ThemeToggleButton(props: Props) { + const { theme, toggleTheme } = React.useContext(ThemeContext); + return ( + + ); +} + +``` + +[⇧ back to top](#table-of-contents) + --- # Redux diff --git a/README_SOURCE.md b/README_SOURCE.md index fa2a059..47476fe 100644 --- a/README_SOURCE.md +++ b/README_SOURCE.md @@ -320,6 +320,14 @@ Hook for state management like Redux in a function component. [⇧ back to top](#table-of-contents) +#### - useContext + +> https://reactjs.org/docs/hooks-reference.html#usecontext + +::codeblock='playground/src/hooks/use-theme-context.tsx':: + +[⇧ back to top](#table-of-contents) + --- # Redux diff --git a/playground/src/context/theme-consumer.tsx b/playground/src/context/theme-consumer.tsx new file mode 100644 index 0000000..5ddd73d --- /dev/null +++ b/playground/src/context/theme-consumer.tsx @@ -0,0 +1,12 @@ +import * as React from 'react'; +import ThemeContext from './theme-context'; + +type Props = {}; + +export default function ToggleThemeButton(props: Props) { + return ( + + {({ theme, toggleTheme }) => + ); +} From 38ccf5567735bff920e0c3775adeb2856456b3b6 Mon Sep 17 00:00:00 2001 From: Sosuke Suzuki Date: Sun, 27 Jan 2019 04:37:26 +0900 Subject: [PATCH 04/20] Add useState example (#128) resolve #120 --- README.md | 25 +++++++++++++++++++++++++ README_SOURCE.md | 8 ++++++++ playground/src/hooks/use-state.tsx | 15 +++++++++++++++ 3 files changed, 48 insertions(+) create mode 100644 playground/src/hooks/use-state.tsx diff --git a/README.md b/README.md index e5986b8..f5b8691 100644 --- a/README.md +++ b/README.md @@ -770,6 +770,31 @@ export default () => ( > https://reactjs.org/docs/hooks-intro.html +#### - useState + +> https://reactjs.org/docs/hooks-reference.html#usestate + +```tsx +import * as React from 'react'; + +type Props = { initialCount: number }; + +export default function Counter({initialCount}: Props) { + const [count, setCount] = React.useState(initialCount); + return ( + <> + Count: {count} + + + + + ); +} + +``` + +[⇧ back to top](#table-of-contents) + #### - useReducer Hook for state management like Redux in a function component. diff --git a/README_SOURCE.md b/README_SOURCE.md index 47476fe..b4d575d 100644 --- a/README_SOURCE.md +++ b/README_SOURCE.md @@ -313,6 +313,14 @@ const mapDispatchToProps = (dispatch: Dispatch) => ({ > https://reactjs.org/docs/hooks-intro.html +#### - useState + +> https://reactjs.org/docs/hooks-reference.html#usestate + +::codeblock='playground/src/hooks/use-state.tsx':: + +[⇧ back to top](#table-of-contents) + #### - useReducer Hook for state management like Redux in a function component. diff --git a/playground/src/hooks/use-state.tsx b/playground/src/hooks/use-state.tsx new file mode 100644 index 0000000..3a7413a --- /dev/null +++ b/playground/src/hooks/use-state.tsx @@ -0,0 +1,15 @@ +import * as React from 'react'; + +type Props = { initialCount: number }; + +export default function Counter({initialCount}: Props) { + const [count, setCount] = React.useState(initialCount); + return ( + <> + Count: {count} + + + + + ); +} From 446654f5ce8fb7a763ca6b1d14617d665ce6658d Mon Sep 17 00:00:00 2001 From: Sosuke Suzuki Date: Sun, 27 Jan 2019 06:11:00 +0900 Subject: [PATCH 05/20] Add examples for Context API (#129) Add examples for Context API --- README.md | 93 ++++++++++++++++++++++++++++++++++++++++++++++++ README_SOURCE.md | 25 +++++++++++++ 2 files changed, 118 insertions(+) diff --git a/README.md b/README.md index f5b8691..71f1b46 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,7 @@ This gives you the power to prioritize our work and support the project contribu - [Render Props](#render-props) 🌟 __NEW__ - [Higher-Order Components](#higher-order-components) 📝 __UPDATED__ - [Redux Connected Components](#redux-connected-components) + - [Context](#context) - [Hooks](#hooks) - [Redux](#redux) - [Action Creators](#action-creators) 📝 __UPDATED__ @@ -766,6 +767,98 @@ export default () => ( [⇧ back to top](#table-of-contents) +## Context + +> https://reactjs.org/docs/context.html + +#### ThemeContext + +```tsx +import * as React from 'react'; + +export type Theme = React.CSSProperties; + +type Themes = { + dark: Theme; + light: Theme; +}; + +export const themes: Themes = { + dark: { + color: 'black', + backgroundColor: 'white', + }, + light: { + color: 'white', + backgroundColor: 'black', + }, +}; + +export type ThemeContextProps = { theme: Theme; toggleTheme?: () => void }; +const ThemeContext = React.createContext({ theme: themes.light }); + +export default ThemeContext; + +``` + +[⇧ back to top](#table-of-contents) + +#### ThemeProvider + +```tsx +import React from 'react'; +import ThemeContext, { themes, Theme } from './theme-context'; +import ToggleThemeButton from './theme-consumer'; + +interface State { + theme: Theme; +} +export class ThemeProvider extends React.Component<{}, State> { + readonly state: State = { theme: themes.light }; + + toggleTheme = () => { + this.setState(state => ({ + theme: state.theme === themes.light ? themes.dark : themes.light, + })); + } + + render() { + const { theme } = this.state; + const { toggleTheme } = this; + return ( + + + + ); + } +} + +``` + +[⇧ back to top](#table-of-contents) + +#### ThemeConsumer + +```tsx +import * as React from 'react'; +import ThemeContext from './theme-context'; + +type Props = {}; + +export default function ToggleThemeButton(props: Props) { + return ( + + {({ theme, toggleTheme }) =>