Open In App

Difference between Flow and PropTypes in ReactJS

Flow is a Javascript extension to type-check your application while propTypes is the built-in type-checking ability of ReactJS. Their functionality may seem similar with a few differences. 

Table of Content



Flow:

Flow is a static type-checking tool for your application. When bigger-scale applications need faster and more productive codes, Flow comes to the rescue. In other words, Flow gives the power to the developer to decide how the developer wants the code to work in any browser and Flow will make sure it does that way.

Example: In this example, we will use flow.






// @flow
function concat(a: string, b: string, c: string) {
    return a + b + c;
}
 
console.log(concat("Geeks", "For", "Geeks"));
console.log(concat(1, 2, 3));

Output:

GeeksForGeeks
//throws Error Because it is of different type

Explanation: We cannot see the numbers merge because we have defined a, b, c of datatype String.

Advantages of using Flow:

Disadvantages of using Flow:

PropTypes:

Like Flow is an extension, PropType is an inbuilt type checker in react. Anything other than the type of prop being passed cannot be checked by it. So it is basically a run-time type checking mechanism.

Note: Before the release of React 15.5.0, PropTypes were available in the React package, but now we have to add the prop-types library in our project.

Example: In this example, we define the prototype as string and import prototypes to type check the code.




import PropTypes from 'prop-types';
 
class Concat extends React.Component {
    static defaultProps = {
        name: 'GeeksForGeeks',
    }
     
    render() {
        return (
            <h1>Hi, {this.props.name}</h1>
        );
    }
}
 
Concat.propTypes = {
    name: PropTypes.string
};

Output:

Output

Advantages of PropTypes:

Disadvantages of PropTypes:

Flow

PropTypes

Flow Works at compile time PropTypes catches errors at run time
Flow is a type checker Javascript extension. PropType is a library.
It is a flexible typechecker, as long as you are only passing annotated types into components, defining props is not a compulsion. It is very strict and can only check types of the values passed as props.

Article Tags :