hooks的使用
useRef
案例效果:
- 点击按钮,文本title修改
- 点击按钮,input聚焦
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30import React, { useRef, PureComponent } from 'react'
class TestChild extends PureComponent {
render() {
return (
<div>UseRefDemo</div>
)
}
}
export default function UseRefDemo() {
const titleRef = useRef()
const inputRef = useRef()
const testChild = useRef()
return (
<>
<div>UseRefDemo</div>
<div ref= {titleRef}>title</div>
<input ref = {inputRef} type = "text"></input>
<button onClick={e => changeDom()}>按钮</button>
<TestChild ref = {testChild}></TestChild>
</>
)
function changeDom(){
titleRef.current.innerHTML = "改变后的title"
inputRef.current.focus()
console.log(testChild)
}
}
hooks的使用
http://example.com/hooks的使用/