react你要知道的

函数式无状态组件

const MyComponent=(props)=(<div>hello {props.name}</div>)//无状态组件的props

 控制传入的属性

class HelloMessage extends React.Component {
    // 若是需要绑定 this.方法或是需要在 constructor 使用 props,定义 state,就需要 constructor。若是在其他方法(如 render)使用 this.props 则不用一定要定义 constructor
    constructor(props) {
        super(props);//构造函数使用props时用,否则可移除,有constructor必有super否则无this
        this.state = {}
     this.函数=this.函数.bind(this)//自己绑定this的指向
}
render() { return ( <div>Hello {this.props.name}</div> ) } } //验证类型 HelloMessage.propTypes = { name: React.PropTypes.string, } //预设默认值 HelloMessage.defaultProps = { name: 'Zuck', }
//设定默认值(2)
static defaultProps = {
    checked: false,
    maxLength: 10, }; // 注意有分号
// 验证类型(2) static propTypes = { checked: React.PropTypes.bool.isRequired, maxLength: React.PropTypes.number.isRequired };

 

转载于:https://www.cnblogs.com/liqunblog/p/9324542.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值