Skip to content

Commit 159c82d

Browse files
committed
Merge pull request #15 from locol23/feature/add-recompose-state-base
feat: add recompose-state-base
2 parents 52b617d + dff8e56 commit 159c82d

File tree

12 files changed

+6655
-0
lines changed

12 files changed

+6655
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Learn React with TypeScript
1616
- [Base](https://github.com/locol23/learn-react-ts/tree/master/packages/styled-components-base)
1717
- Recompose
1818
- State(withStateHandlers)
19+
- [Base](https://github.com/locol23/learn-react-ts/tree/master/packages/recompose-state-base)
1920
- React Hooks
2021
- State(useState)
2122
- [Base](https://github.com/locol23/learn-react-ts/tree/master/packages/hooks-state-base)
+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"presets": ["react", ["env", {"targets": {"chorme": 67} }]],
3+
"plugins": ["transform-object-rest-spread", "transform-class-properties"]
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"tslint.autoFixOnSave": true
3+
}
+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# hooks-state-base
2+
3+
[Hooks API Reference](https://reactjs.org/docs/hooks-reference.html)
4+
5+
## useState
6+
7+
```tsx
8+
const App = () => {
9+
const [text, setText] = useState('init')
10+
11+
return <div>{text}</div> // text = 'init'
12+
}
13+
```
14+
15+
```tsx
16+
const App = (props: AppProps) => {
17+
const [text, setText] = useState(() => `Hello ${props.text}`) // props.text = 'World'
18+
19+
return <div>{text}</div> // text = 'Hello World'
20+
}
21+
```
22+
23+
## useEffect
24+
25+
```tsx
26+
const App = () => {
27+
const [text, setText] = useState('init')
28+
useEffect(() => setText('update'), []) // like componentDidMount
29+
30+
return <div>{text}</div> // text = 'update'
31+
}
32+
```
33+
34+
# useCallback
35+
36+
Returns a memoized callback.
37+
38+
```tsx
39+
const App = () => {
40+
const [text, setText] = useState('init')
41+
const handler = useCallback(() => setText(text), [text])
42+
43+
return (
44+
<div>{text}</div> // text = 'init'
45+
<button onClick={handler}>button</button>
46+
)
47+
}
48+
```
49+
50+
# useMemo
51+
52+
Returns a memoized value.
53+
useMemo is like memoize of [reselect](https://github.com/reduxjs/reselect)
54+
55+
```tsx
56+
const App = () => {
57+
const [text, setText] = useState('init')
58+
useMemo(() => text, [text])
59+
60+
return <div>{text}</div> // text = 'init'
61+
}
62+
```
+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
"name": "recompose-state-base",
3+
"version": "1.0.0",
4+
"main": "index.js",
5+
"repository": "https://github.com/locol23/learn-react-ts.git",
6+
"license": "MIT",
7+
"scripts": {
8+
"clean": "rimraf dist",
9+
"parcel": "parcel src/index.html",
10+
"lint": "tslint src/**/*.ts{,x}",
11+
"lint-watch": "chokidar src/**/*.ts{,x} -c 'npm run lint'",
12+
"dev": "run-s clean lint && run-p lint-watch parcel",
13+
"build": "run-s clean lint && parcel build src/index.html"
14+
},
15+
"dependencies": {
16+
"react": "16.7.0-alpha.0",
17+
"react-dom": "16.7.0-alpha.0",
18+
"recompose": "^0.30.0",
19+
"styled-components": "^4.0.0"
20+
},
21+
"devDependencies": {
22+
"@types/react": "^16.7.8",
23+
"@types/react-dom": "^16.0.11",
24+
"@types/recompose": "0.27.0",
25+
"@types/styled-components": "^4.1.2",
26+
"babel-core": "^6.26.3",
27+
"babel-plugin-transform-class-properties": "6.24.1",
28+
"babel-plugin-transform-object-rest-spread": "6.26.0",
29+
"babel-preset-env": "1.7.0",
30+
"babel-preset-react": "6.24.1",
31+
"chokidar-cli": "1.2.1",
32+
"json5": "^2.1.0",
33+
"npm-run-all": "4.1.3",
34+
"parcel-bundler": "^1.10.3",
35+
"prettier": "1.15.2",
36+
"rimraf": "2.6.2",
37+
"tslint": "5.11.0",
38+
"tslint-config-prettier": "1.16.0",
39+
"tslint-plugin-prettier": "2.0.1",
40+
"typescript": "3.1.6"
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import React from 'react'
2+
import {
3+
compose,
4+
setDisplayName,
5+
StateHandler,
6+
StateHandlerMap,
7+
withStateHandlers,
8+
} from 'recompose'
9+
import { Layout } from '../utils/Layout'
10+
11+
const Component = (props: AppProps) => {
12+
const handler = () => props.setText('Change Text!!')
13+
14+
return (
15+
<React.Fragment>
16+
<Layout>
17+
<h1>{props.text}</h1>
18+
</Layout>
19+
<Layout>
20+
<button onClick={handler}>change</button>
21+
</Layout>
22+
</React.Fragment>
23+
)
24+
}
25+
26+
interface AppProps {
27+
text: string
28+
setText: (str: string) => StateHandler<AppEnhancerStateProps>
29+
}
30+
31+
interface AppEnhancerStateProps {
32+
text: string
33+
}
34+
35+
type AppEnhancerStateHandlerProps = {
36+
setText: (str: string) => StateHandler<AppEnhancerStateProps>
37+
} & StateHandlerMap<AppEnhancerStateProps>
38+
39+
type EnhancedAppProps = AppEnhancerStateProps &
40+
AppEnhancerStateHandlerProps &
41+
AppProps
42+
43+
const Enhancer = compose<EnhancedAppProps, AppProps>(
44+
setDisplayName('App'),
45+
withStateHandlers<
46+
AppEnhancerStateProps,
47+
AppEnhancerStateHandlerProps,
48+
AppProps
49+
>(
50+
{
51+
text: 'Function Components',
52+
},
53+
{
54+
setText: () => str => ({ text: str }),
55+
}
56+
)
57+
)
58+
59+
export const App = Enhancer(Component)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import React from 'react'
2+
import styled from 'styled-components'
3+
4+
const Container = styled.div`
5+
display: flex;
6+
justify-content: center;
7+
margin: 10px;
8+
`
9+
10+
interface LayoutProps {
11+
children: React.ReactNode
12+
}
13+
14+
export const Layout = (props: LayoutProps) => (
15+
<Container>{props.children}</Container>
16+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Learn React</title>
6+
</head>
7+
<body>
8+
<div id="app">Loading...</div>
9+
<script src="index.tsx"></script>
10+
</body>
11+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import React from 'react'
2+
import ReactDOM from 'react-dom'
3+
import { App } from './components/pages/App'
4+
5+
ReactDOM.render(<App />, document.getElementById('app'))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"compilerOptions": {
3+
"target": "esnext",
4+
"module": "ES2015",
5+
"allowJs": true,
6+
"jsx": "react",
7+
"sourceMap": true,
8+
"noImplicitAny": true,
9+
"strictNullChecks": true,
10+
"strictFunctionTypes": true,
11+
"noImplicitThis": true,
12+
"alwaysStrict": true,
13+
"moduleResolution": "node",
14+
"allowSyntheticDefaultImports": true,
15+
"strictPropertyInitialization": true,
16+
"rootDir": "src",
17+
"outDir": "./dist/",
18+
},
19+
"exclude": [
20+
"dist",
21+
"node_modules"
22+
]
23+
}
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"rulesDirectory": ["tslint-plugin-prettier"],
3+
"defaultSeverity": "error",
4+
"extends": ["tslint:latest", "tslint-config-prettier"],
5+
"rules": {
6+
"interface-name": false,
7+
"no-empty-interface": false,
8+
"object-literal-sort-keys" : false,
9+
"no-submodule-imports": false,
10+
"allowSyntheticDefaultImports": true,
11+
"prettier": [
12+
true, {
13+
"semi": false,
14+
"singleQuote": true,
15+
"tabWidth": 2,
16+
"bracketSpacing": true,
17+
"trailingComma": "es5"
18+
}
19+
]
20+
}
21+
}

0 commit comments

Comments
 (0)