Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make vs code autocomplete React component's prop types?

How to make VS Code autocomplete React component's prop types while using the component in JSX markup?

P.S.: I'm using JS, not TS.

like image 203
MaxPrilutskiy Avatar asked Jun 07 '17 09:06

MaxPrilutskiy


People also ask

How do you define a type of prop in React?

Using PropTypes in React PropTypes is React's internal mechanism for adding type checking to components. React components use a special property named propTypes to set up type checking. When props are passed to a React component, they are checked against the type definitions configured in the propTypes property.

Can we pass component as a prop?

You can pass a component as props in React by using the built-in children prop. All elements you pass between the opening and closing tags of a component get assigned to the children prop. Copied!

Can React component modify props?

A component cannot update its own props unless they are arrays or objects (having a component update its own props even if possible is an anti-pattern), but can update its state and the props of its children.

Can props pass JSX?

Not only can JSX elements be passed as props to components, but we can also pass other components as props.


2 Answers

There is also option to use @params definition:

/**  *   * @param {{header: string, subheader: string, imageAlt: string, contentList: Array, orderLink: Object, contentLink: Object}} props   */ export default function JumbotronMain(props) {...} 

enter image description here

like image 111
Lukas Bares Avatar answered Oct 06 '22 06:10

Lukas Bares


I noticed that if you have a stateless component and you get the props using ES6, when you use this component, you get the props with the autocomplete.

const Stateless = ({ prop1, prop2 }) => {   return (     <div></div>   ) } 

img

like image 23
Rafael Grilli Avatar answered Oct 06 '22 08:10

Rafael Grilli