gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 15:59:45 26 4
gpt4 key购买 nike

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

关于reactjs - preact 有类似于 react proptypes 的东西吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50828624/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com