202105笔记 -(有无状态组件、纯函数)

无状态组件

import React from 'react';
import PropTypes from 'prop-types';

const LearnCom = (props) => {
    return (
        <div>{props.desc}</div>
    )
}

LearnCom.propTypes  = {
    desc: PropTypes.string
}
export default LearnCom

理解:

 组件数据通过父级传入,组件不做数据处理

有状态组件

import React from 'react'
import PropTypes from 'prop-types';
class HelloWord extends React.Component {
    constructor(props){
        super(props);
    }

    render(){
        return (<div>
            { this.props.desc }
        </div>)
    }
}
HelloWord.protoTypes = {
    desc : PropTypes.string
}
export default HelloWord

理解

 组件各自拥有自己数据处理

纯函数

// 是 
const foo = (obj, b) => {
  return obj.x + b
}
// 不是
const foo = (obj, b) => {
    console.log(b)
}
// 不是
const foo = (obj, b) => {
	obj.x =2
  return obj.x + b
}

理解

 一个函数是纯函数条件:
 1. 有返回值
 2. 函数参数不能被修改
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

今天也想MK代码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值