├── .gitignore ├── .travis.yml ├── CONTRIBUTING.md ├── LICENSE ├── README.md ├── package.json ├── public ├── favicon.ico ├── index.html ├── manifest.json └── react-typescript.svg ├── src ├── examples │ ├── context │ │ ├── __tests__ │ │ │ ├── __snapshots__ │ │ │ │ └── index.test.tsx.snap │ │ │ └── index.test.tsx │ │ ├── context.ts │ │ └── index.tsx │ ├── higher-order-component │ │ └── index.tsx │ ├── hook-context │ │ ├── __tests__ │ │ │ ├── __snapshots__ │ │ │ │ └── index.test.tsx.snap │ │ │ └── index.test.tsx │ │ ├── context.ts │ │ └── index.tsx │ ├── hook-effect │ │ ├── __tests__ │ │ │ ├── __snapshots__ │ │ │ │ └── index.test.tsx.snap │ │ │ └── index.test.tsx │ │ └── index.tsx │ ├── hook-reducer │ │ ├── __tests__ │ │ │ ├── __snapshots__ │ │ │ │ └── index.test.tsx.snap │ │ │ └── index.test.tsx │ │ └── index.tsx │ ├── reducer-component │ │ ├── __tests__ │ │ │ └── reducerComponent.test.tsx │ │ ├── index.tsx │ │ └── reducerComponent.tsx │ ├── reducer.ts │ ├── render-props │ │ ├── __tests__ │ │ │ ├── __snapshots__ │ │ │ │ └── index.test.tsx.snap │ │ │ └── index.test.tsx │ │ └── index.tsx │ └── utils.ts ├── index.js └── index.tsx ├── tsconfig.json ├── tsconfig.prod.json ├── tsconfig.test.json ├── tslint.json └── yarn.lock /.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/ignore-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | 6 | # testing 7 | /coverage 8 | 9 | # production 10 | /build 11 | 12 | # misc 13 | .DS_Store 14 | .env.local 15 | .env.development.local 16 | .env.test.local 17 | .env.production.local 18 | 19 | npm-debug.log* 20 | yarn-debug.log* 21 | yarn-error.log* 22 | 23 | *.swp 24 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | 3 | node_js: 4 | - 12.0 5 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | Contributing 2 | =============================================================================== 3 | 4 | Have something to add? Feature requests, bug reports, and contributions are 5 | enormously welcome! 6 | 7 | 1. Fork this repo 8 | 2. Update the tests and implement the change 9 | 3. Submit a [pull request][github-pull-request] 10 | 11 | (hint: following the conventions in the [the code review 12 | checklist][code-review-checklist] will expedite review and merge) 13 | 14 | [github-pull-request]: help.github.com/pull-requests/ 15 | [code-review-checklist]: https://github.com/rjz/code-review-checklist 16 | 17 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | // MIT License 2 | 3 | Copyright (C) RJ Zaworski 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

2 | 3 |

