Skip to content

Commit fe5a45b

Browse files
committed
2019.08.26
1 parent 6e4116c commit fe5a45b

4 files changed

Lines changed: 97 additions & 1 deletion

File tree

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import React, { useState, useMemo } from 'react'
2+
3+
function Example7(){
4+
const [leo1, setLeo1] = useState('我是leo1')
5+
const [leo2, setLeo2] = useState('我是leo2')
6+
7+
return (
8+
<>
9+
<button onClick={() => {setLeo1(new Date().getTime())}}>我是leo1</button>
10+
<button onClick={() => {setLeo2(new Date().getTime() + ' hello')}}>我是leo2</button>
11+
<ChildComponent name={leo1}>{leo2}</ChildComponent>
12+
</>
13+
)
14+
}
15+
16+
function ChildComponent({name, children}){
17+
18+
function changeLeo1(name){
19+
console.log("11111111111111")
20+
return name + ',来了'
21+
}
22+
const action = useMemo(()=>changeLeo1(name), [name]) // 只有[name]中变量发生改变才会执行
23+
return (
24+
<>
25+
<div> {action} </div>
26+
<div> {children} </div>
27+
</>
28+
)
29+
}
30+
31+
export default Example7
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import React, { useState, useRef, useEffect } from 'react'
2+
3+
function Example8(){
4+
const inputEl = useRef(null);
5+
const onLeo = () => {
6+
inputEl.current.value = "hello leo!"
7+
console.log(inputEl)
8+
}
9+
10+
const [text, setText] = useState('leo')
11+
const textRef = useRef()
12+
13+
useEffect(() => {
14+
textRef.current = text
15+
console.log('textRef.current',textRef.current)
16+
})
17+
18+
return (
19+
<>
20+
<input type="text" ref={inputEl} />
21+
<button onClick={onLeo}>在input中展示文字</button>
22+
<br />
23+
24+
<input value={text} onChange={e=>{setText(e.target.value)}}/>
25+
</>
26+
)
27+
}
28+
29+
export default Example8
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import React, { useState, useEffect, useCallback } from 'react'
2+
3+
function useWinSize(){
4+
const [size, setSize] = useState({
5+
width: document.documentElement.clientWidth,
6+
height: document.documentElement.clientHeight
7+
})
8+
9+
const onResize = useCallback(() => {
10+
setSize({
11+
width: document.documentElement.clientWidth,
12+
height: document.documentElement.clientHeight
13+
})
14+
},[])
15+
16+
useEffect(() => {
17+
window.addEventListener('resize', onResize)
18+
return () => {
19+
window.removeEventListener('resize', onResize)
20+
}
21+
},[])
22+
23+
return size
24+
}
25+
26+
function Example9(){
27+
const size = useWinSize()
28+
29+
return (
30+
<div>
31+
页面尺寸:{size.width} * {size.height}
32+
</div>
33+
)
34+
}
35+
36+
export default Example9

Cute-React/React-Hooks-Learning/src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from 'react';
22
import ReactDOM from 'react-dom';
3-
import Example from './Example6/Example6';
3+
import Example from './Example9';
44

55
ReactDOM.render(<Example />, document.getElementById('root'));
66

0 commit comments

Comments
 (0)