<?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: ⚡Priyang⚡</title>
    <description>The latest articles on DEV Community by ⚡Priyang⚡ (@priyang_bawa).</description>
    <link>https://dev.to/priyang_bawa</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%2F805352%2F83da1ea5-b271-4977-8242-7bc94d81fdde.jpg</url>
      <title>DEV Community: ⚡Priyang⚡</title>
      <link>https://dev.to/priyang_bawa</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/priyang_bawa"/>
    <language>en</language>
    <item>
      <title>Make Gradient with Background</title>
      <dc:creator>⚡Priyang⚡</dc:creator>
      <pubDate>Tue, 07 Feb 2023 17:53:06 +0000</pubDate>
      <link>https://dev.to/priyang_bawa/make-gradient-with-background-599g</link>
      <guid>https://dev.to/priyang_bawa/make-gradient-with-background-599g</guid>
      <description>&lt;h2&gt;
  
  
  background
&lt;/h2&gt;

&lt;p&gt;as we look at background properties &lt;a href="https://dev.toBackground"&gt;Blog&lt;/a&gt; like &lt;code&gt;background-iamge&lt;/code&gt; and  &lt;code&gt;background-color&lt;/code&gt; and others. but we can alot of properties in one line of code for faster and smiplifying styling. you can follow along &lt;a href="https://codepen.io/Wolfie12/pen/xxjbwRx" rel="noopener noreferrer"&gt;codepen&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Note the shorthand does not work for &lt;code&gt;background-blend-mode&lt;/code&gt; so if we apply any of the &lt;code&gt;background-blend-mode&lt;/code&gt; value than the shorthand will became invalid.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  background-color: #333;
  background-image: url("https://source.unsplash.com/RjBXz3rtdLw");
  background-position: center;
  background-size: 50%;
  background-repeat: repeat-y;

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

&lt;/div&gt;



&lt;p&gt;there is not much of order as you can see above in which the values to be applied but there are certain rule that needs to followed when using shorthand.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;bg-size&lt;/code&gt; can only be applied after &lt;code&gt;bg-postiosn&lt;/code&gt; and it has to seprated by '/' character &lt;code&gt;center/50%&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;  // shorthand for above
  background: #333 url("https://source.unsplash.com/RjBXz3rtdLw")  center/50% repeat-y;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Both will create same result you can playround in &lt;a href="https://codepen.io/Wolfie12/pen/xxjbwRx" rel="noopener noreferrer"&gt;codepen&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F61t0tcy7iv6gj9n21eg0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F61t0tcy7iv6gj9n21eg0.png" alt="https://ik.imagekit.io/5aalo5l7bu7/DevBlog/Screenshot_2022-09-06_at_4.05.09_AM_4NVzN9Hf7.png?ik-sdk-version=javascript-1.4.3&amp;amp;updatedAt=1672156383499" width="481" height="486"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now there is one more rule that if you want shorthand any box values like &lt;code&gt;content-box&lt;/code&gt; or any other for &lt;code&gt;bg-origin&lt;/code&gt; or &lt;code&gt;bg-clip&lt;/code&gt; if only one value is passed to shorthand than both &lt;code&gt;bg-origin&lt;/code&gt; and  &lt;code&gt;bg-clip&lt;/code&gt; will be same.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    background: #333 url("https://source.unsplash.com/RjBXz3rtdLw")  center/50% repeat-y content-box;
        // bg-origin = content-box bg-clip = content-boz
    background: #333 url("https://source.unsplash.com/RjBXz3rtdLw")  center/50% repeat-y content-box padding-box;
        // bg-origin = content-box bg-clip = padding-boz

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

&lt;/div&gt;



&lt;p&gt;One thing about shorthand is that if we put any of the bg proprties before and than we define them in shorthand than it will be overwritten or if we apply after shorthand than it will be overwritten.&lt;/p&gt;

&lt;p&gt;That's it for shorthand if you want more images just like &lt;code&gt;bg-image&lt;/code&gt; you can do that. we can create gradient using &lt;code&gt;bg-image&lt;/code&gt; or with shorthand so let's look into.&lt;/p&gt;

&lt;p&gt;There are three ways we can create gradients.&lt;/p&gt;

&lt;h2&gt;
  
  
  Linear Gradients
&lt;/h2&gt;

&lt;p&gt;Two create linear gradient background we need aleast two colors. we can also define degree or direction value to set in which way the gradients of colors to start. let's go to syntax &lt;a href="https://codepen.io/Wolfie12/pen/xxjbwRx" rel="noopener noreferrer"&gt;codepen&lt;/a&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    background-image: linear-gradient( #3e22c3, #fdbb2d);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fejttsk0ocms1rkvhj4lj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fejttsk0ocms1rkvhj4lj.png" alt="https://ik.imagekit.io/5aalo5l7bu7/DevBlog/Screenshot_2022-09-06_at_6.28.39_PM_KvLve39Hp.png?ik-sdk-version=javascript-1.4.3&amp;amp;updatedAt=1672156383454" width="800" height="798"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Pretty good looking right in above code the first color is bluis and second is yellowis. we can set colors in  any manner as we want like &lt;strong&gt;hsl&lt;/strong&gt; &lt;strong&gt;rgb&lt;/strong&gt; &lt;strong&gt;rgba&lt;/strong&gt; or &lt;strong&gt;hex&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;what the linear-gradient will do is set the first color blue on top and yellow on bottom and it will fill on the middle with combining both colors. we can set how much value we want to give one color and also the direction in which the gradient should take place.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;background-image:linear-gradient(to left, #3e22c3 50%, #fdbb2d 50%);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;in above the both color will take 50% and it will start from left to right so the gradient will start from right with blue color and end with yellow. first the blue will take 50% width and than other color will take rest of the size.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftigl5wt9tpv6jttvmkkk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftigl5wt9tpv6jttvmkkk.png" alt="https://ik.imagekit.io/5aalo5l7bu7/DevBlog/Screenshot_2022-09-06_at_7.31.48_PM_pyuHrn5caI.png?ik-sdk-version=javascript-1.4.3&amp;amp;updatedAt=1672156383237" width="800" height="91"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;since both color will take 50% no space for mixing the colors so you can change the % values in codepen to check how it creates diffrent mixes. try negative values for removing original color but to still create gradient mixes.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;linear-gradient(to left,#3e22c3 50%, #fdbb2d 30%);&lt;/code&gt; this will not change anything because the blues takes 50% and yellow's values will be ignored unless we have another color.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;linear-gradient(to left, #3e22c3 50%, #fdbb2d 50%, #fd2df2);&lt;/code&gt; this will create half blue and gradient. since we haven't put last color % it will divide color equally.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd6c3xsth7fmy81o7fk3h.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd6c3xsth7fmy81o7fk3h.png" alt="https://ik.imagekit.io/5aalo5l7bu7/DevBlog/Screenshot_2022-09-06_at_7.44.22_PM_S4YUWOrsCq.png?ik-sdk-version=javascript-1.4.3&amp;amp;updatedAt=1672156383512" width="800" height="91"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The Blue will not gradient with yellow because yellow and blue is 50%. we can put more % values to the color which will ending position and startgin postion for a color.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    background-image: linear-gradient(
    to right,
    #3e22c3 50% 20%,
    #fd2df2 100%
  ); // &amp;lt;- blue will stay solid till 50% than it will start to blend
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe0louodrbls1hs9z9jal.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe0louodrbls1hs9z9jal.png" alt="https://ik.imagekit.io/5aalo5l7bu7/DevBlog/Screenshot_2022-09-06_at_7.57.37_PM_1YY4rREFNj.png?ik-sdk-version=javascript-1.4.3&amp;amp;updatedAt=1672156383493" width="800" height="94"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;we can also use &lt;strong&gt;degree&lt;/strong&gt; and &lt;strong&gt;Turns&lt;/strong&gt; for setting gradient colors.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Degrees&lt;/strong&gt; — a value from &lt;code&gt;0deg&lt;/code&gt; to &lt;code&gt;360deg&lt;/code&gt;, where &lt;code&gt;0deg&lt;/code&gt; = &lt;code&gt;to top&lt;/code&gt;, &lt;code&gt;90deg&lt;/code&gt; = &lt;code&gt;to right&lt;/code&gt;, etc.&lt;br&gt;
&lt;strong&gt;Turns&lt;/strong&gt; are exactly what they sound like: 360 degree turns. This means that: &lt;code&gt;0.5turn&lt;/code&gt; = &lt;code&gt;180deg&lt;/code&gt;, &lt;code&gt;0.25turn&lt;/code&gt; = &lt;code&gt;90deg&lt;/code&gt;, etc.&lt;/p&gt;

