Skip to content

Commit f8f0e65

Browse files
authored
Merge pull request #62 from locol23/feature/add-use-effect-goal
feat: add useEffect Goal
2 parents 74ff67f + f5b2d20 commit f8f0e65

File tree

9 files changed

+8053
-0
lines changed

9 files changed

+8053
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Learn React with TypeScript
2222
- [Goal](https://github.com/locol23/learn-react-typescript/tree/master/packages/hooks-state-goal)
2323
- useEffect
2424
- [Base](./packages/hooks-use-effect-base)
25+
- [Goal](./packages/hooks-use-effect-goal)
2526
- useContext
2627
- [Base](./packages/hooks-use-context-base)
2728
- State(useReducer)
Lines changed: 4 additions & 0 deletions
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+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# hooks-use-effect-goal
2+
3+
[Hooks API Reference](https://reactjs.org/docs/hooks-reference.html)
4+
5+
## useEffect
6+
7+
```tsx
8+
const App = () => {
9+
const [title, setTitle] = useState('Initial Title')
10+
11+
useEffect(() => {
12+
setTitle('Use Effect')
13+
}, [])
14+
15+
return <h1>{title}</h1>
16+
}
17+
```
18+

packages/hooks-use-effect-goal/package-lock.json

Lines changed: 7931 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "hooks-use-effect-goal",
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:dev": "parcel src/index.html",
10+
"parcel:prod": "parcel build src/index.html",
11+
"dev": "run-s clean parcel:dev",
12+
"build": "run-s clean parcel:prod"
13+
},
14+
"dependencies": {
15+
"react": "^16.13.1",
16+
"react-dom": "^16.13.1"
17+
},
18+
"devDependencies": {
19+
"@types/react": "^16.9.35",
20+
"@types/react-dom": "^16.9.8",
21+
"babel-core": "^6.26.3",
22+
"npm-run-all": "^4.1.5",
23+
"parcel-bundler": "^1.12.4",
24+
"rimraf": "^3.0.0"
25+
}
26+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import React, { useEffect, useState } from 'react'
2+
3+
export const App = () => {
4+
const [title, setTitle] = useState('Initial Title')
5+
6+
useEffect(() => {
7+
setTimeout(() => {
8+
setTitle('Use Effect')
9+
}, 2000)
10+
}, [])
11+
12+
const [text, setText] = useState('Initial Text')
13+
const handlerOne = () => setText('Text 1')
14+
const handlerTwo = () => setText('Text 2')
15+
16+
useEffect(() => console.log(`current text is ${text}`), [text])
17+
18+
const createSession = () => console.log('create session')
19+
const postData = (data: { name: string }) => console.log('post data', data)
20+
const closeSession = () => console.log('close session')
21+
22+
useEffect(() => {
23+
createSession()
24+
postData({ name: 'Tom' })
25+
26+
return closeSession()
27+
}, [])
28+
29+
return (
30+
<>
31+
<h1>{title}</h1>
32+
<div>{text}</div>
33+
<button onClick={handlerOne}>click1</button>
34+
<button onClick={handlerTwo}>click2</button>
35+
</>
36+
)
37+
}
Lines changed: 11 additions & 0 deletions
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>React Typescript Boilerplate</title>
6+
</head>
7+
<body>
8+
<div id="app">Loading...</div>
9+
<script src="index.tsx"></script>
10+
</body>
11+
</html>
Lines changed: 5 additions & 0 deletions
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/App'
4+
5+
ReactDOM.render(<App />, document.getElementById('app'))
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"compilerOptions": {
3+
"target": "esnext",
4+
"module": "ES2015",
5+
"allowJs": false,
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": ["dist", "node_modules"]
20+
}

0 commit comments

Comments
 (0)