React实践:ReactBasicImgViewer

实现的功能:在点击按钮,显示图片,如图所示:
这里写图片描述

代码还在不断的完善和更新中,代码详见:https://github.com/EmilyQiRabbit/ReactBasicPicturesViewer

核心代码(HTML文件中body标签内代码):

 <body>

   <h1>Basic Example</h1>

   <div id="viewerContainer"></div>

   <script src="react/react.js"></script>
   <script src="react/react-dom.js"></script>
   <script src="react/browser.min.js"></script>
   <!-- <script src="react/react-with-addons.js"></script> -->
   <script type="text/babel">
   (function(){
     // 遮盖层(with img)
     var BlockModule = React.createClass({
       getInitialState: function() {
         return { ifShowStyle:this.props.ifShowStyle };
       },
       componentWillReceiveProps : function(nextProps){
         this.setState( { ifShowStyle : nextProps.ifShowStyle } );
       },
       imgDisappearHandler : function(){
         this.setState( { ifShowStyle:{ display: "none" } } );
       },
       render: function() {
         return (
           <div className="blockDiv" style={this.state.ifShowStyle}>
             <ImgModule imgUrl={this.props.imgUrl}></ImgModule>
             <div onClick={this.imgDisappearHandler}></div>
           </div>
         )
       }
     });

     // 图片
     var ImgModule = React.createClass({
       render: function() {
         return <img src = {this.props.imgUrl}/>;
       }
     });

     var EventElement = React.createClass({
       getInitialState: function() {
         return { ifShowStyle: { display: "none" } };
       },
       imgEmergeHandler : function(){
         this.setState( { ifShowStyle:{ display: "block" } } );
       },
       render : function(){
         return (
           <div>
             <BlockModule imgUrl={this.props.imgUrl} ifShowStyle={this.state.ifShowStyle}></BlockModule>
             <button className={this.props.ElementButtonClass} onClick={this.imgEmergeHandler}>click To Show Image</button>
           </div>
         )
       }
     });

     ReactDOM.render(
       <EventElement imgUrl="imgs/test.jpg" ElementButtonClass="btnclass">click To Show Image</EventElement>,
       /* imgUrl:图片src ;
          ElementClass:按钮样式的类名 */
       document.getElementById('viewerContainer')
     );
   })()
   </script>
 </body>

主要知识点:

1、React.createClass

2、state, getInitialState()

3、props

4、onClick事件

5、生命周期函数componentWillReceiveProps

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值