I'm recently migrate to Typescript-react from javascript-based react. So this is new for me.
I notice that Typescript has its own props validation method using interface
. My questions are:
Here is my simple code. This code below works:
import * as React from "react";
// [Question 1] Create an object PropTypes, is this still necessary ?
import * as PropTypes from "prop-types";
// [Question 2] how to use isRequired for this prop ?
interface Props {
text: String;
active: Boolean;
}
const NavBarMenu = (props: Props) => {
// [Question 3] How to make a default value for prop?
return (
<li className="nav-item">
<a className={props.active ? "nav-link active" : "nav-link"} data-toggle="tab" href="#" role="tab">
{props.text}
</a>
</li>
);
};
NavBarMenu.propTypes = {
text: PropTypes.string.isRequired,
active: PropTypes.bool
};
export default NavBarMenu;
Typescript and PropTypes serve different purposes. Typescript validates types at compile time, whereas PropTypes are checked at runtime. Typescript is useful when you are writing code: it will warn you if you pass an argument of the wrong type to your React components, give you autocomplete for function calls, etc.
PropTypes is deprecated since React 15.5.
Props are required in TypeScript In the prop-types package, all props are optional by default. To make a prop required, you will have to use .
Firstly, npm install / yarn add the prop-types package if you haven't already. Then, add your propTypes (and defaultProps too if required) after the stateless functional component has been defined, before you export it.
Mostly no. If the props passed are not of the correct type, TypeScript will throw a compilation error. (which is better because PropTypes throws a runtime error, when statically typed like in TS you can't even run the code since it isn't compiled. So basically you can't push to production, hence no bugs. That's the whole point of using a statically-typed language)
Properties are by default required when written in interfaces (make sure you have strict: true
in tsconfig). You want active
to be optional right? So the interface would look like following:
interface Props {
text: string;
active?: boolean;
}
(use string
instead String
same for all other primitive types, string
and String
are not same)
In case of a functional component, it won't be different than how you define default values for parameters in any other normal function. Example:
const NavBarMenu = ({ text, active = false }: Props) => { ... }
Also TypeScript is not a full replacement for PropTypes, in most cases TS will be sufficient (and better).
Typescript
static defaultProps = {...props}
on the component classPropTypes
If you are using TypeScript in your project you don't need PropTypes
package, you achieve the same with only using interfaces.
You can define required/not required prop with the interface, for example:
interface MyProp {
text: String; // This value is required
active?: Boolean; // By adding ? the value becomes optional
}
In order to make a prop having a default value, you can do that with spreading the prop value on the component, something like:
const NavBarMenu = ({text, active = false}: Props) => {
// the value of active will default for false
return (
<li className="nav-item">
<a className={active ? "nav-link active" : "nav-link"} data-toggle="tab" href="#" role="tab">
{text}
</a>
</li>
);
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With