javascript - react 使用ES6的写法,PropTypes为何不生效?

查看:100
本文介绍了javascript - react 使用ES6的写法,PropTypes为何不生效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

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

let data = 123;

class MyTitle extends React.Component {

    //static propTypes = {
    //    title: React.PropTypes.string.isRequired
    //};//用这种方式则报错

    render() {
        return <h1> {this.props.title} </h1>;
    }
}

MyTitle.propTypes = {
    title: React.PropTypes.string.isRequired
};//验证不生效

ReactDOM.render(
    <MyTitle title={data}/>,
    document.getElementById('example')
);

解决方案

类属性不是ES6的提案,需要babel支持

http://babeljs.io/docs/plugins/transform-class-properties/

这篇关于javascript - react 使用ES6的写法,PropTypes为何不生效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