今天的码农女孩用react写了轮播图以及组件之间的不同之处

轮播图:

<style>
        * {
            margin: 0;
            padding: 0;
        }
        .box {
            margin: 50px auto;
            position: relative;
        }
        .box,.box img {
            width: 300px;
            height: 400px;
        }
        .box div {
            position: absolute;
            top:0px;
            left:0px;
            display: none;
        }
        .box .text {
            display: block;
        }
    </style>
</head>
<body>
    <div id="app"></div>
    <script type="text/babel">
        class App extends React.Component{
            constructor(props){
                super(props)
                this.state={num:0,arr:[
                    "img/about1.png",
                    "img/about2.png",
                    "img/about3.png",
                ]}
                this.fun=this.fun.bind(this)
                
            }
            fun(){
               if(this.state.num>this.state.arr.length-1){
                this.state.num=0
               }
               else {
                this.setState({num:this.state.num+1})
               }
            }
            componentWillUnmount(){
                clearInterval(this.th)
            }
            componentWillMount(){
                this.th=setInterval(()=>{
                    this.fun()
                },1000)
            }
            render(){
                var da=[]
                for(var i=0;i<this.state.arr.length;i++){
                    var v=<div key={i} className={this.state.num==i?'text':''}><img src={this.state.arr[i]} /></div>
                    da.push(v)
                }
                return (
                    <div className="box">
                       {da}
                    </div>
                )
            }
        }
        ReactDOM.render(
            (<div>
                <App />
            </div>),
            document.getElementById("app")
        )
    </script>
</body>

展示组件(Presentational component)和容器组件(Container component)之间有何不同

  • 展示组件关心组件看起来是什么。展示专门通过 props 接受数据和回调,并且几乎不会有自身的状态,但当展示组件拥有自身的状态时,通常也只关心 UI 状态而不是数据的状态。
  • 容器组件则更关心组件是如何运作的。容器组件会为展示组件或者其它容器组件提供数据和行为(behavior),它们会调用 Flux actions,并将其作为回调提供给展示组件。容器组件经常是有状态的,因为它们是(其它组件的)数据源。

类组件(Class component)和函数式组件(Functional component)之间有何不同

  • 类组件不仅允许你使用更多额外的功能,如组件自身的状态和生命周期钩子,也能使组件直接访问 store 并维持状态
  • 当组件仅是接收 props,并将组件自身渲染到页面时,该组件就是一个 '无状态组件(stateless component)',可以使用一个纯函数来创建这样的组件。这种组件也被称为哑组件(dumb components)或展示组件
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值