&lt;p&gt;it work in clock wise angle if we put positive values to the gradient if we put negative value it will set gradients to anti-clock wise pattern.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  background-image: linear-gradient(90deg, #3e22c3 50% 20%, #fd2df2 100%);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;we can alos use &lt;strong&gt;Transparency&lt;/strong&gt; for creating fadding color.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    background-image: linear-gradient(
    180deg,
    #3e22c3 10%,
    rgba(253, 45, 242, 0) 100% 
    ); // alpha 0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Radial Gradients
&lt;/h2&gt;

&lt;p&gt;A radial gradient is defined by a &lt;em&gt;center point&lt;/em&gt;, an &lt;em&gt;ending shape&lt;/em&gt;, and two or more color-stop points. The space between these colors will gradually blend from one color to another with cirular objects.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  background-image: radial-gradient(#3e22c3, #fdbb2d);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3hnjegj3jh1v8kmwgwei.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3hnjegj3jh1v8kmwgwei.png" alt="https://ik.imagekit.io/5aalo5l7bu7/DevBlog/Screenshot_2022-09-06_at_8.49.02_PM_tTD1NcTJz.png?ik-sdk-version=javascript-1.4.3&amp;amp;updatedAt=1672156383621" width="" height=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;similar like degree and direction in &lt;code&gt;linear-grad&lt;/code&gt; we can add value at begging to create diffrent shapes for radial gradients. let's take a look at &lt;a href="https://codepen.io/Wolfie12/pen/xxjbwRx" rel="noopener noreferrer"&gt;codepen&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;There three type of values we can add before the colors.&lt;br&gt;
First we can set &lt;code&gt;ending-shape&lt;/code&gt; which will set the shape of the gradient mix. in above the we haven't defined the &lt;code&gt;ending-shape&lt;/code&gt; of the gradient so it create &lt;code&gt;ellipse&lt;/code&gt; by default. it can changed to &lt;code&gt;circle&lt;/code&gt;.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;radial-gradient(circle, #3e22c3, #fdbb2d);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;we can add &lt;code&gt;position&lt;/code&gt; value which same as &lt;code&gt;bg-position&lt;/code&gt;  which we covered in last section. since we haven't defined any it default is &lt;code&gt;center&lt;/code&gt;. &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;radial-gradient(circle at 10% 10%, #3e22c3, #fdbb2d);
// without ending-shape
radial-gradient(at 10% 10%, #3e22c3, #fdbb2d);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;now the last value is size of the gradient. Taking refrence from &lt;a href="https://developer.mozilla.org/en-US/docs/Web/CSS/gradient/radial-gradient" rel="noopener noreferrer"&gt;MDN Docs&lt;/a&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Keyword&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;closest-side&lt;/td&gt;
&lt;td&gt;The gradient's ending shape meets the side of the box closest to its center (for circles) or meets both the vertical and horizontal sides closest to the center (for ellipses).&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;closest-corner&lt;/td&gt;
&lt;td&gt;The gradient's ending shape is sized so that it exactly meets the closest corner of the box from its center.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;farthest-side&lt;/td&gt;
&lt;td&gt;Similar to &lt;code&gt;closest-side&lt;/code&gt;, except the ending shape is sized to meet the side of the box farthest from its center.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;farthest-corner&lt;/td&gt;
&lt;td&gt;The default value, the gradient's ending shape is sized so that it exactly meets the farthest corner of the box from its center.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;You should try diffrent values in codepen to check the deffrence it's making. depeding what you chose it will spread the gradient effect or shrink it. by default it is &lt;code&gt;farthest-corner&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;That's it there is one more property in to create &lt;strong&gt;Conic&lt;/strong&gt; gradient but i not going to discuss that because i never used it myself and i don't think i have ever saw in any website either but if you want to know more about the &lt;code&gt;conic-gradient&lt;/code&gt; you can read this article in MDN &lt;a href="https://developer.mozilla.org/en-US/docs/Web/CSS/gradient/conic-gradient" rel="noopener noreferrer"&gt;Link&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;you can use &lt;code&gt;bg-image&lt;/code&gt; stacking with gradient and create images with gradient on top.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs75rktqto6bd1mdul3sg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs75rktqto6bd1mdul3sg.png" alt="https://ik.imagekit.io/5aalo5l7bu7/DevBlog/Screenshot_2022-09-06_at_11.09.08_PM_jBJ2MHEqPt.png?ik-sdk-version=javascript-1.4.3&amp;amp;updatedAt=1672156383895" width="800" height="935"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;if You want to reach out or have any thoughs on the blog or have problem here are my links&lt;br&gt;
&lt;a href="https://twitter.com/Priyang_Bawa" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;  or &lt;a href="https://www.linkedin.com/in/priyang-patel-948685176/" rel="noopener noreferrer"&gt;linkedin&lt;/a&gt; . &lt;/p&gt;

&lt;p&gt;Other note i would highly recommad playing around in the codepen and there are two website that will help you to build gradients faster that i want to share with you.&lt;/p&gt;

&lt;p&gt;For Simpler Use &lt;a href="https://cssgradient.io/" rel="noopener noreferrer"&gt;cssgradient.io&lt;/a&gt;.&lt;br&gt;
For bit complex use &lt;a href="https://colorgradient.dev/gradient-generator" rel="noopener noreferrer"&gt;gradient-generator&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>programming</category>
      <category>tooling</category>
    </item>
    <item>
      <title>React Pattern - Build Better Component with Compound component</title>
      <dc:creator>⚡Priyang⚡</dc:creator>
      <pubDate>Tue, 27 Dec 2022 13:02:16 +0000</pubDate>
      <link>https://dev.to/priyang_bawa/react-pattern-compound-component-4c21</link>
      <guid>https://dev.to/priyang_bawa/react-pattern-compound-component-4c21</guid>
      <description>&lt;p&gt;React is a popular JavaScript library for building user interfaces, and one of the key concepts in React is the ability to create reusable components.&lt;/p&gt;

&lt;p&gt;Build Better Component with One pattern that can help with building reusable components is the "Compound Component" pattern. i am sure you have heard of this term before and i am going to explain this to you now.&lt;/p&gt;

&lt;p&gt;In the Compound Component pattern, a parent component passes data and behaviour down to a group of child components through props. The child components then use this data and behaviour to render themselves, and can also pass data back up to the parent through event handlers.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Think of compound components like the &lt;code&gt;_&amp;lt;select&amp;gt;_&lt;/code&gt; and &lt;code&gt;_&amp;lt;option&amp;gt;_&lt;/code&gt; elements in HTML. Apart they don’t do too much, but together they allow you to create the complete experience.  — &lt;a href="https://blog.kentcdodds.com/advanced-react-component-patterns-56af2b74bc5f" rel="noopener noreferrer"&gt;Kent C. Dodds&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;we have two ways of creating the component&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;React.cloneElement&lt;/li&gt;
&lt;li&gt;useContext
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;FormControl&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;label&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;children&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setValue&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;touched&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setTouched&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;valid&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setValid&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;errorMessage&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setErrorMessage&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;handleChange&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="na"&gt;event&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ChangeEvent&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;HTMLInputElement&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nf"&gt;setValid&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="nf"&gt;setValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="nf"&gt;setErrorMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nf"&gt;setValid&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="nf"&gt;setErrorMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Value is required&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;handleBlur&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;setTouched&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;

  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;label&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Label&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;label&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Label&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;}
&lt;/span&gt;      &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;cloneElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;children&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;onChange&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;handleChange&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;onBlur&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;handleBlur&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="p"&gt;})}&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;touched&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;valid&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;ErrorMessage&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;errorMessage&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/ErrorMessage&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;}
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Label&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;children&lt;/span&gt; &lt;span class="p"&gt;}:&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ComponentPropsWithoutRef&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;label&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;label&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;children&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/label&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;;
&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;ErrorMessage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;children&lt;/span&gt; &lt;span class="p"&gt;}:&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ComponentPropsWithoutRef&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;div&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt; &lt;span class="nx"&gt;className&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;error&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;children&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;;
&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;TextInput&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;onChange&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;onBlur&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;onChange&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="na"&gt;event&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ChangeEvent&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;HTMLInputElement&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;onBlur&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="na"&gt;event&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;FocusEvent&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;HTMLInputElement&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt; &lt;span class="kd"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;text&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="nx"&gt;onChange&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;onChange&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="nx"&gt;onBlur&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;onBlur&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Form&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;form&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;FormControl&lt;/span&gt; &lt;span class="nx"&gt;label&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Name&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;TextInput&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/FormControl&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;FormControl&lt;/span&gt; &lt;span class="nx"&gt;label&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Email&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;TextInput&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/FormControl&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="kd"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;submit&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Submit&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/form&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;this example, the &lt;code&gt;FormControl&lt;/code&gt; component is the parent, and the &lt;code&gt;TextInput&lt;/code&gt; component is the child. The &lt;code&gt;FormControl&lt;/code&gt; component uses the &lt;code&gt;useState&lt;/code&gt; hook to manage its own state values for the input value, touched state, valid state, and error message.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;FormControl&lt;/code&gt; component also has separate components for the label and the error message, which allows you to customize the appearance of these elements separately.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;TextInput&lt;/code&gt; component receives the value, onChange, and onBlur props from the &lt;code&gt;FormControl&lt;/code&gt; component, which allows it to update the form state values and trigger the validation logic when the input value changes or when the input is blurred.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;FormControl&lt;/code&gt; component also renders a label and an error message based on the state values. If the input has been touched and is not valid, it will display an error message.&lt;/p&gt;

