React-props

1.props和state的区别,state是内部的,内部的操作触发setState方法;props类似于Java的构造器传过来的参数,一般是外界传入的数据。

2.例子:主view由于点击事件,触发state中的数据变动,该state的数据又是子view的props中的数据

import React from 'react'

class MyOwnPage extends React.Component {

    constructor(props) {
        super(props);
        this.state = {datas: [], data: '', index: 0};
        this.clickButton = this.clickButton.bind(this);

    }

    render() {
        return (
            <div>
                <h1>已插入信息</h1>
                <DetailList datas={this.state.datas}/>
                <input id={'input'}></input>
                <button onClick={this.clickButton}>添加</button>

            </div>


        );
    }

//点击按钮的方法
    clickButton() {
        console.log("点击了提交" + this.state.data);


        this.setState((prevState) => (
                {data: document.getElementById('input').value})
            , function () {
                console.log("设置的数据完毕");
                var bean = {
                    value: this.state.data,
                    id: this.state.index
                };
                this.setState((prevState) => ({
                    datas: prevState.datas.concat(bean)
                }), function () {
                    console.log("数据长度" + this.state.datas.length);
                    this.setState((preState) => (
                        {index: preState.index + 1}
                    ));
                });
            });


    }
}

export default MyOwnPage;

//类似于自定义view====这里使用了props
class DetailList extends React.Component {

    render() {
        console.log("传递过来的数据长度" + this.props.datas.length);
        return (
            <ul>
                {this.props.datas.map(function (bean) {
                    console.log("这个的数据" + bean.id + bean.value);
                    return <li key={bean.id}>{bean.value}</li>;
                })}
                {/*es6的forEach*/}
                {/*{*/}
                {/*this.props.datas.map(bean => (*/}
                {/*<li key={bean.id}>{bean.value}</li>*/}
                {/*))*/}
                {/*}*/}
            </ul>
        );
    }

    test() {
        /*//遍历使用forEach*/

        {
            this.props.datas.forEach((bean) => (
                <li key={bean.id}>{bean.value}</li>))
        }

        {/*//遍历使用map*/
        }
        {
            this.props.datas.map(bean => (
                <li key={bean.id}>{bean.value}</li>
            ))
        }


//ES5的each
        {
            this.props.datas.forEach(function (bean) {
                console.log("数据如下:" + bean.key + bean.value);
                return <li key={bean.id}>{bean.value}</li>;
            })
        }
        //ES5的map
        {
            this.props.datas.map(function (bean) {
                return <li key={bean.key}>{bean.value}</li>;
            })
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值