【问题标题】:does preact have something similar to react proptypes?preact 有类似于 react proptypes 的东西吗?
【发布时间】:2018-11-22 12:39:50
【问题描述】:

就像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 

【问题讨论】:

    标签: reactjs preact


    【解决方案1】:

    您应该能够使用 preact-compact,即 Preact 的 React 兼容层 use PropTypes

    preact-compat 完全支持 PropType,或者您可以手动使用它们。

    使用 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 问题还描述了一个未记录的钩子函数,可用于检查任意类方法上的 PropTypes。

    【讨论】:

      猜你喜欢
      • 2016-03-16
      • 1970-01-01
      • 2017-07-15
      • 2014-09-22
      • 2010-11-29
      • 2020-03-26
      • 2011-10-18
      • 2023-03-09
      • 2011-04-04
      相关资源
      最近更新 更多