React 中 refs

React 中 refs 的作用是什么?

  • Refs 是 React 提供给我们的安全访问 DOM 元素或者某个组件实例的句柄。
    我们可以为元素添加 ref 属性然后在回调函数中接受该元素在 DOM 树中的句柄,该值会作为回调函数的第一个参数返回:
class CustomForm extends Component {
    
    
  handleSubmit = () ={
    
    
    console.log('Input Value: ', this.input.value);
  };
  render() {
    
    
    return (
      ‹form onSubmit={
    
    this.handleSubmit}›
        ‹input type='text' ref={
    
    input =(this.input = input)} /›
        ‹button type='submit'›Submit‹/button›
      ‹/form›
    );
  }
}
	上述代码中的 input 域包含了一个 ref 属性,
	该属性声明的回调函数会接收 input 对应的 DOM 元素,
	我们将其绑定到 this 指针以便在其他的类函数中使用。

​	另外值得一提的是,refs 并不是类组件的专属,函数式组件同样能够利用闭包暂存其值:

function CustomForm({
    
     handleSubmit }) {
    
    
  let inputElement;
  return (
    ‹form onSubmit={
    
    () =handleSubmit(inputElement.value)}›
      ‹input type='text' ref={
    
    input =(inputElement = input)} /›
      ‹button type='submit'›Submit‹/button›
    ‹/form›
  );
}



ref是一个函数,为什么?

class Test extends React.Component{
    
    
	componentDidMount(){
    
    
		console.log(this.el);
	}
	render(){
    
    
		//react在销毁组件的时候会帮助我们清空掉ref的相关引用,这样可以防止内存泄漏等一系列问题。
		return <div ref={
    
    el=>this.el=el}></div>
	}
}

refs的作用业务场景?

例如获取图片的宽度与高度。

<input ref={
    
    el=>this.input = el}/>

class Test extends React.Component{
    
    
	constructor(props){
    
    
		super(props);
		this.handleWidowScroll = this.handleWidowScroll.bind(this);
	}
	handleWidowScroll(){
    
    
		this.setState({
    
    
			top:document.body.scrollTop
		})
	}
	componentDidMount(){
    
    
		window.addEventListener("scroll",this.handleWindowScroll);
	}
	componentWillUnmount(){
    
    
		window.removeEventListener("scroll",this.handleWindowScroll);
	}
}

猜你喜欢

转载自blog.csdn.net/qq_45271323/article/details/106101972