文章目录
- ref
- 实例
- 字符串形式
- 回调函数形式
- createRef
ref
组件内的标签可以定义属性来标识定义自己,有三种方式来进行定义。下面通过一个实例分别介绍
实例
做两个输入框,左边点击按钮有弹窗,右边输出后点击别处页面会有弹窗。
字符串形式
这种形式十分方便,操作起来速度快,但是react不推荐
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title>
</head>
<body><div id="test"></div><script src="../js/react.development.js"></script><script src="../js/react-dom.development.js"></script><script src="../js/babel.min.js"></script><script src="../js/prop-types.js"></script><script type="text/babel">class Demo extends React.Component {showData = () => {const {input1} = this.refsalert(this.refs.input1.value)alert(input1.value + "aaa")}showData2 = () => {const {input2} = this.refsalert(input2.value)}render() {return (<div><input ref="input1" type="text" placeholder="点击按钮提示数据"/> <button onClick={this.showData}>点我提示左侧的数据</button> <input ref="input2" onBlur={this.showData2} type="text" placeholder="失去焦点提示数据"/> </div>)}}ReactDOM.render(<Demo/>, document.getElementById("test"))</script>
</body>
</html>
运行结果:
左侧输入后点击按钮出弹窗
右侧输入后点击其他处出弹窗
回调函数形式
这种方法也比较方便,比字符形式好点。
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title>
</head>
<body><div id="test"></div><script src="../js/react.development.js"></script><script src="../js/react-dom.development.js"></script><script src="../js/babel.min.js"></script><script src="../js/prop-types.js"></script><script type="text/babel">class Demo extends React.Component {showData = () => {const {input1} = thisalert(input1.value)}showData2 = () => {const {input2} = thisalert(input2.value)}//内联函数的写法和类绑定函数的写法没什么区别的render() {return (<div><input ref={c => this.input1 = c} type="text" placeholder="点击按钮提示数据"/> <button onClick={this.showData}>点我提示左侧的数据</button> <input onBlur={this.showData2} ref={c => this.input2 = c} type="text" placeholder="失去焦点提示数据"/> </div>)}}ReactDOM.render(<Demo/>, document.getElementById("test"))</script>
</body>
</html>
输出结果和上面一样
createRef
这种方式好像比较麻烦,但是react推荐这种方法
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title>
</head>
<body><div id="test"></div><script src="../js/react.development.js"></script><script src="../js/react-dom.development.js"></script><script src="../js/babel.min.js"></script><script src="../js/prop-types.js"></script><script type="text/babel">class Demo extends React.Component {// React.createRef调用后可以返回一个容器,该容器可以存储被ref所标识的节点myRef = React.createRef()myRef2 = React.createRef()showData = () => {alert(this.myRef.current.value)}showData2 = () => {alert(this.myRef2.current.value)}//内联函数的写法和类绑定函数的写法没什么区别的render() {return (<div><input ref={this.myRef} type="text" placeholder="点击按钮提示数据"/> <button onClick={this.showData}>点我提示左侧的数据</button> <input onBlur={this.showData2} ref={this.myRef2} type="text" placeholder="失去焦点提示数据"/> </div>)}}ReactDOM.render(<Demo/>, document.getElementById("test"))</script>
</body>
</html>
输出结果和上面一样