用cra脚手架运行下面的demo:
src/App.js
import React, {useState, useEffect} from "react"import {InfiniteScroller} from "react-iscroller";import axios from "axios"; function App() { const [articles, setArticles] = useState([]); useEffect(() => { getArticleList().then(res => { setArticles(res.data.article_list); console.log(articles); //控制台会一直打印这一行 }); }); const getArticleList = params => { return axios.get('/api/articles', params).then(res => { return res.data }, err => { return Promise.reject(err); }).catch((error) => { return Promise.reject(error); }); }; let renderCell = (item, index) => { return ( <li key={index} style={{listStyle: "none"}}> <div> <span style={{color: "red"}}>{index}</span> {item.content} </div> {item.image ? <img src={item.image}/> : null} </li> ); }; let onEnd = () => { //... }; return ( <InfiniteScroller itemAverageHeight={66} containerHeight={window.innerHeight} items={articles} itemKey="id" onRenderCell={renderCell} onEnd={onEnd} /> ); }export default App;
控制台会一直打印useEffect中的console.log(articles),是怎么回事?
当年话下