react循環數據(列表)的實現

首先我們模擬一下後臺傳過來的數據,這裡為瞭讓代碼清晰,數據就簡單的模擬瞭一下

   import bg3 from './image/bg3.png'
   constructor(props){
      super(props)
      this.state = {
      ///這裡
         list:[
            { id:1,img:bg3},
            { id:2,img:bg3},
            { id:3,img:bg3},
            { id:4,img:bg3},
            { id:5,img:bg3},
         ], 
     ///     
      }
   }

然後在使用map方法循環出來

  {
        this.state.list.map((item,key) => {
            return (
              <div className="winfor" onClick={() => console.log(item.id)}>
                 <img src={item.img} className="winforimg" />
                    
                 
              </div>
            )
          })
  }

補充:React 循環列表

import React, {Component} from 'react';
import logo from './logo.svg';
import './App.css';
import {Person,Twoway} from './Person/Person'
 
class  App extends Component{
  state={
    persons:[{name:'小仙女',age:'18'},{name:'小玉',age:'18'},{name:'傢璇',age:'18'}],
 
  }
 
  render(){
    const listItems = this.state.persons.map((item,index) =>
       <Person name={item.name} age={item.age} key={index} />
     );
      return(
        <div className="App">
             {listItems}
        </div>
      )      
  } 
} 
export default App;

到此這篇關於react循環數據的實現的文章就介紹到這瞭,更多相關react循環數據內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀:

    None Found