4 | 5 | # React with TypeScript 6 | 7 | [TypeScript](https://www.typescriptlang.org/) sample implementations of familiar 8 | [React.js](http://reactjs.org) patterns. 9 | 10 | [![Build Status](https://travis-ci.org/rjz/react-with-typescript.svg?branch=master)](https://travis-ci.org/rjz/react-with-typescript) 11 | 12 | ## Examples 13 | 14 | - [`useEffect()` and `useState()` hooks](./src/examples/hook-effect/) 15 | - [`useContext()` hook](./src/examples/hook-context/) 16 | - [`useReducer()` hook](./src/examples/hook-reducer/) 17 | - [Context API](./src/examples/context/) 18 | - [Higher-order component](./src/examples/higher-order-component/) 19 | - [Reducer component](./src/examples/reducer-component/) 20 | - [Render props](./src/examples/render-props/) 21 | 22 | Also, 23 | 24 | - Redux [has its own repository][ts-react-redux] 25 | 26 | ## Run 27 | 28 | Install dependencies and serve examples the usual way: 29 | 30 | ```sh 31 | $ npm install 32 | $ npm start 33 | ``` 34 | 35 | ## Contributing 36 | 37 | Missing something? [Open an 38 | issue](https://github.com/rjz/react-with-typescript/issues/new) or submit a pull 39 | request! 40 | 41 | ## License 42 | 43 | ISC 44 | 45 | [ts-react-redux]: https://github.com/rjz/typescript-react-redux 46 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-with-typescript", 3 | "license": "ISC", 4 | "version": "0.1.0", 5 | "private": true, 6 | "keywords": [ 7 | "react", 8 | "typescript", 9 | "context", 10 | "hooks", 11 | "examples" 12 | ], 13 | "dependencies": { 14 | "eslint": "5.6.0", 15 | "react": "^16.9.0", 16 | "react-dom": "^16.9.0", 17 | "react-scripts": "^3.1.1", 18 | "react-scripts-ts": "^4.0.8", 19 | "zakalwe": "^1.0.10" 20 | }, 21 | "devDependencies": { 22 | "@types/jest": "^23.3.12", 23 | "@types/react": "^16.9.2", 24 | "@types/react-dom": "^16.9.0", 25 | "@types/react-test-renderer": "^16.9.0", 26 | "react-test-renderer": "^16.9.0", 27 | "typescript": "^3.6.2" 28 | }, 29 | "author": "RJ Zaworski (https://rjzaworski.com)", 30 | "scripts": { 31 | "start": "react-scripts-ts start", 32 | "build": "react-scripts-ts build", 33 | "test": "react-scripts-ts test --env=jsdom", 34 | "eject": "react-scripts-ts eject", 35 | "watch": "tsc --watch -p ./tsconfig.json" 36 | }, 37 | "jest": {}, 38 | "browserslist": [ 39 | ">0.2%", 40 | "not dead", 41 | "not ie <= 11", 42 | "not op_mini all" 43 | ] 44 | } 45 | -------------------------------------------------------------------------------- /public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rjz/react-with-typescript/b7c01f0c6d69320e1d58213c71eefb54ea394a09/public/favicon.ico -------------------------------------------------------------------------------- /public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 11 | 12 | 13 | 14 | 91 | React with TypeScript 92 | 93 | 94 |
95 |

React with TypeScript

96 |

TypeScript implementations of familiar React.js patterns.

97 |
98 | 101 |
102 |
103 | 109 |
110 | 111 | 112 | 113 | -------------------------------------------------------------------------------- /public/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "short_name": "React App", 3 | "name": "Create React App Sample", 4 | "icons": [ 5 | { 6 | "src": "favicon.ico", 7 | "sizes": "64x64 32x32 24x24 16x16", 8 | "type": "image/x-icon" 9 | } 10 | ], 11 | "start_url": "./index.html", 12 | "display": "standalone", 13 | "theme_color": "#000000", 14 | "background_color": "#ffffff" 15 | } 16 | -------------------------------------------------------------------------------- /public/react-typescript.svg: -------------------------------------------------------------------------------- 1 | 2 | image/svg+xml -------------------------------------------------------------------------------- /src/examples/context/__tests__/__snapshots__/index.test.tsx.snap: -------------------------------------------------------------------------------- 1 | // Jest Snapshot v1, https://goo.gl/fbAQLP 2 | 3 | exports[`examples/context renders count 1`] = ` 4 | 12 | 13 | 14 | `; 15 | -------------------------------------------------------------------------------- /src/examples/context/__tests__/index.test.tsx: -------------------------------------------------------------------------------- 1 | import * as React from 'react'; 2 | import {createRenderer} from 'react-test-renderer/shallow'; 3 | 4 | import Counter from '../index'; 5 | 6 | describe('examples/context', () => { 7 | it('renders count', () => { 8 | const shallowRenderer = createRenderer(); 9 | const wrapper = shallowRenderer.render(); 10 | // wrapper.setState({count: 42}); 11 | expect(wrapper).toMatchSnapshot(); 12 | }); 13 | }); 14 | -------------------------------------------------------------------------------- /src/examples/context/context.ts: -------------------------------------------------------------------------------- 1 | import * as React from 'react'; 2 | 3 | type CounterContext = { 4 | count: number, 5 | updateCount(delta: number): void, 6 | }; 7 | 8 | // Create a context wrapping some shared state 9 | export default React.createContext({ 10 | count: 42, 11 | updateCount: (n) => { 12 | // Prevent a `Consumer` without a matching `Provider` above it in the tree 13 | // from invoking the default implementation 14 | throw new Error('updateCount() not implemented'); 15 | } 16 | }); 17 | -------------------------------------------------------------------------------- /src/examples/context/index.tsx: -------------------------------------------------------------------------------- 1 | import * as React from 'react'; 2 | 3 | import CounterContext from './context'; 4 | 5 | // `Count` receives its state from a context `Consumer` 6 | export const Count: React.SFC<{}> = () => ( 7 | 8 | {({ count, updateCount }) => ( 9 |
10 |

