How to loop and render elements in React.js without an array of objects to map?

前端 未结 5 1684
轻奢々
轻奢々 2020-11-28 01:35

I\'m trying to convert a jQuery component to React.js and one of the things I\'m having difficulty with is rendering n number of elements based on a for loop.

I un

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-28 01:48

    Here is more functional example with some ES6 features:

    'use strict';
    
    const React = require('react');
    
    function renderArticles(articles) {
        if (articles.length > 0) {      
            return articles.map((article, index) => (
                
    )); } else return []; } const Article = ({article}) => { return ( ); }; const Articles = React.createClass({ render() { const articles = renderArticles(this.props.articles); return (
    { articles }
    ); } }); module.exports = Articles;

提交回复
热议问题