1. 兩種獲取dom節點 的方式
import React, {Fragment} from 'react'
class Test extends React.Component {
constructor(props) {
super(props);
// 創建
this.third = React.createRef();
}
componentDidMount(){
// note: 不同的方式創建ref, 取值方式不同
console.log(this.second) // <input value="second">
console.log(this.third.current); // <input value="third">
}
render() {
// 賦值
return (
<Fragment>
<input value="second" ref={(inputRef) => this.second = inputRef} />
<input value="third" ref={this.third} />
</Fragment>
)
}
}
export default Test
進階:React.forwardRef