<?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: Kiryl Henets</title>
    <description>The latest articles on DEV Community by Kiryl Henets (@kirillgenets).</description>
    <link>https://dev.to/kirillgenets</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%2F325246%2Fa7cec18b-ba87-43d9-a9e1-d29c997017ef.jpeg</url>
      <title>DEV Community: Kiryl Henets</title>
      <link>https://dev.to/kirillgenets</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kirillgenets"/>
    <language>en</language>
    <item>
      <title>Error Boundary for wrong props or how I hacked prop-types package</title>
      <dc:creator>Kiryl Henets</dc:creator>
      <pubDate>Sun, 02 Feb 2020 10:40:14 +0000</pubDate>
      <link>https://dev.to/kirillgenets/how-to-create-an-error-boundary-for-wrong-prop-types-or-how-to-hack-prop-types-package-5gdh</link>
      <guid>https://dev.to/kirillgenets/how-to-create-an-error-boundary-for-wrong-prop-types-or-how-to-hack-prop-types-package-5gdh</guid>
      <description>&lt;p&gt;Hi everyone! Not so long time ago my tech-lead at work gave me a task to create an Error Boundary for wrong prop-types. As we know, prop-types package throws usual console warnings if some components receive wrong props. But we needed it to throw an Error.&lt;br&gt;
There was no information about handling prop-types errors on the internet, so I decided to look into the source code of the &lt;a href="https://github.com/facebook/prop-types"&gt;prop-types package&lt;/a&gt;.&lt;br&gt;
And my attention was riveted by one interesting constant in the &lt;a href="https://github.com/facebook/prop-types/blob/master/lib/ReactPropTypesSecret.js"&gt;ReactPropTypesSecret.js&lt;/a&gt; file. That's it:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--zWJQ_bNR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/tddeenw9o873qnktlj3s.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zWJQ_bNR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/tddeenw9o873qnktlj3s.png" alt="SECRET_DO_NOT_PASS_THIS_OR_YOU_WILL_BE_FIRED"&gt;&lt;/a&gt;&lt;br&gt;
Looks funny, yeah? 😄&lt;br&gt;
So after laughing, I tried to figure out what's the sense of this constant. And I succeed.&lt;br&gt;
In the file &lt;a href="https://github.com/facebook/prop-types/blob/master/factoryWithTypeCheckers.js"&gt;factoryWithTypeCheckers.js&lt;/a&gt; I found function, that checks single prop-type (you give the reference to this function each time when you write propTypes for your component. For example, PropTypes.number calls this function for the number prop-type).&lt;br&gt;
That's how it looks:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--eaunwg0b--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/kbi3q0br5oleh873n2ou.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--eaunwg0b--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/kbi3q0br5oleh873n2ou.png" alt="factoryWithTypeCheckers.js"&gt;&lt;/a&gt; &lt;br&gt;
And we have to pay attention to the last argument of this function - secret. And if we pass the constant that we found in the previous step there - we will get access to hidden features of prop-types! In this case, our function will return the object of prop-types error (if there is no error it returns null).&lt;br&gt;
So with the usage of this knowledge, I created my own high-ordered component, that takes a component as an argument (and this component has to have &lt;em&gt;errorBoundary&lt;/em&gt; prop. This is a component, that our HOC will render if props are not valid). That's how it looks:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from 'react';
import SECRET from 'prop-types/lib/ReactPropTypesSecret';

const propTypesChecker = (Component) =&amp;gt; (props) =&amp;gt; {
  const analysis = Object.keys(Component.propTypes).map((key) =&amp;gt; {
    const validator = Component.propTypes[key];
    return validator(props, key, Component.name, '', null, SECRET);
  });

  const errors = analysis.filter((error) =&amp;gt; error !== null).map((error) =&amp;gt; error.message);
  const arePropsValid = errors.length === 0;
  const ErrorBoundary = props.errorBoundary;

  return (
    arePropsValid
      ? &amp;lt;Component {...props} /&amp;gt;
      : &amp;lt;ErrorBoundary errors={errors} /&amp;gt;
  );
};

export default propTypesChecker;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The main function here is the &lt;em&gt;analysis&lt;/em&gt; function. Here we use ReactPropTypesSecret in order to get access to error objects. And this function just returns all the error messages after checking PropTypes.&lt;/p&gt;

&lt;p&gt;And I created NPM-package, so you don`t need to write this on your own :)&lt;/p&gt;

&lt;p&gt;My package on NPM: &lt;a href="https://www.npmjs.com/package/react-props-validator"&gt;Click!&lt;/a&gt;&lt;br&gt;
Source code: &lt;a href="https://github.com/kirillgenets/react-props-validator"&gt;Click!&lt;/a&gt;&lt;/p&gt;

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