Count: {count}

11 | 12 | 13 |
14 | )} 15 |
16 | ); 17 | 18 | type ContainerState = { 19 | count: number, 20 | }; 21 | 22 | // `Container` acts as a state container and context `Provider` 23 | class Container extends React.Component<{}, ContainerState> { 24 | readonly state = { 25 | count: 43, 26 | }; 27 | 28 | increment = (delta: number) => this.setState({ 29 | count: this.state.count + delta 30 | }) 31 | 32 | render() { 33 | return ( 34 | 40 | 41 | 42 | ); 43 | } 44 | } 45 | 46 | export default Container; 47 | -------------------------------------------------------------------------------- /src/examples/higher-order-component/index.tsx: -------------------------------------------------------------------------------- 1 | import * as React from 'react'; 2 | import { getDisplayName } from '../utils'; 3 | 4 | type HOC = (C: React.ComponentType

) => 5 | React.ComponentType; 6 | 7 | function loadable

(predicate: (p: P) => boolean): HOC { 8 | // Return a higher-order component implementing the "loadable" behavior 9 | // See: https://goo.gl/TxPPCw 10 | return (C: React.ComponentType

) => { 11 | const LoadableComponent: React.SFC

= (props: P) => { 12 | if (predicate(props)) { 13 | return

Just a moment, please...
; 14 | } 15 | return ; 16 | }; 17 | 18 | // Set pretty `displayName` for dev tooling 19 | LoadableComponent.displayName = `Loadable(${getDisplayName(C)})`; 20 | return LoadableComponent; 21 | }; 22 | } 23 | 24 | type ChildProps = { 25 | isLoading: boolean, 26 | }; 27 | 28 | const isLoading = (p: ChildProps) => p.isLoading; 29 | 30 | const ChildComponent: React.SFC = () => ( 31 |
...loaded.
32 | ); 33 | 34 | export default loadable(isLoading)(ChildComponent); 35 | -------------------------------------------------------------------------------- /src/examples/hook-context/__tests__/__snapshots__/index.test.tsx.snap: -------------------------------------------------------------------------------- 1 | // Jest Snapshot v1, https://goo.gl/fbAQLP 2 | 3 | exports[`examples/context renders count 1`] = ` 4 | 12 | 13 | 14 | `; 15 | -------------------------------------------------------------------------------- /src/examples/hook-context/__tests__/index.test.tsx: -------------------------------------------------------------------------------- 1 | import * as React from 'react'; 2 | import {createRenderer} from 'react-test-renderer/shallow'; 3 | 4 | import Counter from '../index'; 5 | 6 | describe('examples/context', () => { 7 | it('renders count', () => { 8 | const shallowRenderer = createRenderer(); 9 | const wrapper = shallowRenderer.render(); 10 | // wrapper.setState({count: 42}); 11 | expect(wrapper).toMatchSnapshot(); 12 | }); 13 | }); 14 | -------------------------------------------------------------------------------- /src/examples/hook-context/context.ts: -------------------------------------------------------------------------------- 1 | import * as React from 'react'; 2 | 3 | type CounterContext = { 4 | count: number, 5 | updateCount(delta: number): void, 6 | }; 7 | 8 | // Create a context wrapping some shared state 9 | export default React.createContext({ 10 | count: 42, 11 | updateCount: (n) => { 12 | // Prevent a `Consumer` without a matching `Provider` above it in the tree 13 | // from invoking the default implementation 14 | throw new Error('updateCount() not implemented'); 15 | } 16 | }); 17 | -------------------------------------------------------------------------------- /src/examples/hook-context/index.tsx: -------------------------------------------------------------------------------- 1 | import * as React from 'react'; 2 | 3 | import CounterContext from './context'; 4 | 5 | // `Count` receives its state from a context `Consumer` 6 | export const Count: React.SFC<{}> = () => { 7 | const { count, updateCount } = React.useContext(CounterContext); 8 | 9 | return ( 10 |
11 |

