<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Shubham Pandey</title>
    <description>The latest articles on DEV Community by Shubham Pandey (@shubham_02).</description>
    <link>https://dev.to/shubham_02</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F497305%2F874e7248-3df7-4e3f-ac3a-747ff0382894.jpeg</url>
      <title>DEV Community: Shubham Pandey</title>
      <link>https://dev.to/shubham_02</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/shubham_02"/>
    <language>en</language>
    <item>
      <title>React Type Checking</title>
      <dc:creator>Shubham Pandey</dc:creator>
      <pubDate>Tue, 27 Oct 2020 18:21:12 +0000</pubDate>
      <link>https://dev.to/shubham_02/react-type-checking-77o</link>
      <guid>https://dev.to/shubham_02/react-type-checking-77o</guid>
      <description>&lt;p&gt;&lt;em&gt;No Type checking&lt;/em&gt; -&amp;gt; Perk of using a dynamic language like Javascript.&lt;/p&gt;

&lt;p&gt;But as your app grows, you start facing issues/bugs due to mishandling of type in props passed.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If you have nested prop passing boom 💥 .&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Thankfully, React comes with built-in type checking abilities.&lt;/p&gt;

&lt;p&gt;React uses &lt;code&gt;prop-types&lt;/code&gt; library to implement type-checking.&lt;br&gt;
&lt;a href="https://www.npmjs.com/package/prop-types"&gt;Link&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Usage
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;App.js&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from "react"
import Cart from "./Cart"

function App() {
    return (
        &amp;lt;div&amp;gt;
            &amp;lt;Card price={400} title="Watch"}/&amp;gt;
            &amp;lt;Card price={600} title="Perfume"/&amp;gt;
            &amp;lt;Card price={1000} title="Mouse"/&amp;gt;
        &amp;lt;/div&amp;gt;
    )
}

export default App
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is a basic shopping cart example where each card represents single item with a &lt;code&gt;title&lt;/code&gt; and a &lt;code&gt;price&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Cart.js&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from "react"
import PropTypes from "prop-types"

const Cart = (props) =&amp;gt; {
  const { title, price } = props;
  return (
    &amp;lt;&amp;gt;
      &amp;lt;Card&amp;gt;
        &amp;lt;h2&amp;gt;{title}&amp;lt;/h2&amp;gt;
        &amp;lt;h3&amp;gt;{price}&amp;lt;/h3&amp;gt;
      &amp;lt;/Card&amp;gt;
    &amp;lt;/&amp;gt;
  );
};

Cart.propTypes = {
    title: PropTypes.string.isRequired,
    price: PropTypes.number
}

export default Cart

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;PropTypes&lt;/strong&gt; supports various validation and other handy code chunks.&lt;/p&gt;

&lt;p&gt;Let's say we wanted our price to be one of &lt;code&gt;200,600,400&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Cart.propTypes = {
    price: PropTypes.oneOf([200,300,400])
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Find other various use cases for &lt;code&gt;PropTypes&lt;/code&gt; &lt;a href="https://reactjs.org/docs/typechecking-with-proptypes.html#proptypes"&gt;Link&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Note:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Usage may vary as per your need but these are handy react-y way of type checks.😁&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
