React(八)—— 获取真实DOM

弥补一个知识点,默认props,本来写上篇文章的,忘了
     class HelloWorld extends React.Component{
         constructor(props){
             super(props)
         }
       
        
         render(){
             return(
                <h1>{this.props.name}</h1>
             )
         }
     }
     HelloWorld.defaultProps={
         name:'React'
     }
     ReactDOM.render(<HelloWorld/>,document.getElementById('div'))

当然默认的权重是最低的,实际开发很多时候还是使用三元

 <h1>{this.props.name?this.props.name:'React'}</h1>
获取真实DOM节点

React中的DOM也是虚拟DOM(virtual DOM),这点跟我们以前讲的Vue非常类似。只有当它插入文档以后,才会变成真实的DOM。React也是在虚拟DOM发生变化时,进行比对后,只渲染变化的部分,它是React极高性能的主要原因之一。

<script type="text/babel">
     class HelloWorld extends React.Component{
         constructor(props){
             super(props)
         }
       
        
         render(){
             return(
                <div>
                  <input type="text" />
                  <input type="button" value="Focus the text input"/>
                </div>
             )
         }
     }
     ReactDOM.render(<HelloWorld/>,document.getElementById('div'))
</script>

OK 初始化一下,写个效果是什么呢?就是点击Button的时候,给input添加光标

<script type="text/babel">
     class HelloWorld extends React.Component{
         constructor(props){
             super(props)
         }
         
         handleClick(){
             this.refs.myTextInput.focus()
         }
        
         render(){
             return(
                <div>
                  <input type="text" ref="myTextInput" />
                  <input type="button" value="Focus the text input" onClick={this.handleClick.bind(this)}/>
                </div>
             )
         }
     }
     ReactDOM.render(<HelloWorld/>,document.getElementById('div'))
</script>

比较简单不多说,其实我个人在vue中比较喜欢偏向vdom的操作,react还不熟悉!

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • WordPress 的核心团队正争论着为应该将哪款(前端框架)加入现在的架构之中。目前看来,暂时脱颖而出的是Rea...
    壮哉我大前端阅读 844评论 0 5
  • 40、React 什么是React?React 是一个用于构建用户界面的框架(采用的是MVC模式):集中处理VIE...
    萌妹撒阅读 1,058评论 0 1
  • Vue和React两个JavaScript框架都是当下比较受欢迎的,他们两者之间的区别有哪些,各自的优缺点是什么,...
    心之所向灬阅读 473评论 0 2
  • 历尽沧桑入世来,浮生似梦惹尘埃。 幽林只与清泉合,雅韵难同尺卷回。 万点星穹游碧野,一弯寒月落亭台。 犹思风雪征遥...
    Delia常青藤阅读 224评论 16 13
  • 小笨朋友介绍个几亿的项目。如果成功了我们就可以结婚了他说。尽管成功可能性据说不高。可是感觉就是当你努力地活着就总有...
    怀揣碧纱巾的女孩阅读 259评论 0 0