Skip to content

Commit

Permalink
Switch example to TypeScript
Browse files Browse the repository at this point in the history
  • Loading branch information
ianobermiller committed Nov 5, 2018
1 parent 92ee570 commit cf887c6
Show file tree
Hide file tree
Showing 27 changed files with 1,644 additions and 17,705 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ Before you can use the hook, you must put your Redux store into `Context`:
```tsx
// Store.js

import React from 'react';
import {createStore} from 'redux';
import reducer from './reducer';

Expand Down Expand Up @@ -131,7 +130,6 @@ project specific wrappers for `useMappedState` and `useDispatch`:
```tsx
// Store.js

import React from 'react';
import {
useDispatch as useDispatchGeneric,
useMappedState as useMappedStateGeneric,
Expand Down
21 changes: 21 additions & 0 deletions example/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# See https://help.github.com/ignore-files/ for more about ignoring files.

# dependencies
/node_modules

# testing
/coverage

# production
/build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*
2,164 changes: 0 additions & 2,164 deletions example/README.md

This file was deleted.

16 changes: 10 additions & 6 deletions example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,21 @@
"license": "MIT",
"private": true,
"dependencies": {
"prop-types": "^15.6.2",
"react": "^16.7.0-alpha.0",
"react-dom": "^16.7.0-alpha.0",
"react-scripts": "^1.1.4",
"react-scripts-ts": "3.1.0",
"redux": "^4.0.1",
"redux-react-hook": "link:.."
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
"start": "react-scripts-ts start",
"build": "react-scripts-ts build",
"test": "react-scripts-ts test --env=jsdom",
"eject": "react-scripts-ts eject"
},
"devDependencies": {
"@types/react": "^16.3.13",
"@types/react-dom": "^16.0.5",
"typescript": "^3.1.3"
}
}
8 changes: 0 additions & 8 deletions example/public/manifest.json

This file was deleted.

6 changes: 3 additions & 3 deletions example/src/App.react.js → example/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import TodoInput from './TodoInput.react';
import TodoList from './TodoList.react';
import * as React from 'react';
import TodoInput from './TodoInput';
import TodoList from './TodoList';

