React-列表渲染

React并没有Vue的v-for这种列表渲染方式

使用for进行列表渲染

import React from 'react'
import ReactDOM from 'react-dom'

// 列表渲染
class Test extends React.Component {
    constructor (props) {
        super(props)
        this.state = {
            list: [
                {
                    title: '第一个标题',
                    content: '第一个内容'
                },
                {
                    title: '第二个标题',
                    content: '第二个内容'
                },
                {
                    title: '第三个标题',
                    content: '第三个内容'
                }
            ]
        }
    }
    // 渲染函数
    render () {
        let listArr = []
        // 通过for循环构建列表
        for (let i = 0; i < this.state.list.length; i++) {
            let item = (
                <li>
                    <h3>{this.state.list[i].title}</h3>
                    <p>{this.state.list[i].content}</p>
                </li>
            )
            listArr.push(item)
        }
        return (
            <div>
                <ul>
                    {listArr}
                    <li>
                        <h3>这是标题</h3>
                        <p>这是内容</p>
                    </li>
                </ul>
            </div>
        )
    }
}

ReactDOM.render(
    <Test />,
    document.getElementById('root')
)

在这里插入图片描述
但是这时候需要注意的是,控制台输出一个警告,告诉我们,需要有一个唯一的key
在这里插入图片描述
这时候需要加上key,因为需要时唯一key,所以这里我使用的是for循环中的i

import React from 'react'
import ReactDOM from 'react-dom'

// 列表渲染
class Test extends React.Component {
    constructor (props) {
        super(props)
        this.state = {
            list: [
                {
                    title: '第一个标题',
                    content: '第一个内容'
                },
                {
                    title: '第二个标题',
                    content: '第二个内容'
                },
                {
                    title: '第三个标题',
                    content: '第三个内容'
                }
            ]
        }
    }
    // 渲染函数
    render () {
        let listArr = []
        // 通过for循环构建列表
        for (let i = 0; i < this.state.list.length; i++) {
            let item = (
                <li key={i}>
                    <h3>{this.state.list[i].title}</h3>
                    <p>{this.state.list[i].content}</p>
                </li>
            )
            listArr.push(item)
        }
        return (
            <div>
                <ul>
                    {listArr}
                    <li>
                        <h3>这是标题</h3>
                        <p>这是内容</p>
                    </li>
                </ul>
            </div>
        )
    }
}

ReactDOM.render(
    <Test />,
    document.getElementById('root')
)

在这里插入图片描述
使用map进行列表渲染
需要注意唯一key的问题,这里我是用的是index作为唯一key

import React from 'react'
import ReactDOM from 'react-dom'

// 列表渲染
class Test extends React.Component {
    constructor (props) {
        super(props)
        this.state = {
            list: [
                {
                    title: '第一个标题',
                    content: '第一个内容'
                },
                {
                    title: '第二个标题',
                    content: '第二个内容'
                },
                {
                    title: '第三个标题',
                    content: '第三个内容'
                }
            ]
        }
    }
    // 渲染函数
    render () {
        let listArr = []
        // 使用map进行渲染
        listArr = this.state.list.map((item, index) => {
            return (
                <li key={index}>
                    <h3>{index}--{item.title}</h3>
                    <p>{index}--{item.content}</p>
                </li>
            )
        })
        return (
            <div>
                <ul>
                    {listArr}
                    <li>
                        <h3>这是标题</h3>
                        <p>这是内容</p>
                    </li>
                </ul>
            </div>
        )
    }
}

ReactDOM.render(
    <Test />,
    document.getElementById('root')
)

在这里插入图片描述
使用map和类组件进行渲染
需要注意的是,这时候的key不应该放在li上,而是应该放在ListItem标签上

import React from 'react'
import ReactDOM from 'react-dom'

// 使用类进行列表渲染
class ListItem extends React.Component {
    render () {
        return (
            <li>
                <h3>{this.props.index}--{this.props.data.title}</h3>
                <p>{this.props.index}--{this.props.data.content}</p>
            </li>
        )
    }
}