&lt;p&gt;This compound component pattern allows the &lt;code&gt;FormControl&lt;/code&gt; component to handle the common logic for all form inputs, such as validation and error handling, while also allowing developers to customize the specific input element with the &lt;code&gt;TextInput&lt;/code&gt; component.&lt;/p&gt;

&lt;h3&gt;
  
  
  Problems
&lt;/h3&gt;

&lt;p&gt;Now it's does the job that we like to do but the problem with this will changing the behavior of our sub-components.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;if we like to pass diffrent value to the &lt;code&gt;label&lt;/code&gt; we don't have access to that or the &lt;code&gt;error&lt;/code&gt; div. one option is that pass the label as React Component or as i will explain in next section in &lt;code&gt;useContext&lt;/code&gt;.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;FormControl&lt;/span&gt; &lt;span class="nx"&gt;label&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Name&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="c1"&gt;// &amp;lt;- we don't have access to label &lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;TextInput&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/FormControl&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;if we pass a two component in the &lt;code&gt;FormControl&lt;/code&gt; than both will get the all the props
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;FormControl&lt;/span&gt; &lt;span class="nx"&gt;label&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Name&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;TextInput&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;OtherComponent&lt;/span&gt;&lt;span class="o"&gt;/&amp;gt;&lt;/span&gt; &lt;span class="c1"&gt;// &amp;lt;- this will also receive the props which can lead to bugs&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/FormControl&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;If I pass Wrap around the &lt;code&gt;&amp;lt;TextInput /&amp;gt;&lt;/code&gt; than it will not get the &lt;code&gt;onChange&lt;/code&gt; or &lt;code&gt;onBlur&lt;/code&gt; Function so we need to prop drill or do the same as &lt;code&gt;React.cloneElement&lt;/code&gt; to receive props which increases the complexity.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;FormControl&lt;/span&gt; &lt;span class="nx"&gt;label&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Name&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Wrapper&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="c1"&gt;// &amp;lt;- this will receive the props we will need prop drill&lt;/span&gt;
            &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;TextInput&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Wrapper&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/FormControl&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can look into Official React-beta docs to learn about the alternatives to &lt;code&gt;cloneElement&lt;/code&gt; - &lt;a href="https://beta.reactjs.org/reference/react/cloneElement#alternatives" rel="noopener noreferrer"&gt;Link&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  useContext()
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;createContext&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;useContext&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Create a context object&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;FormControlContext&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;createContext&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="na"&gt;touched&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;boolean&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="nl"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="nl"&gt;handleBlur&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="na"&gt;event&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;FocusEvent&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;HTMLInputElement&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="nl"&gt;handleChange&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="na"&gt;event&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ChangeEvent&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;HTMLInputElement&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="nl"&gt;valid&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;boolean&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="nl"&gt;errorMessage&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
   &lt;span class="na"&gt;touched&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
   &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
   &lt;span class="na"&gt;handleBlur&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{},&lt;/span&gt;
   &lt;span class="na"&gt;handleChange&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{},&lt;/span&gt;
   &lt;span class="na"&gt;valid&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
   &lt;span class="na"&gt;errorMessage&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;FormControlProps&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="nl"&gt;CustomHandleChange&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ChangeEvent&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;HTMLInputElement&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="nl"&gt;CustomHandleBlur&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;FocusEvent&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;HTMLInputElement&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="nl"&gt;IsInvalid&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="nx"&gt;boolean&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="nl"&gt;children&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ReactNode&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;FormControl&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt;
   &lt;span class="nx"&gt;CustomHandleChange&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
   &lt;span class="nx"&gt;CustomHandleBlur&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
   &lt;span class="nx"&gt;IsInvalid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
   &lt;span class="nx"&gt;children&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}:&lt;/span&gt; &lt;span class="nx"&gt;FormControlProps&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setValue&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
   &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;touched&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setTouched&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
   &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;valid&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setValid&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;IsInvalid&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
   &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;errorMessage&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setErrorMessage&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

   &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;handleChange&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="na"&gt;event&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ChangeEvent&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;HTMLInputElement&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
         &lt;span class="nf"&gt;setValid&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
         &lt;span class="nf"&gt;setValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
         &lt;span class="nf"&gt;setErrorMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
         &lt;span class="nf"&gt;setValid&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
         &lt;span class="nf"&gt;setErrorMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Value is required&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
   &lt;span class="p"&gt;};&lt;/span&gt;

   &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;handleBlur&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nf"&gt;setTouched&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
   &lt;span class="p"&gt;};&lt;/span&gt;

   &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;FormControlContext&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Provider&lt;/span&gt;
         &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{{&lt;/span&gt;
            &lt;span class="nx"&gt;touched&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="na"&gt;handleBlur&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;CustomHandleBlur&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;CustomHandleBlur&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;handleBlur&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="na"&gt;handleChange&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;CustomHandleChange&lt;/span&gt;
               &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;CustomHandleChange&lt;/span&gt;
               &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;handleChange&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="nx"&gt;valid&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="nx"&gt;errorMessage&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
         &lt;span class="p"&gt;}}&lt;/span&gt;
      &lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
         &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;children&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/FormControlContext.Provider&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;   &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Label&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;children&lt;/span&gt; &lt;span class="p"&gt;}:&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ComponentPropsWithoutRef&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;label&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;touched&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useContext&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;FormControlContext&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
   &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;label&lt;/span&gt;
         &lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{{&lt;/span&gt;
            &lt;span class="na"&gt;fontWeight&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;touched&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;bolder&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;lighter&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
         &lt;span class="p"&gt;}}&lt;/span&gt;
      &lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
         &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;children&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/label&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;   &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;ErrorMessage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt;
   &lt;span class="nx"&gt;CustomError&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
   &lt;span class="nx"&gt;children&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}:&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ComponentPropsWithoutRef&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;div&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;CustomError&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;touched&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;valid&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;errorMessage&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useContext&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;FormControlContext&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
   &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;touched&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;valid&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
         &lt;span class="o"&gt;&amp;lt;&amp;gt;&lt;/span&gt;
            &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt; &lt;span class="nx"&gt;className&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;error&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
               &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt; &lt;span class="nx"&gt;className&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;message&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
                  &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;CustomError&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;CustomError&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;errorMessage&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
               &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;            &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;            &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;children&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
         &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="p"&gt;);&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;
   &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;TextInput&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt; &lt;span class="p"&gt;}:&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ComponentPropsWithoutRef&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;input&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;handleBlur&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;handleChange&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useContext&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;FormControlContext&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
   &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt;
         &lt;span class="kd"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;text&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
         &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
         &lt;span class="nx"&gt;onChange&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;handleChange&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
         &lt;span class="nx"&gt;onBlur&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;handleBlur&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
         &lt;span class="p"&gt;{...&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;   &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Form&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;form&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
         &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;FormControl&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Label&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
            &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;TextInput&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
            &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;ErrorMessage&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
         &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/FormControl&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;         &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;FormControl&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Label&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
            &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;TextInput&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
            &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;ErrorMessage&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
         &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/FormControl&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;         &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="kd"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;submit&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Submit&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/form&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;   &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;In this example, the &lt;code&gt;FormControl&lt;/code&gt; component is the parent, and the child components are the ones that will consume the context. i know it got little bigger than the previus one but we got way more control over our components. &lt;/p&gt;

&lt;p&gt;Also we have added validation and custom handlers. The resone we could not have added in the previus because it will lead to bugs as i mention in the problem section.&lt;/p&gt;

&lt;p&gt;Explanation -&lt;/p&gt;

&lt;p&gt;&lt;code&gt;FormControl&lt;/code&gt; will pass the context values and function all the childrens without prop drilling. &lt;code&gt;Label&lt;/code&gt;, &lt;/p&gt;

&lt;p&gt;The &lt;code&gt;Label&lt;/code&gt; component is a simple wrapper around a &lt;code&gt;label&lt;/code&gt; element that receives its children as props. It uses the &lt;code&gt;touched&lt;/code&gt; state from the &lt;code&gt;FormControl&lt;/code&gt; context to style the label differently when the form control has been touched by the user.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;TextInput&lt;/code&gt; component is a simple wrapper around an &lt;code&gt;input&lt;/code&gt; element that receives all other props as an object. It uses the &lt;code&gt;handleBlur&lt;/code&gt;, &lt;code&gt;handleChange&lt;/code&gt;, and &lt;code&gt;value&lt;/code&gt; states from the &lt;code&gt;FormControl&lt;/code&gt; context to manage the value and event handlers for the input element.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;ErrorMessage&lt;/code&gt; component is a wrapper around a &lt;code&gt;div&lt;/code&gt; element that receives its children as props. It uses the &lt;code&gt;touched&lt;/code&gt;, &lt;code&gt;valid&lt;/code&gt;, and &lt;code&gt;errorMessage&lt;/code&gt; states from the &lt;code&gt;FormControl&lt;/code&gt; context to render an error message when the form control has been touched and is currently invalid. It also accepts a &lt;code&gt;CustomError&lt;/code&gt; prop that can be used to override the default error message.&lt;/p&gt;

&lt;p&gt;Using the useContext() hook will prevent the problem caused by &lt;code&gt;React.cloneElement&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;That's it &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuip8kw6v0n36z9g25yn4.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuip8kw6v0n36z9g25yn4.gif" alt="That's Pretty Much Gif" width="480" height="289"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Hope you enjoy the reading and if you want to have something to correct or feedback or just want to ask for help you can reach out to me on &lt;a href="https://twitter.com/Priyang_Bawa" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;  or &lt;a href="https://www.linkedin.com/in/priyang-patel-948685176/" rel="noopener noreferrer"&gt;linkedin&lt;/a&gt; though i personally prefer Twitter.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>learning</category>
      <category>indonesia</category>
    </item>
    <item>
      <title>Explain all the Background Properties</title>
      <dc:creator>⚡Priyang⚡</dc:creator>
      <pubDate>Tue, 27 Dec 2022 07:56:54 +0000</pubDate>
      <link>https://dev.to/priyang_bawa/background-properties-3apj</link>
      <guid>https://dev.to/priyang_bawa/background-properties-3apj</guid>
      <description>&lt;h2&gt;
  
  
  Background Properties
&lt;/h2&gt;

&lt;p&gt;i going to explore and tell you about all the Background Property that we can use to create a good experience for user.&lt;/p&gt;

&lt;h2&gt;
  
  
  Background
&lt;/h2&gt;

&lt;p&gt;We use &lt;code&gt;background&lt;/code&gt;  property to as name suggests it change background of the element.&lt;br&gt;
Follow the along with &lt;a href="https://codepen.io/Wolfie12/pen/QWrLvbM" rel="noopener noreferrer"&gt;codepen&lt;/a&gt; it is a shorthand properties for which we about to explore.&lt;/p&gt;
&lt;h2&gt;
  
  
  background-color
&lt;/h2&gt;

&lt;p&gt;background-color sets the color of the elements .let's get the basic &lt;code&gt;background-color:red;&lt;/code&gt; will set the background to red color. pretty simple right.&lt;br&gt;
but there is limited color strings that are recognized by the browser. so we have some other mehods for which we have some control and variance over colors.&lt;/p&gt;
&lt;h3&gt;
  
  
  Hex
&lt;/h3&gt;

&lt;p&gt;Hex is short for hexadecimal. the syntax is pretty much same as name strings.&lt;br&gt;
&lt;code&gt;background-color:#123456;&lt;/code&gt; or &lt;code&gt;background-color:#123;&lt;/code&gt; where &lt;code&gt;12&lt;/code&gt; is red ,&lt;code&gt;34&lt;/code&gt; is green and &lt;code&gt;56&lt;/code&gt; is blue values between 00 and ff (same as decimal 0-255). &lt;/p&gt;

&lt;p&gt;so the &lt;code&gt;background-color:red;&lt;/code&gt; will be &lt;code&gt;background-color:#ff0000;&lt;/code&gt;. &lt;code&gt;ff&lt;/code&gt; is the full internsity which will result in red background and as you go low it will less the internsity.&lt;/p&gt;

&lt;p&gt;it works similar like how light work where &lt;code&gt;#ffffff&lt;/code&gt; will result in full white and &lt;code&gt;#000000&lt;/code&gt; will result in full black. all that in between is determained by the rgb.&lt;/p&gt;

&lt;p&gt;sometimes you can use three digit hex but only when the value of  (RR, GG, and BB) are same.&lt;br&gt;
for exmple &lt;code&gt;#112233&lt;/code&gt; can also be written as &lt;code&gt;#333&lt;/code&gt;. also four digit hex will create transparent bg.&lt;/p&gt;

&lt;p&gt;let's say if want alpha or opacity to color but not the element so we can add another extra &lt;br&gt;
&lt;code&gt;background-color:#ff0000a3;&lt;/code&gt; will create sility less red color background. it also works with three digits &lt;code&gt;background-color:#f00a&lt;/code&gt; but &lt;code&gt;background-color:#f00ae&lt;/code&gt; will not work.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fexternal-content.duckduckgo.com%2Fiu%2F%3Fu%3Dhttp%253A%252F%252F3.bp.blogspot.com%252F-2OIxuVu7SZs%252FUlwTxrHSq_I%252FAAAAAAAABfc%252Fs_yCWsrvciY%252Fs1600%252Fhtmlcolorcode.jpg%26f%3D1%26nofb%3D1" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fexternal-content.duckduckgo.com%2Fiu%2F%3Fu%3Dhttp%253A%252F%252F3.bp.blogspot.com%252F-2OIxuVu7SZs%252FUlwTxrHSq_I%252FAAAAAAAABfc%252Fs_yCWsrvciY%252Fs1600%252Fhtmlcolorcode.jpg%26f%3D1%26nofb%3D1" alt="Embeddable " width="800" height="639"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  rgba or rgb
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;background-color: rgba(0,0,225,0.2);&lt;/code&gt; it's works same as hex but the syntax is diffrent and we can not use alphbaes inside  rgb or rbga we have to use appropriate decimal ranges from 0-255. &lt;br&gt;
 The a stands for apha which is same as opacity in this context. if we dont want alpha we can user&lt;code&gt;rbg(0,0,225)&lt;/code&gt;.&lt;/p&gt;
&lt;h4&gt;
  
  
  hsl
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;background-color: hsl(120, 100%, 50%);&lt;/code&gt; , hsl stands for hue, saturation, and lightness (HSL). hsl works bit diffrently than others. the hue value ranges from 0 to 360 where 0 is red, 120 is green, and 240 is blue. Saturation is a percentage value. 0% means a shade of gray, and 100% is the full color. it adds more grey color to hue color and last lightness is a percentage value. 0% is black, 50% is neither light or dark, 100% is white.&lt;/p&gt;

&lt;p&gt;we can add alpha by passing extra value which ranges from 1 to 10 like opacity.&lt;br&gt;
&lt;code&gt;background-color: hsl(120, 100%, 50%,0.5);&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Personally i would not recommand hsl unless some design choice is made, i would mostly go with hex values or rgba if alpha is needed. You can open the &lt;a href="https://codepen.io/Wolfie12/pen/QWrLvbM" rel="noopener noreferrer"&gt;codepen&lt;/a&gt; and playaround.&lt;/p&gt;
&lt;h2&gt;
  
  
  background-image
&lt;/h2&gt;

&lt;p&gt;it  sets one or more background images on an element. &lt;br&gt;
&lt;code&gt;background-image: url('https://source.unsplash.com/random/?dark-nature')&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;In above example i have used an open api for &lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F50tiljcohr37eidl4rbe.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F50tiljcohr37eidl4rbe.png" alt="https://ik.imagekit.io/5aalo5l7bu7/DevBlog/Screenshot_2022-09-01_at_4.39.37_AM_ZduZulYjn.png?ik-sdk-version=javascript-1.4.3&amp;amp;updatedAt=1672126360650" width="800" height="101"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;fetching images but it can be any type of url can be passed that will fetch a image or we can also user &lt;code&gt;url(../pathToImage)&lt;/code&gt;.&lt;br&gt;
It will put image with width and height of the element and image will fit from top to bottom and rest of the image will be cut down. as you can see the image is not full image you can look at original image &lt;a href="https://source.unsplash.com/RjBXz3rtdLw" rel="noopener noreferrer"&gt;ImageLink&lt;/a&gt; .  if we put &lt;code&gt;100vh&lt;/code&gt; on the element it will show more content of the image. same goes for &lt;code&gt;width&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmm02invm7fnq3o9fiax6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmm02invm7fnq3o9fiax6.png" alt="https://ik.imagekit.io/5aalo5l7bu7/DevBlog/Screenshot_2022-09-01_at_4.38.51_AM_HD_-owOr57.png?ik-sdk-version=javascript-1.4.3&amp;amp;updatedAt=1672126362371" width="800" height="354"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;we can also add more images on stack. which ever the url is first that will get more presidance and will be on top.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.bg-image{
  color: black;
  background-image: url('https://www.freeiconspng.com/uploads/spongebob-cartoon-png-32.png'),
 url('https://source.unsplash.com/RjBXz3rtdLw');
 height: 100vh; // &amp;lt;- show more content
} 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwk219vp1moakmtmo5cqv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwk219vp1moakmtmo5cqv.png" alt="https://ik.imagekit.io/5aalo5l7bu7/DevBlog/Screenshot_2022-09-01_at_5.02.35_AM_M0BGULpzck.png?ik-sdk-version=javascript-1.4.3&amp;amp;updatedAt=1672126360835" width="800" height="392"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;First Url is a art of spounce bob and  seoncd is naturel wallpaper as we mention above the both image does not fit the container due to it's sizes. &lt;/p&gt;