Count: {count}

12 | 13 | 14 |
15 | ); 16 | }; 17 | 18 | type ContainerState = { 19 | count: number; 20 | }; 21 | 22 | // `Container` acts as a state container and context `Provider` 23 | class Container extends React.Component<{}, ContainerState> { 24 | readonly state = { 25 | count: 43 26 | }; 27 | 28 | increment = (delta: number) => 29 | this.setState({ 30 | count: this.state.count + delta 31 | }) 32 | 33 | render() { 34 | return ( 35 | 41 | 42 | 43 | ); 44 | } 45 | } 46 | 47 | export default Container; 48 | -------------------------------------------------------------------------------- /src/examples/hook-effect/__tests__/__snapshots__/index.test.tsx.snap: -------------------------------------------------------------------------------- 1 | // Jest Snapshot v1, https://goo.gl/fbAQLP 2 | 3 | exports[`examples/hook-effect renders count 1`] = ` 4 |
5 |

6 | Count: 7 | 0 8 |

9 |
10 | `; 11 | -------------------------------------------------------------------------------- /src/examples/hook-effect/__tests__/index.test.tsx: -------------------------------------------------------------------------------- 1 | import * as React from 'react'; 2 | import {createRenderer} from 'react-test-renderer/shallow'; 3 | 4 | import Counter from '../index'; 5 | 6 | describe('examples/hook-effect', () => { 7 | const shallowRenderer = createRenderer(); 8 | 9 | it('renders count', () => { 10 | const result = shallowRenderer.render(); 11 | expect(result).toMatchSnapshot(); 12 | }); 13 | }); 14 | -------------------------------------------------------------------------------- /src/examples/hook-effect/index.tsx: -------------------------------------------------------------------------------- 1 | import * as React from 'react'; 2 | 3 | export function useCounter(interval: number = 1000): number { 4 | const [state, setState] = React.useState({count: 0}); 5 | const increment = () => setState({count: state.count + 1}); 6 | 7 | React.useEffect(() => { 8 | const tictoc = window.setInterval(increment, interval); 9 | return () => window.clearInterval(tictoc); 10 | }); 11 | 12 | return state.count; 13 | } 14 | 15 | const Counter: React.SFC<{}> = () => { 16 | const count = useCounter(1000); 17 | return ( 18 |
19 |

Count: {count}

20 |
21 | ); 22 | }; 23 | 24 | export default Counter; 25 | -------------------------------------------------------------------------------- /src/examples/hook-reducer/__tests__/__snapshots__/index.test.tsx.snap: -------------------------------------------------------------------------------- 1 | // Jest Snapshot v1, https://goo.gl/fbAQLP 2 | 3 | exports[`examples/hook-reducer renders count 1`] = ` 4 |
5 |

6 | Count: 7 | 0 8 |