export default function App() {
return (
Expand Down
25 changes: 20 additions & 5 deletions example/src/Store.js → example/src/Store.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
import React from 'react';
import reducer from './reducer';
import {createStore} from 'redux';
import * as React from 'react';
import {Action, createStore} from 'redux';
import {
useDispatch as useDispatchGeneric,
useMappedState as useMappedStateGeneric,
} from 'redux-react-hook';
import reducer from './reducer';

export interface IState {
lastUpdated: number;
todos: string[];
}

export type Action =
| {
type: 'add todo';
todo: string;
}
| {
type: 'delete todo';
index: number;
};

export function makeStore() {
return createStore(reducer, {
Expand All @@ -13,9 +28,9 @@ export function makeStore() {
});
}

export const Context = React.createContext(null);
export const Context = React.createContext(makeStore());

export function useMappedState(mapState) {
export function useMappedState<T>(mapState: (state: IState) => T): T {
// Wrap the generic useMappedState so that you don't have to pass in the store Context all over
// If you want to pass only a subset of state, this is also the place to do it. For example,
// if your store schema has an undo stack, and you only want to pass the current state.
Expand Down
22 changes: 0 additions & 22 deletions example/src/TodoInput.react.js

This file was deleted.

21 changes: 21 additions & 0 deletions example/src/TodoInput.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import * as React from 'react';
import {useDispatch} from './Store';

export default function TodoInput() {
const [newTodo, setNewTodo] = React.useState('');
const dispatch = useDispatch();

return (
<input
type="text"
onChange={e => setNewTodo(e.target.value)}
onKeyDown={e => {
if (e.key === 'Enter') {
dispatch({type: 'add todo', todo: newTodo});
setNewTodo('');
}
}}
value={newTodo}
/>
);
}
18 changes: 0 additions & 18 deletions example/src/TodoItem.react.js

This file was deleted.

21 changes: 21 additions & 0 deletions example/src/TodoItem.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import * as React from 'react';
import {IState, useDispatch, useMappedState} from './Store';

export default function TodoItem({index}: {index: number}) {
const mapState = React.useCallback((state: IState) => state.todos[index], [
index,
]);
const todo = useMappedState(mapState);

const dispatch = useDispatch();
const deleteTodo = React.useCallback(
() => dispatch({type: 'delete todo', index}),
[index],
);

return (
<li>
<button onClick={deleteTodo}>x</button> {todo}
</li>
);
}
10 changes: 5 additions & 5 deletions example/src/TodoList.react.js → example/src/TodoList.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import React from 'react';
import {useMappedState} from './Store';
import TodoItem from './TodoItem.react';
import * as React from 'react';
import {IState, useMappedState} from './Store';
import TodoItem from './TodoItem';

const mapState = state => ({
const mapState = (state: IState) => ({
lastUpdated: state.lastUpdated,
todoCount: state.todos.length,
});
Expand All @@ -13,7 +13,7 @@ export default function TodoList() {
<div>
<div>Count: {todoCount}</div>
<ul>
{new Array(todoCount).fill().map((_, index) => (
{new Array(todoCount).fill(null).map((_, index) => (
<TodoItem index={index} key={index} />
))}
</ul>
Expand Down
2 changes: 0 additions & 2 deletions example/src/index.css

This file was deleted.

7 changes: 3 additions & 4 deletions example/src/index.js → example/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import React from 'react';
import ReactDOM from 'react-dom';
import * as React from 'react';
import * as ReactDOM from 'react-dom';

import './index.css';
import App from './App.react';
import App from './App';
import {Context, makeStore} from './Store';

const store = makeStore();
Expand Down
4 changes: 3 additions & 1 deletion example/src/reducer.js → example/src/reducer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export default function reducer(state, action) {
import {Action, IState} from './Store';

export default function reducer(state: IState, action: Action) {
switch (action.type) {
case 'add todo': {
return {
Expand Down
15 changes: 15 additions & 0 deletions example/src/typings.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Add hooks to the react typings
declare module 'react' {
export function useCallback<T>(callback: T, dependencies: Array<any>): T;
export function useContext<T>(context: React.Context<T>): T;
export function useEffect(
didUpdate: () => (() => void) | void,
dependencies?: Array<any>,
): void;
export function useRef<T>(initialValue?: T): {current: T};
export function useState<T>(
initialState: T | (() => T),
): [T, (newState: T | (() => T)) => void];
}

export {};
31 changes: 31 additions & 0 deletions example/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"compilerOptions": {
"baseUrl": ".",
"outDir": "build/dist",
"module": "esnext",
"target": "es5",
"lib": ["es6", "dom"],
"sourceMap": true,
"allowJs": true,
"jsx": "react",
"moduleResolution": "node",
"rootDir": "src",
"forceConsistentCasingInFileNames": true,
"noImplicitReturns": true,
"noImplicitThis": true,
"noImplicitAny": true,
"importHelpers": true,
"strictNullChecks": true,
"suppressImplicitAnyIndexErrors": true,
"noUnusedLocals": true
},
"exclude": [
"build",
"scripts",
"acceptance-tests",
"webpack",
"jest",
"src/setupTests.ts",
"node_modules"
]
}
3 changes: 3 additions & 0 deletions example/tsconfig.prod.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "./tsconfig.json"
}
6 changes: 6 additions & 0 deletions example/tsconfig.test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "commonjs"
}
}
13 changes: 13 additions & 0 deletions example/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"extends": ["tslint:recommended", "tslint-react", "tslint-config-prettier"],
"linterOptions": {
"exclude": [
"config/**/*.js",
"node_modules/**/*.ts",
"coverage/lcov-report/*.js"
]
},
"rules": {
"jsx-no-lambda": false
}
}
Loading

0 comments on commit cf887c6

Please sign in to comment.