&lt;p&gt;Note : I want to mention that we can also put gradients of color using this proprty but later on that.&lt;/p&gt;

&lt;p&gt;Now how about we set there images correctly show it's fully visable.&lt;/p&gt;

&lt;h2&gt;
  
  
  background-position
&lt;/h2&gt;

&lt;p&gt;A position defines an x/y coordinateto place an item relative to the edges of an element's box, which means it will take that position size of the element. let's go to &lt;a href="https://codepen.io/Wolfie12/pen/QWrLvbM" rel="noopener noreferrer"&gt;codepen&lt;/a&gt; and code.&lt;br&gt;
for now i have remove spounce bob url from stack for better explaing. &lt;/p&gt;

&lt;p&gt;It can be defined using one to five values.  One of the keyword values &lt;code&gt;top&lt;/code&gt;, &lt;code&gt;left&lt;/code&gt;, &lt;code&gt;bottom&lt;/code&gt;, &lt;code&gt;right&lt;/code&gt;   &lt;code&gt;center&lt;/code&gt; or % or any other units value.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9qn6vdsqkrbkbirqmt2e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9qn6vdsqkrbkbirqmt2e.png" alt="https://ik.imagekit.io/5aalo5l7bu7/DevBlog/Pasted_image_20220901063824_SjBicLyzI.png?ik-sdk-version=javascript-1.4.3&amp;amp;updatedAt=1672126363615" width="800" height="362"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see above i have spltited origin photo into nine pieaces. now let's go to syntax.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  background-position-x: center;
  background-position-y: bottom;
  background-position: center bottom; // shorthand
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;background-position-y:bottom;&lt;/code&gt; as name suggrest y-axis to bottom of image and x sets x-axis as for the &lt;code&gt;background-position: center bottom;&lt;/code&gt; it shorthand for both where based on the value provided the axis will be decied.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    background-position: center bottom;
    background-position: bottom center; // &amp;lt;- same
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The default values for x is &lt;code&gt;left&lt;/code&gt; and for y is &lt;code&gt;top&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkzv7wllzyfoddtz14tpz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkzv7wllzyfoddtz14tpz.png" alt="https://ik.imagekit.io/5aalo5l7bu7/DevBlog/Screenshot_2022-09-01_at_9.14.29_AM_PZ4YGC1Uo.png?ik-sdk-version=javascript-1.4.3&amp;amp;updatedAt=1672126361517" width="800" height="496"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;you can compare result with other image and try out diffrent values and compare the changes.&lt;br&gt;
now it may not be same in your screen as shown but the point i want to make is the positions will cut image based on it provided values and &lt;code&gt;width&lt;/code&gt; of the container. &lt;/p&gt;

