Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ReactJS properties validation

I'm moving from ReactJs to React-Native and found this function structure in facebook's code for a react-native button:

class Button extends React.Component<{
  title: string,
  onPress: () => any,
  color?: ?string,
  hasTVPreferredFocus?: ?boolean,
  accessibilityLabel?: ?string,
  disabled?: ?boolean,
  testID?: ?string,
}> {
  static propTypes = {
    /**
     * Text to display inside the button
     */
    title: PropTypes.string.isRequired,
    /**
     * Text to display for blindness accessibility features
     */
    accessibilityLabel: PropTypes.string,
    /**
     * Color of the text (iOS), or background color of the button (Android)
     */
    color: ColorPropType,
    /**
     * If true, disable all interactions for this component.
     */
    disabled: PropTypes.bool,
    /**
     * TV preferred focus (see documentation for the View component).
     */
    hasTVPreferredFocus: PropTypes.bool,
    /**
     * Handler to be called when the user taps the button
     */
    onPress: PropTypes.func.isRequired,
    /**
     * Used to locate this view in end-to-end tests.
     */
    testID: PropTypes.string,
  };

  ...
}

In ReactJS I used just to build my components with propTypes checking, so:

a) What is the purpose of having the props specification inside the brackets at the class definition (<{...}> ? Is this available also in normal ReactJS?

b) Will that check the passed properties format? If so, why do I need propTypes here?

like image 369
Mendes Avatar asked Jun 02 '18 15:06

Mendes


People also ask

What are the property validation in React?

React JS has an inbuilt feature for validating props data type to make sure that values passed through props are valid. React components have a property called propTypes which is used to setup data type validation. Syntax: The syntax to use propTypes is shown below. class Component extends React.

Is there any props validation in React?

Props are an important mechanism for passing the read-only attributes to React components. The props are usually required to use correctly in the component. If it is not used correctly, the components may not behave as expected. Hence, it is required to use props validation in improving react components.


1 Answers

For clarity's sake, the proper term for the definition inside the brackets is called a generic, which is like a type argument when the function/class/whatever is unsure what the type of something is, so it lets you fill in the blanks - in this case, the type for the component's props.

Particularly, this syntax looks like Flow, but TypeScript is also a popular option for type checking.

So:

  • The generic is used to type check using your component at compile time. This is syntax specific to type checkers, and isn't available in normal JS.
  • propTypes is used to check the types during runtime, to improve DX for people who aren't using a type system.

Generally, projects with type systems only opt to use the generic to reduce verbosity, If you're using a type system, propTypes is only really needed if you're releasing the component to the public.

like image 89
kingdaro Avatar answered Sep 21 '22 17:09

kingdaro