// 列表渲染
class Test extends React.Component {
    constructor (props) {
        super(props)
        this.state = {
            list: [
                {
                    title: '第一个标题',
                    content: '第一个内容'
                },
                {
                    title: '第二个标题',
                    content: '第二个内容'
                },
                {
                    title: '第三个标题',
                    content: '第三个内容'
                }
            ]
        }
    }
    // 渲染函数
    render () {
        let listArr = []
        // 使用map和类组件进行列表渲染
        listArr = this.state.list.map((item, index) => {
            return (
                <ListItem key={index} data={item} index={index} />
            )
        })


        return (
            <div>
                <ul>
                    {listArr}
                    <li>
                        <h3>这是标题</h3>
                        <p>这是内容</p>
                    </li>
                </ul>
            </div>
        )
    }
}

ReactDOM.render(
    <Test />,
    document.getElementById('root')
)

在这里插入图片描述
直接在render函数中的{}直接列表渲染

import React from 'react'
import ReactDOM from 'react-dom'


// 列表渲染
class Test extends React.Component {
    constructor (props) {
        super(props)
        this.state = {
            list: [
                {
                    title: '第一个标题',
                    content: '第一个内容'
                },
                {
                    title: '第二个标题',
                    content: '第二个内容'
                },
                {
                    title: '第三个标题',
                    content: '第三个内容'
                }
            ]
        }
    }
    // 渲染函数
    render () {
        return (
            <div>
                <ul>
                    {
                        this.state.list.map((item, index) => {
                            return (
                                <li key={index}>
                                    <h3>{index}--{item.title}</h3>
                                    <p>{index}--{item.content}</p>
                                </li>
                            )
                        })
                    }
                    <li>
                        <h3>这是标题</h3>
                        <p>这是内容</p>
                    </li>
                </ul>
            </div>
        )
    }
}

ReactDOM.render(
    <Test />,
    document.getElementById('root')
)

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Antd Table 是 Ant Design 框架中的一个表格组件,而 react-window 则是一个能够帮助 React 应用中高效渲染大量数据的组件。使用 react-window 能够减少因为渲染大量数据而导致页面卡顿的情况,提升用户的使用体验。 如果你希望在 Antd Table 中使用 react-window 的虚拟列表功能,可以先安装 react-window: ``` npm install react-window ``` 然后,在 Antd Table 中使用 react-window 的虚拟列表功能,需要进行如下操作: 1. 引入 react-window 库中的 List 组件 2. 在 columns 配置项中,设置 fixed 属性,以确保表头的列不会被虚拟化 3. 在 dataSource 中,只保留当前可见区域的数据,而不是全部数据。 下面是一个 Antd Table 中使用 react-window 的示例代码: ```jsx import { Table } from 'antd'; import { List } from 'react-window'; const VirtualTable = props => { const { columns, dataSource } = props; const renderRow = ({ index, style }) => { const rowData = dataSource[index]; return ( <div style={style}> <Table.Row key={rowData.key} record={rowData} index={index} columns={columns} /> </div> ); }; return ( <Table {...props} columns={columns.map(column => ({ ...column, fixed: column.fixed || 'left', }))} pagination={false} components={{ body: ({ children, ...restProps }) => { return ( <List height={400} itemCount={dataSource.length} itemSize={54} width={1000} {...restProps} > {renderRow} </List> ); }, }} /> ); }; export default VirtualTable; ``` 在上述代码中,我们将 Antd Table 包装在一个名为 VirtualTable 的组件中,该组件使用 react-window 的 List 组件来渲染表格数据。其中,itemCount 和 itemSize 属性分别设置了数据总数和每一行的高度,height 和 width 属性则分别设置了表格的高度和宽度。在 renderRow 函数中,我们根据当前的 index 和 style 来渲染每一行的数据。最后,我们将 List 组件作为 Antd Table 的 body 组件,以实现虚拟列表的功能。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值