非受控组件
- 不受状态管理的组件(可以基于ref获取Dom元素进行操作);
- 使用方式如下
- ref="xxx" 不推荐使用
- ref={x => this.xxx = x}
- React.createRef
- 一般的使用ref的场景
- 赋值给标签,获取dom元素
- 赋值给类组件, 获取子组件的实例(调用里面的方法等)
- 函数组件不能直接设置, 但是可以基于 React.forwardRef进行ref的转发
import React, { PureComponent, createRef } from 'react'
export default class MyRef extends PureComponent {
constructor() {
super();
this.cRef = createRef();
}
state = {
num: 0
}
box = null
handle = () => {
this.setState({
num: this.state.num + 1
})
}
handle1 = () => {
this.refs.box.innerHTML++;
}
handle2 = () => {
console.log(this.box);
this.box.innerHTML++;
}
handle3 = () => {
this.cRef.current.innerHTML++;
}
render() {
return (
<div style={{padding: '20px'}}>
<span>{ this.state.num }</span>
<br />
<button onClick={this.handle}>plus by state</button>
<hr />
<div ref="box" style={{marginTop: '20px'}}>0</div>
<button onClick={this.handle1}>plus by ref1</button>
<hr />
<div ref={x => this.box = x} style={{marginTop: '20px'}}>0</div>
<button onClick={this.handle2}>plus by ref2</button>
<hr />
<div ref={this.cRef} style={{marginTop: '20px'}}>0</div>
<button onClick={this.handle3}>plus by ref3</button>
</div>
)
}
}

父组件传递给子组件
- 直接赋值给函数子组件会报错,子组件需要用forwordRef包裹,进行ref转发
import React, { PureComponent, createRef } from 'react'
import Son from './Son'
import Son2 from './Son2'
export default class Parent extends PureComponent {
cRef1 = createRef();
cRef2 = createRef();
componentDidMount() {
console.log(this.cRef1, this.cRef2)
}
render() {
return (
<div>
<Son ref={this.cRef1}/>
<Son2 ref={this.cRef2}/>
</div>
)
}
}
import React, { PureComponent } from 'react'
export default class Son extends PureComponent {
render() {
return (
<div>Son</div>
)
}
}
import React, { forwardRef } from 'react'
const Son2 = forwardRef((props, ref) => {
return (
<div>
<span>Son2</span>
<div ref={ref}>我是有ref了</div>
</div>
)
})
export default Son2;