&lt;p&gt;now if we put arbitray number with units it will shift that much value in respactive axis and than display the image.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  background-position-y: center;  
  background-position-x: 20%;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F16equftes45anm5x5hfw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F16equftes45anm5x5hfw.png" alt="https://ik.imagekit.io/5aalo5l7bu7/DevBlog/Screenshot_2022-09-01_at_6.56.58_AM_vCL-pNYBZq.png?ik-sdk-version=javascript-1.4.3&amp;amp;updatedAt=1672126361957" width="800" height="496"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;here it is center-left coner of the image with &lt;code&gt;20%&lt;/code&gt; shift to the image. if i put &lt;code&gt;80%&lt;/code&gt;  than it will produce  Center-right with 20% offset. you can try diffrent values in &lt;a href="https://codepen.io/Wolfie12/pen/QWrLvbM" rel="noopener noreferrer"&gt;codepen&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Note :  &lt;code&gt;top top&lt;/code&gt;   and &lt;code&gt;left right&lt;/code&gt; are not valid means same proprtie or opsite axis property does not work. &lt;br&gt;
&lt;code&gt;background-position : center&lt;/code&gt; will &lt;strong&gt;center-center&lt;/strong&gt; the image and &lt;code&gt;background-position : right;&lt;/code&gt; will &lt;strong&gt;center-right&lt;/strong&gt;. same goes for others.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  background-position : right; // &amp;lt;- center-right
  background-position : left; // &amp;lt;- left-right
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;we can pass one more value to &lt;code&gt;background-position&lt;/code&gt; proprty. Two values are keyword values, and the third is the offset for the preceding value.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;background-position: center bottom 20px;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft201iifsynok8ccz0rjg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft201iifsynok8ccz0rjg.png" alt="https://ik.imagekit.io/5aalo5l7bu7/DevBlog/Screenshot_2022-09-01_at_6.44.44_AM_Odzt78VnE.png?ik-sdk-version=javascript-1.4.3&amp;amp;updatedAt=1672126361839" width="800" height="496"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;the image will set to center bottom and offset to 20px to bottom with new repeat image. Why repeat image we will talk that is next proproty.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  background-position: center bottom 20px;
  background-position: bottom 20px right; // &amp;lt;- same as above

  // This will not work because form center there will be not possible to offset
  background-position: center center 20px;

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

