• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

react使用ref获取

武飞扬头像
forget_feng
帮助1

1、react 函数组件使用useRef获取input框中的值

  1.  
    import React, { useRef } from "react";
  2.  
     
  3.  
    export default function App() {
  4.  
    const inputVal = useRef(null)
  5.  
    const inputValOpt = () => {
  6.  
    console.log(inputVal.current.value)
  7.  
    }
  8.  
    return (
  9.  
    <div>
  10.  
    <input type='text' ref={inputVal} />
  11.  
    <button onClick={inputValOpt}>点击</button>
  12.  
    </div>
  13.  
    )
  14.  
    }

2、react class组件ref的用法

  1.  
    import React, { Component } from 'react'
  2.  
     
  3.  
    export default class App extends Component {
  4.  
    showData = () => {
  5.  
    const { input1 } = this.refs
  6.  
    console.log(input1.value, '点击获取');
  7.  
    }
  8.  
    showData2 = () => {
  9.  
    const { input2 } = this.refs
  10.  
    console.log(input2.value, '....失去焦点的时候获取')
  11.  
    }
  12.  
    render() {
  13.  
    return (
  14.  
    <div>
  15.  
    <input type="text" placeholder="点击按钮提示数据" ref="input1" />&nbsp;&nbsp;
  16.  
    <button onClick={this.showData}>点击我提示左边的数据</button>&nbsp;&nbsp;
  17.  
    <input type="text" placeholder="失去焦点提示数据" onBlur={this.showData2} ref="input2" />&nbsp;&nbsp;
  18.  
    </div>
  19.  
    )
  20.  
    }
  21.  
    }
学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhfieihi
系列文章
更多 icon
同类精品
更多 icon
继续加载