Shobhit Singh
Shobhit Singh

Reputation: 86

does preact have something similar to react proptypes?

just like prop types, is there anything similar in preact

similiar to this:

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 

Upvotes: 3

Views: 2086

Answers (1)

wp78de
wp78de

Reputation: 18980

You should be able to use PropTypes using preact-compact, the React compatibility layer for Preact:

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

With Webpack or Browserify aliases in place, existing React modules should work nicely:

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 question also describes an undocumented hook function which can be used to check PropTypes on arbitrary class methods.

Upvotes: 2

Related Questions