MENU

React学习笔记【9】

第十一节

列表渲染

将列表的内容拼装成数组放置到模板当中
将数据拼装成JSX数组对象

使用数组的map方法,对每一项数据按照JSX的形式进行加工
最终得到一个每一项都是JSX对象的数组,再将数组渲染到模板中

key值需要放置到每一项中


本节代码

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

//拼接
/*
const arr = ["Alice", "Bob", "Cherry"]

const arrHtml = [<li>Alice</li>, <li>Bob</li>, <li>Cherry</li>]
class Welcome extends React.Component {
    constructor(props) {
        super(props);
        console.log("constructor");
    }
    render() {
        return (
            <div>
                <ul>
                    {arr}
                    <hr />
                    {arrHtml}
                </ul>
            </div>
        )
    }
}

ReactDOM.render(
    <Welcome />
    ,
    document.querySelector("#root")
)
*/

//列表渲染 第一种
/*
class Welcome extends React.Component {
    constructor(props) {
        super(props)
        this.state = {
            list: [
                {
                    title: "第一节 React 事件",
                    content: "事件的内容"
                },
                {
                    title: "第二节 React 数据传递",
                    content: "数据传递相关内容"
                },
                {
                    title: "第三节 条件渲染",
                    content: "条件渲染相关内容"
                }
            ]
        }
    }
    render() {
        let listArr = [];
        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)
            console.log(listArr);
        }
        return (
            <div>
                <h1>今日课程内容</h1>
                <ul>
                    {listArr}
                </ul>
            </div>
        )
    }
}


ReactDOM.render(<Welcome />, document.querySelector("#root"));
*/

function ListItem(props) {
    return (
        <li/* key={props.index}*/>
            <h3>{props.data.title}</h3>
            <p>{props.data.content}</p>
        </li>
    )
}

//有交互的用class
class ListItem2 extends React.Component {
    constructor(props) {
        super(props)
    }
    render() {
        return (
            <li onClick={
                (e) => {
                    this.clickEvent(this.props.index, this.props.data.title, e)
                }
            }>
                <h3>{this.props.data.title}</h3>
                <p>{this.props.data.content}</p>
            </li>
        )
    }
    clickEvent = (index, title, event) => {
        alert(index + " - " + title)
    }

}


class Welcome extends React.Component {
    constructor(props) {
        super(props)
        this.state = {
            list: [
                {
                    title: "第一节 React 事件",
                    content: "事件的内容"
                },
                {
                    title: "第二节 React 数据传递",
                    content: "数据传递相关内容"
                },
                {
                    title: "第三节 条件渲染",
                    content: "条件渲染相关内容"
                }
            ]
        }
    }
    clickFn = (index, title, event) => {
        alert("clickFn:" + (index + 1) + " - " + title)
    }
    render() {
        let listArr = this.state.list.map((item, index) => {

            return (
                // <ListItem key={index} data={item} index={index}></ListItem>
                <ListItem2 key={index} data={item} index={index}></ListItem2>

            )
        })
        return (
            <div>
                <h1>今日课程内容</h1>
                <ul>
                    {listArr}
                </ul>
                <h1>不用组件完成的列表</h1>
                <ul>
                    {
                        this.state.list.map((item, index) => {
                            return (
                                <li key={index} onClick={(e) => { this.clickFn(index, item.title, e) }}>
                                    <h3>{item.title}</h3>
                                    <p>{item.content}</p>
                                </li>
                            )
                        })
                    }
                </ul>
            </div>
        )
    }
}

ReactDOM.render(<Welcome />, document.querySelector("#root"));

文章标题:React学习笔记【9】
如果文中内容侵犯了您的权益,请及时与博主取得联系进行删除!
本站文章未经许可禁止转载,本文地址:https://blog.wanvale.com/archives/244/

Archives QR Code Tip
QR Code for this page
Tipping QR Code