【问题标题】:How to dynamically populate bootstrap grids using reactjs?如何使用 reactjs 动态填充引导网格?
【发布时间】:2018-07-03 05:46:39
【问题描述】:

我正在尝试使用引导网格显示匹配统计信息框。总共 16 个网格 4X4,即每行包含 4 列。除了创建 16 个匹配统计框之外,还有什么方法可以使用 ReactJS 动态填充数据。

在 app.js 中:

import React, { Component } from 'react';
import './App.css';
import Navbar from './components/navbar';
import Content from './components/content';
import Pagination from './components/pagination';

class App extends Component {
  render() {
    return (
      <div className="App">
        <Navbar/>
        <Content/>
        <Pagination/>
      </div>
    );
  }
}

export default App;

在 Content.js 中:

import React, { Component } from 'react';
import './content.css';

class Content extends Component {
    constructor(props){
        super(props);
        this.state = {
            matches:[],
            loading:true
        };
    }

    componentDidMount(){
        fetch('api/matches')
        .then(res => res.json())
        .then(res => {
      console.log(res)
      this.setState({
        matches:res,
        loading:false
      })
    })
    }

    render() {
        if (this.state.loading) {
            return <div>>Loading...</div>
        }

    return (
      <div>
          <div class="row">

            <div class="col-lg-3">
                <div id="one">
                    <p class="match">Match {this.state.matches[0].id}</p>
                    <p><h4>{this.state.matches[0].team1}</h4></p>
                    <p>VS</p>
                    <p><h4>{this.state.matches[0].team2}</h4></p>
                    <div class="winner">
                        <p><h3>Winner</h3></p>
                        <p><h4>{this.state.matches[0].winner}</h4></p>
                    </div>
                    <div class="stats">
                    <button type="button" class="btn btn-primary">View Stats</button>
                    </div>
                </div>
            </div>
          </div>
      </div>
    );
  }
}

export default Content;

在 content.js 中只有 1 个匹配统计框,而不是硬编码 16 个框,如何防止渲染函数中出现代码冗余代码。

我的网络应用的当前视图:

【问题讨论】:

    标签: javascript json twitter-bootstrap reactjs


    【解决方案1】:

    您可以使用地图功能。请检查以下代码:

    import React, { Component } from 'react';
    import './content.css';
    
    class Content extends Component {
        constructor(props) {
            super(props);
            this.state = {
                matches: [],
                loading: true
            };
        }
    
        componentDidMount() {
            fetch('api/matches')
                .then(res => res.json())
                .then(res => {
                    console.log(res)
                    this.setState({
                        matches: res,
                        loading: false
                    })
                })
        }
    
        renderMatches() {
            return this.state.matches.map(match => {
                return (
                    <div class="col-lg-3">
                        <div id="one">
                            <p class="match">Match {match.id}</p>
                            <p><h4>{match.team1}</h4></p>
                            <p>VS</p>
                            <p><h4>{match.team2}</h4></p>
                            <div class="winner">
                                <p><h3>Winner</h3></p>
                                <p><h4>{match.winner}</h4></p>
                            </div>
                            <div class="stats">
                                <button type="button" class="btn btn-primary">View Stats</button>
                            </div>
                        </div>
                    </div>
                );
            })
        }
    
        render() {
            if (this.state.loading) {
                return <div>>Loading...</div>
            }
    
            return (
                <div>
                    <div class="row">
                        {this.renderMatches()}
                    </div>
                </div>
            );
        }
    }
    
    export default Content;
    

    【讨论】:

    • 我想显示 16 个框,即 4X4,所以我需要写 {this.renderMatches()} 16 次。正如您在我的代码中看到的那样,列大小为 3,最多应添加 12 个,即 4 个框。
    • {this.renderMatches()} 将使用类 col-lg-3 创建 16 个 div,因为我们正在使用 map 函数进行循环。
    • 总共返回 577 个 JSON 对象(matches[0],matches[1]....matches[576]) 那么map 什么时候停止。
    • 传递给map函数的第二个参数是index。所以我们可以有一个逻辑来检查页码。和当前索引以显示当前页面的 16 个匹配项。
    • 我没听懂你说的。映射函数的第二个参数是什么?我也无法理解这一行 -> So we can have a logic where we check the page no. and current index to show the 16 matches for the current page
    【解决方案2】:

    您可以将代码拆分为单独的 TableRowCell 组件。这样您就可以将数据添加到Table,它可以将适当的数据提供给行,而行可以将适当的数据提供给单元格。

    这里有一个粗略的例子来帮助你入门:

    function Table({data}) {
      return (
        <table>
          {data.map(row => <Row data={row} />)}
        </table>
      )
    }
    
    function Row({data}) {
      return (
        <tr>
          {data.map(cell => <Cell data={cell} />)}
        </tr>
      )
    }
    
    function Cell({data}) {
      return (
        <td>{data}</td>
      )
    }
    
    const data = [
      [1, 2, 3, 4],
      [5, 6, 7, 8],
      [9, 10, 11, 12]
    ];
    
    ReactDOM.render(
      <Table data={data} />,
      document.getElementById('container')
    );
    table {
      border-collapse: collapse;
      border: 1px solid #454545;
    }
    
    td {
      padding: 5px;
      border: 1px solid #dfdfdf;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
    <div id="container"></div>

    【讨论】:

    • 谢谢。所以通过这种方式我可以填充我的引导网格,因为引导网格的总列数是 12?
    • 当然,我想你可以为所欲为。我认为最好的事情首先是确保数据的格式适合项目。因此,如果它只是一个元素数组,可能会考虑更改它,因此它是一个长度为 n 列的数组数组,就像我在示例中那样。这样可以更轻松地构建组件。
    • 我要填充的数据是数组,请看截图:imgur.com/a/puaAj 是适合我的 JSON 数据的方法。
    • 这是一个对象数组。所以我假设您希望每个对象的信息在表中作为一行?如果您将该数据添加到 jsfiddle 我可以看看。
    • 然后只需arr.slice(0, 16) 要显示的数组部分并将其作为数据传递。至于其余的,我希望您可以根据答案中的信息来解决。
    猜你喜欢
    • 2020-09-16
    • 2019-04-15
    • 2017-11-29
    • 1970-01-01
    • 2018-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-23
    相关资源
    最近更新 更多