Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of export interface Props in React (Typescript ver)

In the Creating a component section of React Typescript Starter example, Creating a component, there is a basic React component in Typescript:

// src/components/Hello.tsx

import * as React from 'react';

export interface Props {
  name: string;
  enthusiasmLevel?: number;
}

function Hello({ name, enthusiasmLevel = 1 }: Props) {
  if (enthusiasmLevel <= 0) {
    throw new Error('You could be a little more enthusiastic. :D');
  }

  return (
    <div className="hello">
      <div className="greeting">
        Hello {name + getExclamationMarks(enthusiasmLevel)}
      </div>
    </div>
  );
}

export default Hello;

// helpers

function getExclamationMarks(numChars: number) {
  return Array(numChars + 1).join('!');
}

I am new to Typescript. It seems that the interface Props is used by Typescript to do props type checks (similar to what the Proptypes npm package does). So the question is:

  1. If I am already using this kind of Typescript interface syntax do to props type check, do I still need to use Proptypes package like this in the same component?
    import PropTypes from 'prop-types';

    Hello.propTypes = {
      name: PropTypes.string,
      enthusiasmLevel: PropTypes.number
    };
  1. Besides, why does here use export interface? what is the purpose of exporting the interface Props? Is it compulsory?
like image 913
Jonathan Avatar asked Mar 19 '19 08:03

Jonathan


People also ask

What does export interface mean in TypeScript?

TypeScript supports export = to model the traditional CommonJS and AMD workflow. The export = syntax specifies a single object that is exported from the module. This can be a class, interface, namespace, function, or enum.

Why do we need export in React?

In React, Vue and many other frameworks the Export is mostly used to export reusable components to make modular based applications.

What is the purpose of props in React?

What are props in React? We use props in React to pass data from one component to another (from a parent component to a child component(s)). Props is just a shorter way of saying properties. They are useful when you want the flow of data in your app to be dynamic.

WHAT IS interface in TypeScript React?

Interface is a structure that defines the contract in your application. It defines the syntax for classes to follow. Classes that are derived from an interface must follow the structure provided by their interface. The TypeScript compiler does not convert interface to JavaScript.


1 Answers

Firstly I recommend declaring your components the ES6 way

const Hello: React.FC<IHello> = ({ name, enthusiasmLevel = 1 }) => {}

Your interface defines the contract of your component / The accepted parameters

export interface IHello {
  name: string;
  enthusiasmLevel?: number;
}

You are exporting this, so you can import your interface from other files / components which want to make use of the Hello component. For example you can use your Hello component like so from another component:

const props: IHello = {
    name: "John",
    enthusiamsLevel: 5
}

<Hello {...props} />

If I am already using this kind of Typescript interface syntax do to props type check, do I still need to use Proptypes in the same component?

You always want type strong definitions in TypeScript. So when declaring your prop variable in another component, you don't want to do const props: any = { If you decide to change your interface declaration for this component later on, you would be forced to update all your references which uses this interface. - You might want to require 1 more prop variable and in that case you would want to update your usages of this interface. If you are not used to TypeScript this can seem quite hideous at first - but the benefit of always having strong type definitions will show over time. Especially when you update your type definitions.

like image 161
Jonas Praem Avatar answered Nov 06 '22 13:11

Jonas Praem