根据反应

时间:2018-04-11 22:45:22

标签: javascript html reactjs meteor collections

在我的流星项目中,我有一个名为拍卖的集合。使用react我希望以无限数量的行呈现此次拍卖的3列。为了实现这一点,我认为可以发送对象的索引,但我不知道如何做到这一点。另一个问题是它显示了html代码的错误,因为我没有关闭' div'标签

这是我的App.js:

            import React, { Component } from 'react';
            import ReactDOM from 'react-dom';
            import { withTracker } from 'meteor/react-meteor-data';

            import { Auctions } from '../api/auctions.js';

            import Auction from './Auction.js';

            //App component - represents the whole app
            class App extends Component {
                renderAuctions() {
                    return this.props.auctions.map((auction, index) => (
                        <Auction key={auction._id} auction={auction} index={index} />
                    ));
                }

                render() {
                    return (
                        <div className="container section">
                            <div className="row">
                                {this.renderAuctions()}
                            </div>
                        </div>
                    );
                }
            }

            export default withTracker(() => {
                return {
                    auctions: Auctions.find({}).fetch(),
                };
            })(App);

我的Auction.js:

            import React, { Component } from 'react';

            //Task component - resepresnts a single todo item

            export default class Auction extends Component {
                render() {
                    if(index % 3 === 0) {
                        return (
                            </div> /* Shows an erros here because of closing tag*/
                            <div className="row">
                                <div className="col s4 ">
                                    <div className="card">
                                        <div className="card-image">
                                            <img src="images/logo.png" />
                                        </div>
                                        <div className="card-content">
                                            <span className="card-title">
                                                {this.props.auction.auctionName}
                                            </span>
                                            <p>
                                                I am a very simple card. I am good at containing small bits of information.
                                                I am convenient because I require little markup to use effectively.
                                            </p>
                                        </div>
                                        <div className="card-action">
                                            <a href="#">This is a link</a>
                                        </div>
                                    </div>
                                </div>
                        );
                    } else {
                        <div className="col s4">
                            <h1>Brincoooo</h1>
                            <div className="card">
                                <div className="card-image">
                                    <img src="images/logo.png" />
                                </div>
                                <div className="card-content">
                                    <span className="card-title">
                                        {this.props.auction.auctionName}
                                    </span>
                                    <p>
                                        I am a very simple card. I am good at containing small bits of information.
                                        I am convenient because I require little markup to use effectively.
                                    </p>
                                </div>
                                <div className="card-action">
                                    <a href="#">This is a link</a>
                                </div>
                            </div>
                        </div>
                    }

                }
            }

1 个答案:

答案 0 :(得分:1)

每次从渲染函数返回HTML时,它都需要自包含且具有平衡标记。这就是React的工作方式,以及它为什么会给你一个错误。

您可以考虑使用flexbox,而不是一次尝试对3个拍卖进行分组。使用flexbox,您只需呈现所有拍卖,并自动为您设置包装。屏幕较宽的用户将看到超过3列,移动用户在纵向模式下可能会看到一列。

如果你想了解flexbox,可以在这里找到一个可爱的教程:https://flexboxfroggy.com/如果你不喜欢那个,有很多教程,例如:https://scotch.io/tutorials/a-visual-guide-to-css3-flexbox-properties < / p>

我允许你从这里开始工作