&lt;/div&gt;



&lt;p&gt;But there is one value we can add&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;      background-position: top 15px right 20px;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;this will cause the image to have top side with 15px offset and righ side 20px offset.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkm8eobv13ehdecoqc5bz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkm8eobv13ehdecoqc5bz.png" alt="https://ik.imagekit.io/5aalo5l7bu7/DevBlog/Screenshot_2022-09-01_at_8.43.49_AM_59R6AcplVi.png?ik-sdk-version=javascript-1.4.3&amp;amp;updatedAt=1672126362884" width="800" height="496"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;about percentage how the exactlty they are mesured i would recommed you to read this &lt;a href="https://developer.mozilla.org/en-US/docs/Web/CSS/background-position#regarding_percentages" rel="noopener noreferrer"&gt;MDN articale&lt;/a&gt; .&lt;/p&gt;

&lt;h2&gt;
  
  
  background-repeat
&lt;/h2&gt;

&lt;p&gt;as name suggrests it sets how background images are repeated. by default as we saw above the proprty is set to &lt;code&gt;repeat&lt;/code&gt; but we can stop that by &lt;code&gt;no-repeat&lt;/code&gt; let look at &lt;a href="https://codepen.io/Wolfie12/pen/QWrLvbM" rel="noopener noreferrer"&gt;codepen&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  color:blue;
  background-image: url("https://source.unsplash.com/RjBXz3rtdLw");
  background-size:contain;
  background-repeat: repeat;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;if we remove the &lt;code&gt;background-repeat&lt;/code&gt; it will give by default on repeat. which will repeat on both x and y but it will not repeat on x in this case because the &lt;code&gt;width&lt;/code&gt; of the image will fill the container.  adn we have put &lt;code&gt;background-size:contain;&lt;/code&gt; but later on that.&lt;/p&gt;

&lt;p&gt;![[Screenshot 2022-09-02 at 9.43.12 AM.png]]&lt;/p&gt;