9 |
10 | 15 | 20 |
21 |
22 | `; 23 | -------------------------------------------------------------------------------- /src/examples/hook-reducer/__tests__/index.test.tsx: -------------------------------------------------------------------------------- 1 | import * as React from 'react'; 2 | import {createRenderer} from 'react-test-renderer/shallow'; 3 | 4 | import Counter from '../index'; 5 | 6 | describe('examples/hook-reducer', () => { 7 | const shallowRenderer = createRenderer(); 8 | 9 | it('renders count', () => { 10 | const result = shallowRenderer.render(); 11 | expect(result).toMatchSnapshot(); 12 | }); 13 | }); 14 | -------------------------------------------------------------------------------- /src/examples/hook-reducer/index.tsx: -------------------------------------------------------------------------------- 1 | import * as React from 'react'; 2 | 3 | import reducer from '../reducer'; 4 | 5 | function Counter() { 6 | const [state, dispatch] = React.useReducer(reducer, {count: 0}); 7 | 8 | return ( 9 |
10 |

Count: {state.count}

11 |
12 | 13 | 14 |
15 |
16 | ); 17 | } 18 | 19 | export default Counter; 20 | -------------------------------------------------------------------------------- /src/examples/reducer-component/__tests__/reducerComponent.test.tsx: -------------------------------------------------------------------------------- 1 | import * as React from 'react'; 2 | import {createRenderer} from 'react-test-renderer/shallow'; 3 | 4 | import * as RC from '../reducerComponent'; 5 | 6 | type Action = 'TIC'; 7 | 8 | type State = { 9 | count: number; 10 | }; 11 | 12 | const TickerView: RC.Component = () =>
; 13 | 14 | const reduceCount = (s: State, a: Action) => ({ 15 | count: +s.count + (a === 'TIC' ? 1 : 0), 16 | }); 17 | 18 | const Ticker = RC.make(reduceCount, {count: 42})(TickerView); 19 | 20 | describe('examples/reducerComponent', () => { 21 | it('sets .displayName', () => { 22 | expect(Ticker.displayName).toEqual('ReducerComponent(TickerView)'); 23 | }); 24 | 25 | it('sets child props', () => { 26 | const renderer = createRenderer(); 27 | renderer.render(); 28 | const component = renderer.getRenderOutput(); 29 | expect(component.type).toEqual(TickerView); 30 | }); 31 | 32 | describe('.send', () => { 33 | it('updates parent state', () => { 34 | const renderer = createRenderer(); 35 | renderer.render(); 36 | const component = renderer.getRenderOutput(); 37 | component.props.send('TIC'); 38 | const nextComponent = renderer.getRenderOutput(); 39 | expect(nextComponent.props.count).toEqual(43); 40 | }); 41 | }); 42 | }); 43 | -------------------------------------------------------------------------------- /src/examples/reducer-component/index.tsx: -------------------------------------------------------------------------------- 1 | import * as React from 'react'; 2 | 3 | import reducer, { Action, State } from '../reducer'; 4 | 5 | import * as RC from './reducerComponent'; 6 | 7 | const Counter: RC.Component = ({ count, send }) => ( 8 |
9 |

Count: {count}

10 |
11 | 12 | 13 |
14 |
15 | ); 16 | 17 | export default RC.make(reducer, { count: 0 })(Counter); 18 | -------------------------------------------------------------------------------- /src/examples/reducer-component/reducerComponent.tsx: -------------------------------------------------------------------------------- 1 | import * as React from 'react'; 2 | import { getDisplayName } from '../utils'; 3 | 4 | type Sender = { 5 | send(a: A): void 6 | }; 7 | 8 | export type Component = React.SFC>; 9 | 10 | export type Reducer = (state: S, action: A) => S; 11 | 12 | export const make = (reduce: Reducer, initialState: S) => 13 | (C: Component) => 14 | class ReducerComponent extends React.Component<{}, S> { 15 | 16 | // Set pretty `displayName` for dev tooling 17 | static displayName = `ReducerComponent(${getDisplayName(C)})`; 18 | 19 | readonly state: S = initialState; 20 | 21 | send = (a: A) => 22 | this.setState(reduce(this.state, a)) 23 | 24 | render() { 25 | return ; 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /src/examples/reducer.ts: -------------------------------------------------------------------------------- 1 | export type Action = 'INCREMENT' | 'DECREMENT'; 2 | 3 | export type State = { 4 | count: number; 5 | }; 6 | 7 | type Reducer = (state: S, action: A) => S; 8 | 9 | const reducer: Reducer = (state, action) => { 10 | switch (action) { 11 | case 'INCREMENT': 12 | return {count: state.count + 1}; 13 | case 'DECREMENT': 14 | if (state.count === 0) { 15 | return state; 16 | } 17 | return {count: state.count - 1}; 18 | default: 19 | return state; 20 | } 21 | }; 22 | 23 | export default reducer; 24 | -------------------------------------------------------------------------------- /src/examples/render-props/__tests__/__snapshots__/index.test.tsx.snap: -------------------------------------------------------------------------------- 1 | // Jest Snapshot v1, https://goo.gl/fbAQLP 2 | 3 | exports[`examples/render-props renders expected count 1`] = ` 4 |
5 |

