【问题标题】:React Factory Function反应工厂函数
【发布时间】:2020-08-23 12:24:27
【问题描述】:

我在网上找到了这段代码,但是,我是 React 的初学者,我看不懂。这段代码有什么作用?它会克隆组件吗?

对于这个非常基本的问题,我感到非常抱歉。非常感谢您的帮助!

const Factory = function ({
  React,
  thing,
  isActive,
  addThing,
  deleteThing,
  name,
  description,
} = {}) {

  const Thing = function (props) {
    return {
      propTypes: {
        thing: React.PropTypes.object,
        isActive: React.PropTypes.bool,
        addThing: React.PropTypes.func,
        deleteThing: React.PropTypes.func,
        name: React.PropType.string,
        description: React.PropType.string,
      },

      props,

      render () {

        return (
          <Card>
          <Card.Body>
          <Card.Title>{ this.props.name }</Card.Title>
          <Card.Text>{ this.props.description }</Card.Text>
          </Card.Body>
          </Card>
        );
      }
    };
  };

  return Thing;

};

export default Factory;

【问题讨论】:

  • 你能分享这篇文章的链接吗

标签: reactjs frontend


【解决方案1】:

propTypes 用于验证。

这里的Factory 是一个函数组件,它将接收以下参数

React,thing,isActive,addThing,deleteThing,name,description

然后您正在验证使用propTypes 传递的每个参数的数据类型 像 isActive 应该是布尔值。如果不满足会抛出错误。

然后它会渲染一个带有名称和描述的 JSX 元素,并返回 Thing 的引用

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-11
    • 1970-01-01
    • 2012-01-31
    • 2020-09-01
    • 2020-05-22
    • 2013-11-14
    相关资源
    最近更新 更多