&lt;p&gt;We have &lt;code&gt;repeat-x&lt;/code&gt; of only x repeat &lt;code&gt;repeat-y&lt;/code&gt; for only repeat y. there is two of more values.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;space&lt;/code&gt; which will make the image is repeated as much as possible without clipping. The first and last images are pinned to either side of the element, and whitespace is distributed evenly between the images. in this case &lt;code&gt;background-position&lt;/code&gt; will be igored if it images need to cliped.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbdui6wdak2jx7qyyx3l9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbdui6wdak2jx7qyyx3l9.png" alt="https://ik.imagekit.io/5aalo5l7bu7/DevBlog/Screenshot_2022-09-02_at_10.15.54_AM_PpAysAze3.png?ik-sdk-version=javascript-1.4.3&amp;amp;updatedAt=1672126364949" width="754" height="790"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With &lt;code&gt;round&lt;/code&gt; the images will fit the size with out cutting. by reduceing the size or reducing the number of repeated images.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhgw6mprwnu01fdzt3kxu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhgw6mprwnu01fdzt3kxu.png" alt="https://ik.imagekit.io/5aalo5l7bu7/DevBlog/Screenshot_2022-09-02_at_10.44.52_AM_OIyx-2gd8.png?ik-sdk-version=javascript-1.4.3&amp;amp;updatedAt=1672126365134" width="800" height="149"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  background-size
&lt;/h2&gt;

&lt;p&gt;it will set as name sugggets size of the image. follow along with &lt;a href="https://codepen.io/Wolfie12/pen/QWrLvbM" rel="noopener noreferrer"&gt;codepen&lt;/a&gt; .&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  height: 100vh;
  background-size : 50%;
  //                X will only apply
  background-size : 50% 50%;
  //                X    Y both will apply
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjo1u8iivpt1yg0nolqpp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjo1u8iivpt1yg0nolqpp.png" alt="https://ik.imagekit.io/5aalo5l7bu7/DevBlog/Screenshot_2022-09-02_at_11.06.12_AM_UIf8A09EHT.png?ik-sdk-version=javascript-1.4.3&amp;amp;updatedAt=1672126365619" width="800" height="1077"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;background-size: contain;&lt;/code&gt; as we saw  background-repeat in section will spread image to it's &lt;code&gt;width&lt;/code&gt; and the &lt;code&gt;height&lt;/code&gt; will be auto calculated by &lt;code&gt;width&lt;/code&gt; and than image will be repeated based on the background-repeat.&lt;/p&gt;

&lt;p&gt;There is one more property which you properly have used or saw in &lt;code&gt;background&lt;/code&gt; shorthands. &lt;br&gt;
it's &lt;code&gt;background-size: cover;&lt;/code&gt; which will pit the image fully on the element container.&lt;/p&gt;

&lt;p&gt;Note: With &lt;code&gt;cover&lt;/code&gt; value &lt;code&gt;background-repeat&lt;/code&gt; will work because the image will take all the space of the container.&lt;/p&gt;

&lt;p&gt;Now We have go through alot but stay with me we are about create amazing Headings. there are just few proprties are remaining.&lt;/p&gt;
&lt;h2&gt;
  
  
  background-clip
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F17rbltgp22cbj6uqfdt0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F17rbltgp22cbj6uqfdt0.png" alt="https://ik.imagekit.io/5aalo5l7bu7/DevBlog/Screenshot_2022-09-02_at_11.56.57_AM_WhZz6tv9Wn.png?ik-sdk-version=javascript-1.4.3&amp;amp;updatedAt=1672126366215" width="800" height="956"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Looks goods right well let's look at code &lt;a href="https://codepen.io/Wolfie12/pen/QWrLvbM" rel="noopener noreferrer"&gt;codepen&lt;/a&gt; . Now note that when you open the codepen it you can see that i have set two properties&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    background-clip: text;
    -webkit-background-clip: text;
    color: transparent;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The resone for -webkit is we still need to add -webkit to use the proprty in chromium &lt;br&gt;
browsers.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;background-clip: text;&lt;/code&gt; will clip the background and expcet text on the container and when we make &lt;code&gt;color: transparent;&lt;/code&gt; will look like Image color texts. There are three more proprties &lt;code&gt;border-box&lt;/code&gt; | &lt;code&gt;padding-box&lt;/code&gt; | &lt;code&gt;content-box&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;border-box&lt;/code&gt; is the default which will not do any change. &lt;code&gt;border-box&lt;/code&gt; and &lt;code&gt;content-box&lt;/code&gt; is same as box sizing as we dicuss in [Box-Sizing Blog] but the image will be cut accordingly. &lt;code&gt;padding-box&lt;/code&gt; will add on &lt;code&gt;padding&lt;/code&gt; of the container and than cut the rest of the image.&lt;/p&gt;
&lt;h2&gt;
  
  
  background-origin
&lt;/h2&gt;

&lt;p&gt;background-origin defines how the background should start where after the &lt;code&gt;border&lt;/code&gt; , &lt;code&gt;padding&lt;/code&gt; or the content of the element. it's works similar way &lt;code&gt;background-clip&lt;/code&gt; but rather than cutting the image it defines where the image should start from.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    background-origin : border-box;
    background-origin : content-box;
    background-origin : padding-box;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  background-attachment
&lt;/h2&gt;

&lt;p&gt;sets whether a background image's position is fixed or scrollable with in container. let's go to code &lt;a href="https://codepen.io/Wolfie12/pen/QWrLvbM" rel="noopener noreferrer"&gt;codepen&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Video to Link - &lt;a href="https://youtube.com/shorts/ZOY6QA5eqho?feature=share" rel="noopener noreferrer"&gt;YTVideo&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I cant Add the Video in Markdown sadly so i had to use link😅.&lt;br&gt;
It's will &lt;code&gt;fix&lt;/code&gt; the image to position so it will give you like parallax effect.&lt;/p&gt;

&lt;p&gt;Note &lt;code&gt;fix&lt;/code&gt; will not work with &lt;code&gt;background-clip: text;&lt;/code&gt; and the container will not be scrollable&lt;/p&gt;

&lt;p&gt;&lt;code&gt;scroll&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The background is fixed relative to the element itself and does not scroll with its contents.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;local&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The background is fixed relative to the element's contents. If the element has a scrolling mechanism, the background scrolls with the element's contents.&lt;/p&gt;
&lt;h2&gt;
  
  
  background-blend-mode
&lt;/h2&gt;

&lt;p&gt;now let's come to last proprty but quite fun i would say😎. it sets how an element's background images should blend with each other and with the element's background color.&lt;br&gt;
let's look at open the &lt;a href="https://codepen.io/Wolfie12/pen/QWrLvbM" rel="noopener noreferrer"&gt;codepen&lt;/a&gt; .&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    background-color:red;
    background-blend-mode: difference;
    background-blend-mode: difference, luminosity;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzx7ati4q7gxanj8z9uyd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzx7ati4q7gxanj8z9uyd.png" alt="https://ik.imagekit.io/5aalo5l7bu7/DevBlog/Screenshot_2022-09-02_at_5.34.50_PM_jtXl4zBM2.png?ik-sdk-version=javascript-1.4.3&amp;amp;updatedAt=1672126362373" width="650" height="530"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;another use case would be put &lt;code&gt;background-color&lt;/code&gt; with dark color and &lt;code&gt;background-image&lt;/code&gt;&lt;br&gt;
 put &lt;code&gt;background-blend-mode: difference;&lt;/code&gt; this can replace &lt;code&gt;:after&lt;/code&gt; selector and creating dark shadow on image.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    background-color: #333;
    background-blend-mode: difference;
    background-image: url("https://source.unsplash.com/RjBXz3rtdLw");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Looks good right your can try diffrent values to see changes. Try with multiple images.&lt;/p&gt;

