zoukankan      html  css  js  c++  java
  • React的条件渲染和列表渲染

    React事件

    特点:
    1/react事件,绑定事件的命名,驼峰命名法。
    2/{},传入1个函数,而不是字符串

    <button onClick={this.sendData}>传递helloworld给父元素</button>

    事件对象:React返回的事件对象是代理的原生的事件对象,如果想要查看事件对象的具体值,必须之间输出事件对象的属性。

    注意:
    原生,阻止默认行为时,可以直接返回return false;
    React中,阻止默认必须e.preventDefault();

    React事件传参数

    {/* 使用ES6箭头函数传递多个参数 */}
    <button  onClick={(e)=>{this.parentEvent1('msg:helloworld',e)}}>提交</button>
    {/* //不使用ES6箭头函数传递多个参数的方式 */}
    <button  onClick={function(e){this.parentEvent1('不使用es6,msg:helloworld',e)}.bind(this)}>提交</button>

     

     

    条件渲染

    React中条件渲染即和JavaScript中,条件运算,如if...else...三元运算符等。

    1/直接通过条件运算返回要渲染的JSX对象
    2/通过条件运算得出jsx对象,在将JSX对象渲染到模板中。

    案例一

    //条件渲染一
    function Login() {
        return (
            <div>
                <h1>欢迎登录</h1>
            </div>
        )
    }
    
    function LoginNeed() {
        return (
            <div>
                <h1>请先登录</h1>
            </div>
        )
    }
    
    
    class HelloWorld extends React.Component{
        constructor(props) {
            super(props)
            this.state = {
                isLogin:false
            }
        }
        render() {
            if(this.state.isLogin){
                return(
                    <Login />
                )
            }else{
                return (
                    <LoginNeed />
                )
            }
        }
    }
    
    
    ReactDOM.render(
        <HelloWorld />,
        document.querySelector('#root')
    )

    案例二

    function Login() {
        return (
            <div>
                <h1>欢迎登录</h1>
            </div>
        )
    }
    
    function LoginNeed() {
        return (
            <div>
                <h1>请先登录</h1>
            </div>
        )
    }
    
    
    class HelloWorld extends React.Component{
        constructor(props) {
            super(props)
            this.state = {
                isLogin:true
            }
        }
        render() {
            let element = null
            if(this.state.isLogin){
                element = <Login />
            }else{
                element = <LoginNeed />
            }
            return (
                <div>
                    {element}
                </div>
            )
        }
    }
    
    ReactDOM.render(
        <HelloWorld />,
        document.querySelector('#root')
    )

    案例一直接根据判断条件进行return,这样耦合性太强,而且很难增加新功能,

    使用案例二这种将函数赋值给元素,大大提升了代码的复用性,而且可拓展能力加强

    列表渲染

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

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

    Key值需要放置到每一项中。

    简单案例

    class HelloWorld extends React.Component{
        constructor(props) {
            super(props)
            this.state = {
                list:[
                    {
                        title:"第一节 React事件",
                        content:"事件内容"
                    },
                    {
                        title:"第二节 React数据传递",
                        content:"数据传递内容",
                    },
                    {
                        title:"第三节 条件渲染",
                        content:"条件渲染内容",
                    }
                ]
            }
        }
    
        render() {
            let ArrList = []
            for(let i=0;i<this.state.list.length;i++){
                let item = (
                    <li>
                        <h1>{this.state.list[i].title}</h1>
                        <p>{this.state.list[i].content}</p>
                    </li>
                )
                ArrList.push(item)
            }
            return (
                <div>
                    <ul>
                        {ArrList}
                    </ul>
                </div>
            )
        }
    }
    
    ReactDOM.render(
        <HelloWorld />,
        document.querySelector('#root')
    )

    解耦性更高的方案二

    function ListItem(props){
        return (
            <div>
                <h1>{props.item.title}</h1>
                <p>{props.item.content}</p>
            </div>
        )
    }
    
    
    class HelloWorld extends React.Component{
        constructor(props) {
            super(props)
            this.state = {
                list:[
                    {
                        title:"第一节 React事件",
                        content:"事件内容"
                    },
                    {
                        title:"第二节 React数据传递",
                        content:"数据传递内容",
                    },
                    {
                        title:"第三节 条件渲染",
                        content:"条件渲染内容",
                    }
                ]
            }
        }
        render() {
            let Arr = this.state.list.map((item)=>{
                return(
                    <ListItem item={item} />
                )
            })
            return (
                <div>
                    {Arr}
                </div>
            )
        }
    }
    
    
    ReactDOM.render(
        <HelloWorld />,
        document.querySelector('#root')
    )
  • 相关阅读:
    BPM已经全面开启
    终于理解集线器、交换机、路由器之间的区别了
    Linux系统桥接模式下配置静态ip地址
    理解并手写Spring MVC框架
    Java 8 Funciton、Consumer、Predicate的作用及用法
    你想了解的分布式文件系统HDFS,看这一篇就够了
    分布式处理框架Hadoop的安装与使用
    CentOS7 中常用命令
    VMware安装Centos7并联网使用
    理解Spring AOP的实现方式与思想
  • 原文地址:https://www.cnblogs.com/asdaa/p/12393001.html
Copyright © 2011-2022 走看看