React Factory Function

Viewed 229

I found this code online, however, I am a beginner to React and I cannot understand it. What does this code do? Does it clone a component?

I'm very sorry for the very basic question. Help would be insanely appreciated!

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;
1 Answers

propTypes is used for validation.

Here Factory is a functional component which will receive below arguments

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

and then you are validating the data type of each of the argument passed using propTypes Like isActive should be boolean. If not satisfied it will throw an error.

Then it renders an JSX element with name and description and also returns a reference of Thing

Related