preact 有类似于 react proptypes 的东西吗?

does preact have something similar to react proptypes?

就像prop类型一样,preact有没有类似的东西

类似于此:

const Component = React.createClass({
  propTypes: {
    name: React.PropTypes.string //here we define expected type of the prop
  },
  // ...
})

<Component name="Ari" /> // a component having prop name 

您应该能够 use PropTypes using preact-compact,Preact 的 React 兼容层:

PropTypes are fully supported in preact-compat, or you can use them manually.

有了 Webpack 或 Browserify 别名,现有的 React 模块应该可以很好地工作:

import React, { Component } from 'react';
import { render } from 'react-dom';

class Foo extends Component {
    propTypes = {
        a: React.PropTypes.string.isRequired
    };
    render() {
        let { a, b, children } = this.props;
        return <div {...{a,b}}>{ children }</div>;
    }
}

render((
    <Foo a="a">test</Foo>
), document.body);

This GitHub 问题还描述了一个未记录的钩子函数,可用于检查任意 class 方法上的 PropTypes。