1. 程式人生 > >react入門-refs

react入門-refs

struct component script round char button opts tel edge

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