6 | Count: 7 | 44 8 |

9 | 14 | 19 |
20 | `; 21 | -------------------------------------------------------------------------------- /src/examples/render-props/__tests__/index.test.tsx: -------------------------------------------------------------------------------- 1 | import * as React from 'react'; 2 | import * as TestRenderer from 'react-test-renderer'; 3 | 4 | import Counter from '../index'; 5 | 6 | describe('examples/render-props', () => { 7 | it('renders expected count', () => { 8 | const renderer = TestRenderer.create(); 9 | expect(renderer.toJSON()).toMatchSnapshot(); 10 | }); 11 | }); 12 | -------------------------------------------------------------------------------- /src/examples/render-props/index.tsx: -------------------------------------------------------------------------------- 1 | import * as React from 'react'; 2 | 3 | type CounterState = { 4 | count: number, 5 | }; 6 | 7 | type ChildProps = CounterState & { 8 | increment(d: number): void, 9 | }; 10 | 11 | type CounterProps = { 12 | render(state: ChildProps): React.ReactNode, 13 | }; 14 | 15 | class CounterDataProvider extends React.Component { 16 | readonly state: CounterState = { 17 | count: 44, 18 | }; 19 | 20 | increment = (delta: number) => 21 | this.setState({ count: this.state.count + delta }) 22 | 23 | render() { 24 | return this.props.render({ 25 | ...this.state, 26 | increment: this.increment, 27 | }); 28 | } 29 | } 30 | 31 | export default () => ( 32 | ( 34 |
35 |

Count: {props.count}

36 | 37 | 38 |
39 | )} 40 | /> 41 | ); 42 | -------------------------------------------------------------------------------- /src/examples/utils.ts: -------------------------------------------------------------------------------- 1 | import * as React from 'react'; 2 | 3 | export const getDisplayName =

(C: React.ComponentType

): string => 4 | C.displayName || C.name || 'Component'; 5 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import Counter from './Counter'; 4 | 5 | ReactDOM.render(, document.getElementById('root')); 6 | -------------------------------------------------------------------------------- /src/index.tsx: -------------------------------------------------------------------------------- 1 | import * as React from 'react'; 2 | import * as ReactDOM from 'react-dom'; 3 | import ReducerComponent from './examples/reducer-component'; 4 | import HigherOrderComponent from './examples/higher-order-component'; 5 | import RenderPropsComponent from './examples/render-props'; 6 | import HookEffectComponent from './examples/hook-effect'; 7 | import HookContextComponent from './examples/hook-context'; 8 | import HookReducerComponent from './examples/hook-reducer'; 9 | import ContextComponent from './examples/context'; 10 | 11 | type ExampleProps = { 12 | title: string | React.ReactNode; 13 | link: string; 14 | children: React.ReactNode; 15 | }; 16 | 17 | const srcUrl = (link: string) => 18 | `https://github.com/rjz/react-with-typescript/blob/master/src/${link}`; 19 | 20 | const Example = (props: ExampleProps) => ( 21 |