&lt;p&gt;Now there only one thing which is left to do which is shorthand proprty called &lt;code&gt;background&lt;/code&gt; which let you difine alot of above property into one line of code and let's us use gradient function for creating gridient background colors and mix with other values like &lt;code&gt;background-blend-mode&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;I am going to explain shorthand and gradient function in next blog, this one was quite a big one. i initally thought that i might go in 1 blog but i think it will be better in splits.&lt;/p&gt;

&lt;p&gt;Hope you enjoy the reading and if you want to have something to correct or feedback or just want to ask for help you can reach out to me on &lt;a href="https://twitter.com/Priyang_Bawa" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;  or &lt;a href="https://www.linkedin.com/in/priyang-patel-948685176/" rel="noopener noreferrer"&gt;linkedin&lt;/a&gt; though i personally prefer Twitter.&lt;/p&gt;

</description>
      <category>watercooler</category>
    </item>
    <item>
      <title>Mock API Call at FrontEnd</title>
      <dc:creator>⚡Priyang⚡</dc:creator>
      <pubDate>Fri, 11 Mar 2022 10:12:44 +0000</pubDate>
      <link>https://dev.to/priyang_bawa/mock-api-call-at-frontend-3afm</link>
      <guid>https://dev.to/priyang_bawa/mock-api-call-at-frontend-3afm</guid>
      <description>&lt;h2&gt;
  
  
  Integration Testing
&lt;/h2&gt;

&lt;p&gt;So when testing a component or page have you ever thought of if i can just check how would it behave if the data was coming from API.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Question&lt;/strong&gt;: Why should you mock api&lt;br&gt;
ans: you want to test the behaviour of the component and hardcoding data does not provide better test case.&lt;/p&gt;

&lt;p&gt;Also backend and frontend can be develop in parallel and when any behaviour from the backend changes we can first test those changes if it breaks⛔️ anything.&lt;/p&gt;

&lt;p&gt;Well worry not because if you are using Axios there is a pretty good library that can make the process smooth as butter on bread.&lt;br&gt;
It is called &lt;strong&gt;axios-mock-adapter&lt;/strong&gt;🔥.&lt;/p&gt;

&lt;p&gt;For this blog i will be using React with jest and react-testing-library but we can use this library in any framework or just plain old vanilla JS. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4xig4inxa34d9ms73mep.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4xig4inxa34d9ms73mep.png" alt="jest Images with react"&gt;&lt;/a&gt;&lt;br&gt;
Let's take a TDD approach and first make a test case for component where it fetch data from backend.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import axios from "axios";
import MockAdapter from "axios-mock-adapter";
import data from './data';

//Component
import Products from "../Products";

const mock = new MockAdapter(axios);

// we need to reset after every test.
afterEach(() =&amp;gt; {
  mock.reset();
});

// I keep all the mock data into separate file so 
// If there is any change we can just change in one file also 
// better for CRUD.

const MockData = data.Product

it("Mock Fetch Product Details", async () =&amp;gt; {
  // mock fetch product details api
  // mock get  mock.onGet(URL) the URL should be exactly the same as the Axios request
  // we can reply with a positive response or a negative response using header Code

  mock.onGet("/api/products").reply(200, MockData);

  render(&amp;lt;Products /&amp;gt;);

// wait for loading to complete
  waitForElementToBeRemoved(() =&amp;gt; screen.queryAllByTestId("Loading"));

  expect(
    screen.getByText(MockData.description)
  ).toBeInTheDocument();

});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And that's it that was all for mocking api. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsek2genr8cb9uezv1bvd.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsek2genr8cb9uezv1bvd.gif" alt="Mind blasting"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now let's create component&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import axios from "axios";
import { useEffect, useState } from "react";

const Products = () =&amp;gt; {
  const [products, setProducts] = useState&amp;lt;any&amp;gt;(null);
  const [loading, setLoading] = useState&amp;lt;any&amp;gt;(true);
  const [error, setError] = useState&amp;lt;any&amp;gt;(null);

  useEffect(() =&amp;gt; {
    const fetchData = async () =&amp;gt; {
      try {
        const response = await axios.get("/api/products");
        setProducts(response.data);
        setError(null);
      } catch (error) {
        setError(error);
        setProducts(null);
      } finally {
        setLoading(false);
      }
    };
    fetchData();
  }, []);

  if (loading) return &amp;lt;div data-testid='loading'&amp;gt;Loading&amp;lt;/div&amp;gt;;
  if (error) return &amp;lt;div&amp;gt;{error.message} &amp;lt;/div&amp;gt;;
  if (!products) return null;
  return (
    &amp;lt;div&amp;gt;
      {products.map((product: any) =&amp;gt; (
        &amp;lt;div key={product._id}&amp;gt;
          &amp;lt;h2&amp;gt;{product.name}&amp;lt;/h2&amp;gt;
          &amp;lt;img src={product.image} alt={product.name} /&amp;gt;
          &amp;lt;p&amp;gt;{product.description}&amp;lt;/p&amp;gt;
          &amp;lt;p&amp;gt;{product.price}&amp;lt;/p&amp;gt;
        &amp;lt;/div&amp;gt;
      ))}
    &amp;lt;/div&amp;gt;
  );
};
export default Products;

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

&lt;/div&gt;



&lt;p&gt;In next Test we will test fail response with 500 status code.&lt;br&gt;
Note: we can check any response we want&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;it("Mock Failed Response", async () =&amp;gt; {
  const ErrorResponse = "Error Message"
  // we just need to change status code
  mock.onGet("/api/products").reply(500, ErrorResponse);

  render(&amp;lt;SingleProduct /&amp;gt;);

  expect(
    screen.getByText(ErrorResponse)
  ).toBeInTheDocument();

});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we want to test Post, PUT, DELETE we just need to change one line of code&lt;br&gt;
&lt;code&gt;mock.onPost&lt;/code&gt;&lt;br&gt;
&lt;code&gt;mock.onPut&lt;/code&gt;&lt;br&gt;
&lt;code&gt;mock.onDelete&lt;/code&gt;&lt;br&gt;
or we can just put &lt;code&gt;mock.any&lt;/code&gt; this mock any method request.&lt;/p&gt;

&lt;p&gt;That was all but you the library can do much more than just this you also can cain api calls and create network errors.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mock.onGet("/api/products/product").networkError();

mock.onGet("/api/products/product").networkErrorOnce();

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

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://github.com/ctimmerm/axios-mock-adapter" rel="noopener noreferrer"&gt;Link to Repo&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Why should you mock the test
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;frontend development can start with parallel to the backend.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It's more better than just hardcoding the data into frontend for better reliability.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The mock API can easily be replaced with the Real API once it’s ready and if any change in data happens we can test if it breaks anything.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Error Handling — e.g., Timeouts, delays, validations, etc.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you want to know more about testing you components i would recommend reading this article by kent c todds.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;a href="https://kentcdodds.com/blog/write-tests" rel="noopener noreferrer"&gt;Blog Link&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;And that was all thanks for reading till now let me know how did you feel about the article, this is my first article or blog i would love to know you opinion.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>testing</category>
      <category>react</category>
      <category>apicall</category>
    </item>
  </channel>
</rss>
