码迷,mamicode.com
首页 > 其他好文 > 详细

react入门-refs

时间:2018-03-04 21:25:53      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:http   super   lang   pat   this   device   name   round   class   

vue有ref来获取template里面的子组件/子元素,react当然也有:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <div id="app"></div>
    <script src="common/react.js"></script>
    <script src="common/react-dom.js"></script>
    <script src="https://cdn.bootcss.com/babel-core/5.8.38/browser.js"></script>
    <script type="text/babel">
        class App extends React.Component {
            constructor(opts) {
                super(opts)
                this.focus = this.focus.bind(this)
            }
            focus() {
                //this.refs.amie获取的是原生的"<input ref="amie" type="text" />"DOM
                this.refs.amie.focus()
            }
            render() {
                return <div>
                    <input ref="amie" type="text" />
                    <input type="button" value="focus" onClick={this.focus}/>
                </div>
            }
        }
        ReactDOM.render(
            <App />,
            document.getElementById(app)
        )
    </script>
</body>
</html>

 

react入门-refs

标签:http   super   lang   pat   this   device   name   round   class   

原文地址:https://www.cnblogs.com/amiezhang/p/8505909.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!