28 | ); 29 | 30 | ReactDOM.render( 31 |
32 | useEffect() Component} 34 | link="hook-effect" 35 | > 36 | 37 | 38 | 39 | useReducer() Component} 41 | link="hook-reducer" 42 | > 43 | 44 | 45 | 46 | useContext() Component} 48 | link="hook-context" 49 | > 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 |
, 70 | document.getElementById('root') as HTMLElement, 71 | ); 72 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "baseUrl": "src", 4 | "outDir": "build/dist", 5 | "target": "es5", 6 | "lib": ["es6", "dom"], 7 | "sourceMap": true, 8 | "allowJs": false, 9 | "jsx": "react", 10 | "moduleResolution": "node", 11 | "rootDir": "src", 12 | "forceConsistentCasingInFileNames": true, 13 | "noImplicitReturns": true, 14 | "noImplicitThis": true, 15 | "noImplicitAny": true, 16 | "strictNullChecks": true, 17 | "suppressImplicitAnyIndexErrors": true, 18 | "noUnusedLocals": true 19 | }, 20 | "exclude": [ 21 | "node_modules", 22 | "build", 23 | "scripts", 24 | "acceptance-tests", 25 | "webpack", 26 | "jest", 27 | "src/setupTests.ts" 28 | ] 29 | } 30 | -------------------------------------------------------------------------------- /tsconfig.prod.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./tsconfig.json", 3 | "compilerOptions": { 4 | "module": "commonjs" 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /tsconfig.test.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./tsconfig.json", 3 | "compilerOptions": { 4 | "module": "commonjs" 5 | } 6 | } -------------------------------------------------------------------------------- /tslint.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": ["tslint-react"], 3 | "rules": { 4 | "align": [true, "parameters", "arguments", "statements"], 5 | "ban": false, 6 | "class-name": true, 7 | "comment-format": [true, "check-space"], 8 | "curly": true, 9 | "eofline": false, 10 | "forin": true, 11 | "indent": [true, "spaces"], 12 | "interface-name": [true, "never-prefix"], 13 | "jsdoc-format": true, 14 | "jsx-no-lambda": false, 15 | "jsx-no-multiline-js": false, 16 | "label-position": true, 17 | "max-line-length": [true, 120], 18 | "member-ordering": [ 19 | true, 20 | "public-before-private", 21 | "static-before-instance", 22 | "variables-before-functions" 23 | ], 24 | "no-any": true, 25 | "no-arg": true, 26 | "no-bitwise": true, 27 | "no-console": [ 28 | false, 29 | "log", 30 | "error", 31 | "debug", 32 | "info", 33 | "time", 34 | "timeEnd", 35 | "trace" 36 | ], 37 | "no-consecutive-blank-lines": true, 38 | "no-construct": true, 39 | "no-debugger": true, 40 | "no-duplicate-variable": true, 41 | "no-empty": true, 42 | "no-eval": true, 43 | "no-shadowed-variable": true, 44 | "no-string-literal": true, 45 | "no-switch-case-fall-through": true, 46 | "no-trailing-whitespace": false, 47 | "no-unused-expression": true, 48 | "no-use-before-declare": true, 49 | "one-line": [ 50 | true, 51 | "check-catch", 52 | "check-else", 53 | "check-open-brace", 54 | "check-whitespace" 55 | ], 56 | "quotemark": [true, "single", "jsx-double"], 57 | "radix": true, 58 | "semicolon": [true, "always"], 59 | "switch-default": true, 60 | 61 | "trailing-comma": [false], 62 | 63 | "triple-equals": [true, "allow-null-check"], 64 | "typedef": [true, "parameter", "property-declaration"], 65 | "typedef-whitespace": [ 66 | true, 67 | { 68 | "call-signature": "nospace", 69 | "index-signature": "nospace", 70 | "parameter": "nospace", 71 | "property-declaration": "nospace", 72 | "variable-declaration": "nospace" 73 | } 74 | ], 75 | "variable-name": [ 76 | true, 77 | "ban-keywords", 78 | "check-format", 79 | "allow-leading-underscore", 80 | "allow-pascal-case" 81 | ], 82 | "whitespace": [ 83 | false, 84 | "check-branch", 85 | "check-decl", 86 | "check-module", 87 | "check-operator", 88 | "check-separator", 89 | "check-type", 90 | "check-typecast" 91 | ] 92 | } 93 | } 94 | --------------------------------------------------------------------------------