轮播图:
- <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)之间有何不同
类组件(Class component)和函数式组件(Functional component)之间有何不同