Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What would the propType be of a JSX prop in react

In react you can set the propType of the props that come into a component. For one of my props I want the value to either be null or a line of JSX. The React docs don't cover what propType should be used for JSX.

When testing this I found that it wouldn't throw errors when using the propTypes of object or element. When using another such as symbol I would get an error that said it was an object. If it was an object surely it should complain when it's an element.

What is the correct propType for JSX?

like image 333
silverlight513 Avatar asked Nov 25 '16 10:11

silverlight513


People also ask

What is a Proptype in React?

PropTypes are a good first line defense when it comes to debugging your apps. But before getting into detail about PropTypes, we have to understand the concept of props. Props are the read-only properties that are shared between components to give the unidirectional flow of React a dynamic touch.

What is the Proptype of the children prop?

Class components So, the type of the children prop in a class component is ReactNode .

How do I pass JSX as a prop?

You want to use JSX inside your props You can simply use {} to cause JSX to parse the parameter. The only limitation is the same as for every JSX element: It must return only one root element.

What does props mean in JSX?

“Props” is a special keyword in React, which stands for properties and is being used for passing data from one component to another.


2 Answers

Try using this propType...

React.PropTypes.element 
like image 148
curv Avatar answered Sep 26 '22 04:09

curv


PropTypes.node or PropTypes.element can be used

like image 27
Saisurya Kattamuri Avatar answered Sep 22 '22 04:09

Saisurya Kattamuri