{"id":28653,"date":"2023-02-22T16:18:52","date_gmt":"2023-02-22T10:48:52","guid":{"rendered":"https:\/\/mobisoftinfotech.com\/resources\/?p=28653"},"modified":"2026-04-22T12:16:48","modified_gmt":"2026-04-22T06:46:48","slug":"learn-typescript-with-react-by-building-a-crud-application","status":"publish","type":"post","link":"https:\/\/mobisoftinfotech.com\/resources\/blog\/learn-typescript-with-react-by-building-a-crud-application","title":{"rendered":"Learn TypeScript with React By Building a CRUD Application"},"content":{"rendered":"<p><span style=\"font-weight: 400;\">TypeScript is a powerful programming language that enhances the development of complex web applications by adding a static type system to JavaScript. React is a popular JavaScript library used for building user interfaces. Combining these two technologies can take your web development to the next level.&nbsp;<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">In this article, you will learn the basics of TypeScript with React in the first sections of the article. And then you will learn to build a contact manager app using React and TypeScript.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">So by the end of this article, you will have a clear understanding of how to properly use TypeScript with React.<\/span> If you want to refer the code for this entire project, you can download it from <a href=\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/contact-manager-app-typescript.zip\" download>here<\/a>.<\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">So let&#8217;s get started.<\/span><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">React And TypeScript Basics<\/h2>\n\n\n\n<p><span style=\"font-weight: 400;\">When learning React with TypeScript, the first thing you should understand is the file extension.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">Every React + TypesSript file need to have a <code>.tsx<\/code> extension.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">If the file does not contain any JSX-specific code, then you can use the <code>.ts<\/code> extension instead of the <code>.tsx<\/code> extension.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">To create a component in React with TypeScript, you can use the <code>FC<\/code> type from the <code>react<\/code> package and use it after the component name.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">Create a new file with the name <code>Header.tsx <\/code>and add the following code:<\/span><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\"><span class=\"hljs-keyword\">import<\/span> { FC } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'react'<\/span>;\n<span class=\"hljs-keyword\">const<\/span> Header: FC = <span class=\"hljs-function\"><span class=\"hljs-params\">()<\/span> =&gt;<\/span> {\n  <span class=\"hljs-keyword\">return<\/span> &lt;h1&gt;Welcome, Mike!&lt;<span class=\"hljs-regexp\">\/h1&gt;;\n};\nexport default Header;\n<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><span style=\"font-weight: 400;\">Here, we&#8217;re just displaying a welcome message in a functional component.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">Now, If we want to pass a dynamic <code>name <\/code>as a prop we can do that like this:<\/span><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\"><span class=\"hljs-keyword\">import<\/span> { FC } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'react'<\/span>;\n\n<span class=\"hljs-keyword\">interface<\/span> HeaderProps {\n  name: <span class=\"hljs-built_in\">string<\/span>;\n}\n\n<span class=\"hljs-keyword\">const<\/span> Header: FC&lt;HeaderProps&gt; = <span class=\"hljs-function\">(<span class=\"hljs-params\">{ <span class=\"hljs-params\">name<\/span> }<\/span>) =&gt;<\/span> {\n  <span class=\"hljs-keyword\">return<\/span> &lt;h4&gt;Welcome, {name}!&lt;<span class=\"hljs-regexp\">\/h4&gt;;\n};\n\nexport default Header;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><span style=\"font-weight: 400;\">Here, we have declared <code>HeaderProps<\/code> as an interface that has a <code>name<\/code> property and for the <code>Header<\/code> component, we have added angle brackets to use that interface.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">This way TypeScript will know that the <code>Header <\/code>component needs a <code>name <\/code>prop which is a required prop.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">And we can call the component like this:<\/span><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\">&lt;Header name=<span class=\"hljs-string\">'Jerry'<\/span> \/&gt;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><span style=\"font-weight: 400;\">If you miss any of the required props like <code>name <\/code>in our case, then TypeScript will throw an error as can be seen below:<\/span><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><noscript><img decoding=\"async\" width=\"800\" height=\"500\" src=\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/1_missing_prop_error.gif\" alt=\"Missing Prop\" class=\"wp-image-28662\"><\/noscript><img decoding=\"async\" width=\"800\" height=\"500\" src=\"data:image\/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%20800%20500%22%3E%3C%2Fsvg%3E\" alt=\"Missing Prop\" class=\"wp-image-28662 lazyload\" data-src=\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/1_missing_prop_error.gif\"><\/figure>\n\n\n\n<p><span style=\"font-weight: 400;\">So whatever props the component interface is designed to take has to be provided unless they&#8217;re optional.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">If some of the properties are optional and not required all the time then we can use the <code>?<\/code> operator to indicate that it&#8217;s an optional prop like this:<\/span><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\"><span class=\"hljs-keyword\">interface<\/span> HeaderProps {\n  name: <span class=\"hljs-built_in\">string<\/span>;\n  isMarried?: <span class=\"hljs-built_in\">boolean<\/span>;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><span style=\"font-weight: 400;\">Here, <code>isMarried<\/code> is made an optional prop because of the <code>?<\/code> at the end and we can use it like this:<\/span><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\"><span class=\"hljs-keyword\">const<\/span> Header: FC&lt;HeaderProps&gt; = <span class=\"hljs-function\">(<span class=\"hljs-params\">{ <span class=\"hljs-params\">name<\/span>, <span class=\"hljs-params\">isMarried<\/span> }<\/span>) =&gt;<\/span> {\n  <span class=\"hljs-keyword\">return<\/span> (\n    &lt;&gt;\n      &lt;h4&gt;Welcome, {name}!&lt;<span class=\"hljs-regexp\">\/h4&gt;\n      &lt;p&gt;Marital status: {isMarried ? 'Married' : 'Not Provided'}&lt;\/<\/span>p&gt;\n    &lt;<span class=\"hljs-regexp\">\/&gt;\n  );\n};\n<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><span style=\"font-weight: 400;\">So, If the <code>isMarried<\/code> prop is provided, you will see the <code>Married<\/code> status otherwise <code>Not Provided<\/code> status as shown below:<\/span><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><noscript><img decoding=\"async\" width=\"800\" height=\"500\" src=\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/2_optional_prop.gif\" alt=\"Optional Prop\" class=\"wp-image-28663\"><\/noscript><img decoding=\"async\" width=\"800\" height=\"500\" src=\"data:image\/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%20800%20500%22%3E%3C%2Fsvg%3E\" alt=\"Optional Prop\" class=\"wp-image-28663 lazyload\" data-src=\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/2_optional_prop.gif\"><\/figure>\n\n\n\n<p><span style=\"font-weight: 400;\">In the above code, we have used an interface to declare the type of props passed.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">But you can also create a type as shown below:<\/span><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\"><span class=\"hljs-keyword\">type<\/span> HeaderProps = {\n  name: <span class=\"hljs-built_in\">string<\/span>,\n  isMarried?: <span class=\"hljs-built_in\">boolean<\/span>\n};<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><span style=\"font-weight: 400;\">And use it the same way as used for the interface.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">It&#8217;s up to you which one to choose, however in most of the code, you will see the interface used instead of the type.<\/span><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Alternative Way of Declaring Component Props Types<\/h2>\n\n\n\n<p><span style=\"font-weight: 400;\">In the above code, we have used <code>: FC<\/code> to specify the props types.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">There is another way you will find in some of the React + TypeScript codebases.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">So instead of declaring a <code>Header<\/code> component like this:<\/span><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\"><span class=\"hljs-keyword\">const<\/span> Header: FC&lt;HeaderProps&gt; = <span class=\"hljs-function\">(<span class=\"hljs-params\">{ <span class=\"hljs-params\">name<\/span>, <span class=\"hljs-params\">isMarried<\/span> }<\/span>) =&gt;<\/span> {\n  <span class=\"hljs-comment\">\/\/ ...<\/span>\n};<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>we can declare it like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\"><span class=\"hljs-keyword\">const<\/span> Header = <span class=\"hljs-function\">(<span class=\"hljs-params\">{ <span class=\"hljs-params\">name<\/span>, <span class=\"hljs-params\">isMarried<\/span> }: <span class=\"hljs-params\">HeaderProps<\/span><\/span>) =&gt;<\/span> {\n  <span class=\"hljs-comment\">\/\/ ...<\/span>\n};<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><span style=\"font-weight: 400;\">As you can see, we have specified the TypeScript type while destructuring the props.<\/span><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How to Work With Event Handlers<\/h2>\n\n\n\n<p><span style=\"font-weight: 400;\">It&#8217;s common to have some event handlers in the component for events like click event, change event, etc.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">So let&#8217;s see how to work with them when using TypeScript.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">Take a look at the below code:<\/span><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\"><span class=\"hljs-keyword\">import<\/span> { FC, useState } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'react'<\/span>;\n<span class=\"hljs-keyword\">const<\/span> UserSearch: FC = <span class=\"hljs-function\"><span class=\"hljs-params\">()<\/span> =&gt;<\/span> {\n  <span class=\"hljs-keyword\">const<\/span> &#091;searchTerm, setSearchTerm] = useState(<span class=\"hljs-string\">''<\/span>);\n  <span class=\"hljs-keyword\">const<\/span> handleSearch = <span class=\"hljs-function\">(<span class=\"hljs-params\"><span class=\"hljs-params\">event<\/span><\/span>) =&gt;<\/span> {};\n  <span class=\"hljs-keyword\">return<\/span> (\n    &lt;&gt;\n      &lt;input\n        <span class=\"hljs-keyword\">type<\/span>=<span class=\"hljs-string\">'text'<\/span>\n        name=<span class=\"hljs-string\">'searchTerm'<\/span>\n        value={searchTerm}\n        placeholder=<span class=\"hljs-string\">'Type something!'<\/span>\n        onChange={handleSearch}\n      \/&gt;\n      {searchTerm &amp;&amp; &lt;div&gt;SearchTerm: {searchTerm}&lt;<span class=\"hljs-regexp\">\/div&gt;}\n      &lt;hr \/<\/span>&gt;\n    &lt;<span class=\"hljs-regexp\">\/&gt;\n  );\n};\nexport default UserSearch;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><span style=\"font-weight: 400;\">Here, we&#8217;re displaying an input field where users enter something and we&#8217;re displaying that value below that input.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">But as you can see, we&#8217;ve not specified any type for the `event` parameter of the <code>handleSearch<\/code> function which we explicitly need to specify otherwise we will get a TypeScript error as shown below:<\/span><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><noscript><img decoding=\"async\" width=\"1024\" height=\"487\" src=\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/3_event_error_type-1024x487.png\" alt=\"Event Type Error\" class=\"wp-image-28664\" srcset=\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/3_event_error_type-1024x487.png 1024w, https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/3_event_error_type-300x143.png 300w, https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/3_event_error_type-768x365.png 768w, https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/3_event_error_type-640x304.png 640w, https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/3_event_error_type-150x71.png 150w, https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/3_event_error_type.png 1208w\" sizes=\"(max-width: 1024px) 100vw, 1024px\"><\/noscript><img decoding=\"async\" width=\"1024\" height=\"487\" src=\"data:image\/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%201024%20487%22%3E%3C%2Fsvg%3E\" alt=\"Event Type Error\" class=\"wp-image-28664 lazyload\" srcset=\"data:image\/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%201024%20487%22%3E%3C%2Fsvg%3E 1024w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" data-srcset=\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/3_event_error_type-1024x487.png 1024w, https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/3_event_error_type-300x143.png 300w, https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/3_event_error_type-768x365.png 768w, https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/3_event_error_type-640x304.png 640w, https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/3_event_error_type-150x71.png 150w, https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/3_event_error_type.png 1208w\" data-src=\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/3_event_error_type-1024x487.png\"><\/figure>\n\n\n\n<p><span style=\"font-weight: 400;\">As you can see in the above screenshot, when the mouse hovered over the <code>event<\/code> parameter, TypeScript gives an error saying, &#8220;<code>Parameter event<\/code> implicitly has an <code>any<\/code> type.&#8221;<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">To fix that, we first need to identify the exact type of event.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">To do that, temporarily change the below code:<\/span><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\">onChange = { handleSearch };<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>to this code:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\">onChange = { <span class=\"hljs-function\">(<span class=\"hljs-params\"><span class=\"hljs-params\">event<\/span><\/span>) =&gt;<\/span> {} };<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><span style=\"font-weight: 400;\">Here, we&#8217;re using an inline function so, If you mouse over the event parameter, you will see the event type `React.ChangeEvent&lt;HTMLInputElement&gt;` which we can use for the event parameter as shown below:<\/span><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><noscript><img decoding=\"async\" width=\"800\" height=\"500\" src=\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/4_change_event_type.gif\" alt=\"handling change event type in React TypeScript form\" class=\"wp-image-28699\"><\/noscript><img decoding=\"async\" width=\"800\" height=\"500\" src=\"data:image\/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%20800%20500%22%3E%3C%2Fsvg%3E\" alt=\"handling change event type in React TypeScript form\" class=\"wp-image-28699 lazyload\" data-src=\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/4_change_event_type.gif\"><\/figure>\n\n\n\n<p><span style=\"font-weight: 400;\">So by just adding an inline function, you can easily identify the event type of any event handler added.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">Now, as we got the event type, we can set the <code>searchTerm<\/code> value using the <code>setSearchTerm<\/code> function as shown below:<\/span><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\"><span class=\"hljs-keyword\">const<\/span> handleSearch = <span class=\"hljs-function\">(<span class=\"hljs-params\"><span class=\"hljs-params\">event<\/span>: <span class=\"hljs-params\">React<\/span>.<span class=\"hljs-params\">ChangeEvent<\/span>&lt;<span class=\"hljs-params\">HTMLInputElement<\/span>&gt;<\/span>) =&gt;<\/span> {\n  setSearchTerm(event.target.value);\n};<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><span style=\"font-weight: 400;\">And the search is working as shown below:<\/span><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><noscript><img decoding=\"async\" width=\"800\" height=\"500\" src=\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/5_working_search.gif\" alt=\"Working Search\" class=\"wp-image-28667\"><\/noscript><img decoding=\"async\" width=\"800\" height=\"500\" src=\"data:image\/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%20800%20500%22%3E%3C%2Fsvg%3E\" alt=\"Working Search\" class=\"wp-image-28667 lazyload\" data-src=\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/5_working_search.gif\"><\/figure>\n\n\n\n<p><span style=\"font-weight: 400;\">Now, you know the basics of React + TypeScript, let&#8217;s build a contact manager app so will you get hands-on experience with React + TypeScript.<\/span><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How to Setup a TypeScript + React Project<\/h2>\n\n\n\n<p><span style=\"font-weight: 400;\">**You can see the final demo of the application we&#8217;re building <\/span><a href=\"https:\/\/contact-manager-app-14c32.web.app\/\" rel=\"nofollow noopener\"><span style=\"font-weight: 400;\">here<\/span><\/a><span style=\"font-weight: 400;\">.&nbsp;<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">To set up the app we will be using <\/span><a href=\"https:\/\/vitejs.dev\/\" rel=\"nofollow noopener\"><span style=\"font-weight: 400;\">Vite<\/span><\/a><span style=\"font-weight: 400;\">. It&#8217;s a popular and faster alternative to create-react-app.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">We&#8217;ll use Vite because <code>create-react-app<\/code> becomes slow when the application grows and takes a lot of time to refresh the page when we make any changes in the application code. Also, by default, it adds a lot of extra packages which we rarely need.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">Vite just rebuilds the things we changed, instead of rebuilding the entire application which saves a lot of time during development.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">Keep in mind that Vite requires Node.js version 14.18+, so make sure to install a Node version greater than or equal to 14.18.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">The easiest and simplest way to install and switch Node.js versions is to use [<\/span><a href=\"https:\/\/github.com\/nvm-sh\/nvm#installing-and-updating\" rel=\"nofollow noopener\"><span style=\"font-weight: 400;\">nvm<\/span><\/a><span style=\"font-weight: 400;\">].<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">Even if you&#8217;re using create-react-app, all the code you will learn in this tutorial should run exactly the same without any errors.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">To create a new Vite project with React and TypeScript, execute the <code>npm init vite<\/code> command from the terminal.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">It will ask you for the <code>project name<\/code>, <code>framework<\/code>, and <code>variant<\/code>.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">&#8211; For <code>project name<\/code>, you can enter <code>contact-manager-app-typescript<\/code> or any name of your choice.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">&#8211; For <code>framework<\/code>, select <code>React<\/code> from the list of options<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">&#8211; For <code>variant<\/code>, select <code>TypeScript<\/code> from the list of options<\/span><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><noscript><img decoding=\"async\" width=\"800\" height=\"500\" src=\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/6_create_project_using_vite.gif\" alt=\"Create Project using Vite\" class=\"wp-image-28668\"><\/noscript><img decoding=\"async\" width=\"800\" height=\"500\" src=\"data:image\/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%20800%20500%22%3E%3C%2Fsvg%3E\" alt=\"Create Project using Vite\" class=\"wp-image-28668 lazyload\" data-src=\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/6_create_project_using_vite.gif\"><\/figure>\n\n\n\n<p><span style=\"font-weight: 400;\">Once the project is created, you can open that project in your favorite IDE like Visual Studio Code.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">The project folder structure will look like this:<\/span><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><noscript><img decoding=\"async\" width=\"290\" height=\"437\" src=\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/7_folder_structure.png\" alt=\"Folder structure of React Vite project\" class=\"wp-image-28669\" srcset=\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/7_folder_structure.png 290w, https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/7_folder_structure-199x300.png 199w, https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/7_folder_structure-150x226.png 150w\" sizes=\"(max-width: 290px) 100vw, 290px\"><\/noscript><img decoding=\"async\" width=\"290\" height=\"437\" src=\"data:image\/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%20290%20437%22%3E%3C%2Fsvg%3E\" alt=\"Folder structure of React Vite project\" class=\"wp-image-28669 lazyload\" srcset=\"data:image\/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%20290%20437%22%3E%3C%2Fsvg%3E 290w\" sizes=\"(max-width: 290px) 100vw, 290px\" data-srcset=\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/7_folder_structure.png 290w, https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/7_folder_structure-199x300.png 199w, https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/7_folder_structure-150x226.png 150w\" data-src=\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/7_folder_structure.png\"><\/figure>\n\n\n\n<p><span style=\"font-weight: 400;\">Now, execute the <code>yarn install<\/code> or <code>npm install<\/code> command from inside the project folder to install all the packages from the <code>package.json<\/code> file.<\/span><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><noscript><img decoding=\"async\" width=\"800\" height=\"500\" src=\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/8_install_packages-1.gif\" alt=\"installing npm packages for React TypeScript project\" class=\"wp-image-28701\"><\/noscript><img decoding=\"async\" width=\"800\" height=\"500\" src=\"data:image\/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%20800%20500%22%3E%3C%2Fsvg%3E\" alt=\"installing npm packages for React TypeScript project\" class=\"wp-image-28701 lazyload\" data-src=\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/8_install_packages-1.gif\"><\/figure>\n\n\n\n<p><span style=\"font-weight: 400;\">Once all the packages are installed, you can execute the <code>yarn run dev<\/code> or <code>npm run dev<\/code> command to start the created React application.<\/span><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><noscript><img decoding=\"async\" width=\"654\" height=\"234\" src=\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/9_app_started.png\" alt=\"App Started\" class=\"wp-image-28670\" srcset=\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/9_app_started.png 654w, https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/9_app_started-300x107.png 300w, https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/9_app_started-640x229.png 640w, https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/9_app_started-150x54.png 150w\" sizes=\"(max-width: 654px) 100vw, 654px\"><\/noscript><img decoding=\"async\" width=\"654\" height=\"234\" src=\"data:image\/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%20654%20234%22%3E%3C%2Fsvg%3E\" alt=\"App Started\" class=\"wp-image-28670 lazyload\" srcset=\"data:image\/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%20654%20234%22%3E%3C%2Fsvg%3E 654w\" sizes=\"(max-width: 654px) 100vw, 654px\" data-srcset=\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/9_app_started.png 654w, https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/9_app_started-300x107.png 300w, https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/9_app_started-640x229.png 640w, https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/9_app_started-150x54.png 150w\" data-src=\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/9_app_started.png\"><\/figure>\n\n\n\n<p><span style=\"font-weight: 400;\">As you can see the application can be accessed on the URL <code>http:\/\/localhost:5173\/<\/code>.<\/span><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Initial Project Setup<\/h2>\n\n\n\n<p><span style=\"font-weight: 400;\">Install the <code>bootstrap<\/code>, <code>react-bootstrap<\/code> and <code>react-icons<\/code> npm packages by executing the following command from the project folder:<\/span><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\">yarn add bootstrap@<span class=\"hljs-number\">5.2<\/span><span class=\"hljs-number\">.3<\/span> react-bootstrap@<span class=\"hljs-number\">2.7<\/span><span class=\"hljs-number\">.0<\/span> react-icons@<span class=\"hljs-number\">4.7<\/span><span class=\"hljs-number\">.1<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><span style=\"font-weight: 400;\">or with npm:<\/span><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\">npm install bootstrap@<span class=\"hljs-number\">5.2<\/span><span class=\"hljs-number\">.3<\/span> react-bootstrap@<span class=\"hljs-number\">2.7<\/span><span class=\"hljs-number\">.0<\/span> react-icons@<span class=\"hljs-number\">4.7<\/span><span class=\"hljs-number\">.1<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><span style=\"font-weight: 400;\">Here, we&#8217;re installing the latest and specific versions of packages so you will not have any issues running the application If in the future there is a newer version of any of the packages.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">Now, open the <code>index.css<\/code> file and add the following contents inside it:<\/span><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-15\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\">* {\n  <span class=\"hljs-attribute\">padding<\/span>: <span class=\"hljs-number\">0<\/span>;\n  <span class=\"hljs-attribute\">margin<\/span>: <span class=\"hljs-number\">0<\/span>;\n  <span class=\"hljs-attribute\">box-sizing<\/span>: border-box;\n}\n<span class=\"hljs-selector-tag\">body<\/span> {\n  <span class=\"hljs-attribute\">font-family<\/span>: Inter, sans-serif;\n  <span class=\"hljs-attribute\">padding<\/span>: <span class=\"hljs-number\">1rem<\/span>;\n  <span class=\"hljs-attribute\">letter-spacing<\/span>: <span class=\"hljs-number\">1px<\/span>;\n  <span class=\"hljs-attribute\">background-color<\/span>: <span class=\"hljs-number\">#f7f6f9<\/span>;\n  <span class=\"hljs-attribute\">color<\/span>: <span class=\"hljs-number\">#7b774e<\/span>;\n}\n<span class=\"hljs-selector-class\">.main-container<\/span> {\n  <span class=\"hljs-attribute\">margin<\/span>: <span class=\"hljs-number\">1rem<\/span> auto;\n  <span class=\"hljs-attribute\">display<\/span>: flex;\n  <span class=\"hljs-attribute\">justify-content<\/span>: start;\n  <span class=\"hljs-attribute\">align-items<\/span>: baseline;\n}\n<span class=\"hljs-selector-class\">.btn<\/span>,\n<span class=\"hljs-selector-class\">.btn<\/span><span class=\"hljs-selector-pseudo\">:active<\/span>,\n<span class=\"hljs-selector-class\">.btn<\/span><span class=\"hljs-selector-pseudo\">:hover<\/span> {\n  <span class=\"hljs-attribute\">border<\/span>: none;\n  <span class=\"hljs-attribute\">background-color<\/span>: <span class=\"hljs-number\">#3b8855d6<\/span>;\n}\n<span class=\"hljs-selector-class\">.icon<\/span> {\n  <span class=\"hljs-attribute\">cursor<\/span>: pointer;\n}\n<span class=\"hljs-selector-class\">.errorMsg<\/span> {\n  <span class=\"hljs-attribute\">color<\/span>: <span class=\"hljs-number\">#f21e08<\/span>;\n  <span class=\"hljs-attribute\">background<\/span>: <span class=\"hljs-number\">#fff0f0<\/span>;\n  <span class=\"hljs-attribute\">padding<\/span>: <span class=\"hljs-number\">10px<\/span>;\n}\n<span class=\"hljs-selector-tag\">h1<\/span> {\n  <span class=\"hljs-attribute\">text-align<\/span>: center;\n}\n<span class=\"hljs-selector-class\">.contact-form<\/span> {\n  <span class=\"hljs-attribute\">margin<\/span>: <span class=\"hljs-number\">1rem<\/span> auto;\n  <span class=\"hljs-attribute\">width<\/span>: <span class=\"hljs-number\">45%<\/span>;\n}\n<span class=\"hljs-selector-class\">.modal-dialog<\/span> <span class=\"hljs-selector-class\">.contact-form<\/span> {\n  <span class=\"hljs-attribute\">width<\/span>: <span class=\"hljs-number\">100%<\/span>;\n}\n<span class=\"hljs-selector-class\">.submit-btn<\/span> {\n  <span class=\"hljs-attribute\">margin-top<\/span>: <span class=\"hljs-number\">1rem<\/span>;\n  <span class=\"hljs-attribute\">letter-spacing<\/span>: <span class=\"hljs-number\">1px<\/span>;\n}\n<span class=\"hljs-selector-class\">.contacts-list-table-container<\/span> {\n  <span class=\"hljs-attribute\">max-width<\/span>: <span class=\"hljs-number\">100%<\/span>;\n  <span class=\"hljs-attribute\">height<\/span>: <span class=\"hljs-number\">500px<\/span>;\n  <span class=\"hljs-attribute\">overflow<\/span>: auto;\n}\n<span class=\"hljs-selector-class\">.contacts-list<\/span> {\n  <span class=\"hljs-attribute\">display<\/span>: flex;\n  <span class=\"hljs-attribute\">width<\/span>: <span class=\"hljs-number\">45%<\/span>;\n  <span class=\"hljs-attribute\">flex-direction<\/span>: column;\n  <span class=\"hljs-attribute\">justify-content<\/span>: flex-start;\n  <span class=\"hljs-attribute\">align-items<\/span>: center;\n}\n<span class=\"hljs-selector-class\">.contacts-list-title<\/span> {\n  <span class=\"hljs-attribute\">margin-bottom<\/span>: <span class=\"hljs-number\">1.2rem<\/span>;\n}\n<span class=\"hljs-selector-class\">.contacts-list-table<\/span>,\n<span class=\"hljs-selector-class\">.contacts-list-table<\/span> <span class=\"hljs-selector-tag\">tr<\/span>,\n<span class=\"hljs-selector-class\">.contacts-list-table<\/span> <span class=\"hljs-selector-tag\">th<\/span>,\n<span class=\"hljs-selector-class\">.contacts-list-table<\/span> <span class=\"hljs-selector-tag\">td<\/span> {\n  <span class=\"hljs-attribute\">padding<\/span>: <span class=\"hljs-number\">5px<\/span> <span class=\"hljs-number\">20px<\/span>;\n}\n<span class=\"hljs-selector-class\">.contacts-list-table<\/span> {\n  <span class=\"hljs-attribute\">border-collapse<\/span>: collapse;\n  <span class=\"hljs-attribute\">width<\/span>: <span class=\"hljs-number\">100%<\/span>;\n}\n<span class=\"hljs-selector-class\">.contacts-list-table<\/span> <span class=\"hljs-selector-tag\">th<\/span>,\n<span class=\"hljs-selector-class\">.contacts-list-table<\/span> <span class=\"hljs-selector-tag\">td<\/span> {\n  <span class=\"hljs-attribute\">border<\/span>: <span class=\"hljs-number\">1px<\/span> solid <span class=\"hljs-number\">#dddddd<\/span>;\n  <span class=\"hljs-attribute\">text-align<\/span>: left;\n  <span class=\"hljs-attribute\">padding<\/span>: <span class=\"hljs-number\">8px<\/span>;\n}\n<span class=\"hljs-selector-class\">.contacts-list-table<\/span> <span class=\"hljs-selector-tag\">tr<\/span><span class=\"hljs-selector-pseudo\">:nth-child(even)<\/span> {\n  <span class=\"hljs-attribute\">background<\/span>: <span class=\"hljs-number\">#ecebee<\/span>;\n}\n<span class=\"hljs-selector-class\">.contacts-list-header<\/span> {\n  <span class=\"hljs-attribute\">position<\/span>: sticky;\n  <span class=\"hljs-attribute\">top<\/span>: <span class=\"hljs-number\">0<\/span>;\n  <span class=\"hljs-attribute\">background<\/span>: <span class=\"hljs-number\">#ecebee<\/span>;\n}\n<span class=\"hljs-keyword\">@media<\/span> screen <span class=\"hljs-keyword\">and<\/span> (<span class=\"hljs-attribute\">max-width:<\/span> <span class=\"hljs-number\">900px<\/span>) {\n  <span class=\"hljs-selector-class\">.contact-form<\/span>,\n  <span class=\"hljs-selector-class\">.contacts-list<\/span>,\n  <span class=\"hljs-selector-class\">.main-container<\/span> {\n    <span class=\"hljs-attribute\">flex-direction<\/span>: column;\n    <span class=\"hljs-attribute\">align-items<\/span>: center;\n    <span class=\"hljs-attribute\">width<\/span>: <span class=\"hljs-number\">100%<\/span>;\n  }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-15\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Now, open the <code>main.tsx<\/code> file and add the bootstrap CSS import before the index.css import as shown below:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-16\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\"><span class=\"hljs-keyword\">import<\/span> <span class=\"hljs-string\">'bootstrap\/dist\/css\/bootstrap.min.css'<\/span>;\n<span class=\"hljs-keyword\">import<\/span> <span class=\"hljs-string\">'.\/index.css'<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-16\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">How to Create the Initial Pages<\/h2>\n\n\n\n<p><span style=\"font-weight: 400;\">Now, create a <code>components<\/code> folder inside the <code>src<\/code> folder and create a <code>Header.tsx<\/code> file inside it.<\/span><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-17\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\"><span class=\"hljs-keyword\">import<\/span> { FC } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'react'<\/span>;\n<span class=\"hljs-keyword\">const<\/span> Header: FC = <span class=\"hljs-function\"><span class=\"hljs-params\">()<\/span> =&gt;<\/span> {\n  <span class=\"hljs-keyword\">return<\/span> (\n    &lt;header&gt;\n      &lt;h1&gt;Contact Manager App&lt;<span class=\"hljs-regexp\">\/h1&gt;\n    &lt;\/<\/span>header&gt;\n  );\n};\n<span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-keyword\">default<\/span> Header;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-17\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Here, we have declared the <code>Header<\/code> component using <code>FC<\/code> which is a TypeScript way of declaring a component as a functional component.<\/p>\n\n\n\n<p>If you want, you can skip the <code>: FC<\/code> as the code will work without mentioning it also but it&#8217;s always good to specify it explicitly.<\/p>\n\n\n\n<p>Now, Open the <code>App.tsx<\/code> file and replace it with the following contents:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-18\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\"><span class=\"hljs-keyword\">import<\/span> Header <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'.\/components\/Header'<\/span>;\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">App<\/span>(<span class=\"hljs-params\"><\/span>) <\/span>{\n  <span class=\"hljs-keyword\">return<\/span> (\n    &lt;div className=<span class=\"hljs-string\">'App'<\/span>&gt;\n      &lt;Header \/&gt;\n    &lt;<span class=\"hljs-regexp\">\/div&gt;\n  );\n}\nexport default App;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-18\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Now, If you start the application using <code>yarn run dev<\/code> or <code>npm run dev<\/code> and access it at URL <code>http:\/\/localhost:5173\/<\/code>, you will see the following screen:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><noscript><img decoding=\"async\" width=\"791\" height=\"461\" src=\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/10_initial_screen.png\" alt=\"Initial screen\" class=\"wp-image-28672\" srcset=\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/10_initial_screen.png 791w, https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/10_initial_screen-300x175.png 300w, https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/10_initial_screen-768x448.png 768w, https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/10_initial_screen-640x373.png 640w, https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/10_initial_screen-150x87.png 150w\" sizes=\"(max-width: 791px) 100vw, 791px\"><\/noscript><img decoding=\"async\" width=\"791\" height=\"461\" src=\"data:image\/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%20791%20461%22%3E%3C%2Fsvg%3E\" alt=\"Initial screen\" class=\"wp-image-28672 lazyload\" srcset=\"data:image\/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%20791%20461%22%3E%3C%2Fsvg%3E 791w\" sizes=\"(max-width: 791px) 100vw, 791px\" data-srcset=\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/10_initial_screen.png 791w, https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/10_initial_screen-300x175.png 300w, https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/10_initial_screen-768x448.png 768w, https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/10_initial_screen-640x373.png 640w, https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/10_initial_screen-150x87.png 150w\" data-src=\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/10_initial_screen.png\"><\/figure>\n\n\n\n<p>If you want, you can open the <code>index.html<\/code> file and change the title of the page to <code>Contact Manager App<\/code> instead of the <code>Vite + React + TS<\/code>.<\/p>\n\n\n\n<p>Now, create a new file <code>ContactForm.tsx<\/code> inside the <code>components<\/code> folder and add the following code inside it:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-19\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\"><span class=\"hljs-keyword\">import<\/span> { FC, useState } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'react'<\/span>;\n<span class=\"hljs-keyword\">import<\/span> { Button, Form } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'react-bootstrap'<\/span>;\n<span class=\"hljs-keyword\">const<\/span> ContactForm: FC = <span class=\"hljs-function\"><span class=\"hljs-params\">()<\/span> =&gt;<\/span> {\n  <span class=\"hljs-keyword\">const<\/span> &#091;contact, setContact] = useState({\n    firstName: <span class=\"hljs-string\">''<\/span>,\n    lastName: <span class=\"hljs-string\">''<\/span>,\n    phone: <span class=\"hljs-string\">''<\/span>\n  });\n  <span class=\"hljs-keyword\">const<\/span> handleOnChange = <span class=\"hljs-function\">(<span class=\"hljs-params\"><span class=\"hljs-params\">event<\/span><\/span>) =&gt;<\/span> {\n    <span class=\"hljs-keyword\">const<\/span> { name, value } = event.target;\n    setContact(<span class=\"hljs-function\">(<span class=\"hljs-params\"><span class=\"hljs-params\">prevState<\/span><\/span>) =&gt;<\/span> {\n      <span class=\"hljs-keyword\">return<\/span> {\n        ...prevState,\n        &#091;name]: value\n      };\n    });\n  };\n  <span class=\"hljs-keyword\">const<\/span> handleOnSubmit = <span class=\"hljs-function\">(<span class=\"hljs-params\"><span class=\"hljs-params\">event<\/span><\/span>) =&gt;<\/span> {\n    event.preventDefault();\n  };\n  <span class=\"hljs-keyword\">return<\/span> (\n    &lt;Form onSubmit={handleOnSubmit} className=<span class=\"hljs-string\">'contact-form'<\/span>&gt;\n      &lt;Form.Group controlId=<span class=\"hljs-string\">'firstName'<\/span>&gt;\n        &lt;Form.Label&gt;First Name&lt;<span class=\"hljs-regexp\">\/Form.Label&gt;\n        &lt;Form.Control\n          className='firstName'\n          name='firstName'\n          value={contact.firstName}\n          type='text'\n          onChange={handleOnChange}\n        \/<\/span>&gt;\n      &lt;<span class=\"hljs-regexp\">\/Form.Group&gt;\n      &lt;Form.Group controlId='lastName'&gt;\n        &lt;Form.Label&gt;Last Name&lt;\/<\/span>Form.Label&gt;\n        &lt;Form.Control\n          className=<span class=\"hljs-string\">'lastName'<\/span>\n          name=<span class=\"hljs-string\">'lastName'<\/span>\n          value={contact.lastName}\n          <span class=\"hljs-keyword\">type<\/span>=<span class=\"hljs-string\">'text'<\/span>\n          onChange={handleOnChange}\n        \/&gt;\n      &lt;<span class=\"hljs-regexp\">\/Form.Group&gt;\n      &lt;Form.Group controlId='phone'&gt;\n        &lt;Form.Label&gt;Phone&lt;\/<\/span>Form.Label&gt;\n        &lt;Form.Control\n          className=<span class=\"hljs-string\">'phone'<\/span>\n          name=<span class=\"hljs-string\">'phone'<\/span>\n          value={contact.phone}\n          <span class=\"hljs-keyword\">type<\/span>=<span class=\"hljs-string\">'number'<\/span>\n          onChange={handleOnChange}\n        \/&gt;\n      &lt;<span class=\"hljs-regexp\">\/Form.Group&gt;\n      &lt;Form.Group controlId='submit'&gt;\n        &lt;Button variant='primary' type='submit' className='submit-btn'&gt;\n          Add Contact\n        &lt;\/<\/span>Button&gt;\n      &lt;<span class=\"hljs-regexp\">\/Form.Group&gt;\n    &lt;\/<\/span>Form&gt;\n  );\n};\n<span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-keyword\">default<\/span> ContactForm;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-19\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In the above code, we&#8217;re displaying a form with three input fields namely, first name, last name, phone, and a submit button.<\/p>\n\n\n\n<p>For displaying the inputs, we&#8217;re using the <code>Form.Control<\/code> component from <code>react-bootstrap<\/code> so the UI will look nice and we don&#8217;t need to write a lot of CSS ourselves.<\/p>\n\n\n\n<p>All the above code is a pure React code without any TypeScript Code, so you will see red underlines for the <code>event<\/code> parameter of the <code>handleOnChange<\/code> and <code>handleOnSubmit<\/code> methods as can be seen in the below screenshot:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><noscript><img decoding=\"async\" width=\"1270\" height=\"794\" src=\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/11_event_error.png\" alt=\"Event Error\" class=\"wp-image-28673\" srcset=\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/11_event_error.png 1270w, https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/11_event_error-300x188.png 300w, https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/11_event_error-1024x640.png 1024w, https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/11_event_error-768x480.png 768w, https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/11_event_error-640x400.png 640w, https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/11_event_error-150x94.png 150w\" sizes=\"(max-width: 1270px) 100vw, 1270px\"><\/noscript><img decoding=\"async\" width=\"1270\" height=\"794\" src=\"data:image\/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%201270%20794%22%3E%3C%2Fsvg%3E\" alt=\"Event Error\" class=\"wp-image-28673 lazyload\" srcset=\"data:image\/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%201270%20794%22%3E%3C%2Fsvg%3E 1270w\" sizes=\"(max-width: 1270px) 100vw, 1270px\" data-srcset=\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/11_event_error.png 1270w, https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/11_event_error-300x188.png 300w, https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/11_event_error-1024x640.png 1024w, https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/11_event_error-768x480.png 768w, https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/11_event_error-640x400.png 640w, https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/11_event_error-150x94.png 150w\" data-src=\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/11_event_error.png\"><\/figure>\n\n\n\n<p>As you can see in the above screenshot, when you mouse hover over the <code>event<\/code> parameter, you can see the TypeScript error saying, &#8220;<code>Parameter<\/code> <code>event<\/code> implicitly has an <code>any<\/code> type.&#8221;<\/p>\n\n\n\n<p>To fix that, we first need to identify the exact type of event we need to provide.<\/p>\n\n\n\n<p>To do that, temporarily change the below code:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-20\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\">onChange = { handleOnChange };<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-20\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>to this code:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-21\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\">onChange = { <span class=\"hljs-function\">(<span class=\"hljs-params\"><span class=\"hljs-params\">event<\/span><\/span>) =&gt;<\/span> {} };<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-21\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Here, we&#8217;re using an inline function so, if you mouse over the event parameter, you will see the event type <code>React.ChangeEvent&lt;FormControlElement&gt;<\/code> which we can use for the event parameter as shown below:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><noscript><img decoding=\"async\" width=\"800\" height=\"500\" src=\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2024\/11\/12_event_type.gif\" alt=\"defining event types in React TypeScript application\" class=\"wp-image-31748\"><\/noscript><img decoding=\"async\" width=\"800\" height=\"500\" src=\"data:image\/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%20800%20500%22%3E%3C%2Fsvg%3E\" alt=\"defining event types in React TypeScript application\" class=\"wp-image-31748 lazyload\" data-src=\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2024\/11\/12_event_type.gif\"><\/figure>\n\n\n\n<p>However, as we know, the element for which the <code>onChange<\/code> handler is added is an input element so instead of <code>React.ChangeEvent&lt;FormControlElement&gt;<\/code>, we can use <code>React.ChangeEvent&lt;HTMLInputElement&gt;<\/code> so we don&#8217;t need to import any extra TypeScript specific type.<\/p>\n\n\n\n<p>With this change, you can see the TypeScript error is gone.<\/p>\n\n\n\n<p>Similarly, we can find out the event parameter type for the <code>onSubmit<\/code> handler as shown below:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><noscript><img decoding=\"async\" width=\"800\" height=\"500\" src=\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2024\/11\/13_event_type_submit-.gif\" alt=\"form submit event handling in React TypeScript\" class=\"wp-image-31749\"><\/noscript><img decoding=\"async\" width=\"800\" height=\"500\" src=\"data:image\/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%20800%20500%22%3E%3C%2Fsvg%3E\" alt=\"form submit event handling in React TypeScript\" class=\"wp-image-31749 lazyload\" data-src=\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2024\/11\/13_event_type_submit-.gif\"><\/figure>\n\n\n\n<p>So using this simple way, we can find out the event type of any change or submit handler function.<\/p>\n\n\n\n<p>Now, you can revert the onChange and onSubmit handlers from inline functions to the respective handler function:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-22\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\">onChange = { handleOnChange };\nonSubmit = { handleOnSubmit };<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-22\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Now, open the <code>App.tsx<\/code> file, and let&#8217;s add the <code>ContactForm<\/code> component below the <code>Header<\/code> component.<\/p>\n\n\n\n<p>So your <code>App.tsx<\/code> file will look like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-23\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\"><span class=\"hljs-keyword\">import<\/span> ContactForm <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'.\/components\/ContactForm'<\/span>;\n<span class=\"hljs-keyword\">import<\/span> Header <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'.\/components\/Header'<\/span>;\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">App<\/span>(<span class=\"hljs-params\"><\/span>) <\/span>{\n  <span class=\"hljs-keyword\">return<\/span> (\n    &lt;div className=<span class=\"hljs-string\">'App'<\/span>&gt;\n      &lt;Header \/&gt;\n      &lt;div className=<span class=\"hljs-string\">'main-container'<\/span>&gt;\n        &lt;ContactForm \/&gt;\n      &lt;<span class=\"hljs-regexp\">\/div&gt;\n    &lt;\/<\/span>div&gt;\n  );\n}\n<span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-keyword\">default<\/span> App;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-23\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>And If you check the application, you will see the following screen:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><noscript><img decoding=\"async\" width=\"1024\" height=\"433\" src=\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/14_contact_form-1024x433.png\" alt=\"Contact Form\" class=\"wp-image-28676\" srcset=\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/14_contact_form-1024x433.png 1024w, https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/14_contact_form-300x127.png 300w, https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/14_contact_form-768x325.png 768w, https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/14_contact_form-640x271.png 640w, https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/14_contact_form-150x63.png 150w, https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/14_contact_form.png 1437w\" sizes=\"(max-width: 1024px) 100vw, 1024px\"><\/noscript><img decoding=\"async\" width=\"1024\" height=\"433\" src=\"data:image\/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%201024%20433%22%3E%3C%2Fsvg%3E\" alt=\"Contact Form\" class=\"wp-image-28676 lazyload\" srcset=\"data:image\/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%201024%20433%22%3E%3C%2Fsvg%3E 1024w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" data-srcset=\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/14_contact_form-1024x433.png 1024w, https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/14_contact_form-300x127.png 300w, https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/14_contact_form-768x325.png 768w, https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/14_contact_form-640x271.png 640w, https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/14_contact_form-150x63.png 150w, https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/14_contact_form.png 1437w\" data-src=\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/14_contact_form-1024x433.png\"><\/figure>\n\n\n\n<p>Let&#8217;s add a <code>console.log<\/code> statement in the <code>handleOnSubmit<\/code> method as shown below so we can see the data submitted.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-24\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\"><span class=\"hljs-keyword\">const<\/span> handleOnSubmit = <span class=\"hljs-function\">(<span class=\"hljs-params\"><span class=\"hljs-params\">event<\/span>: <span class=\"hljs-params\">React<\/span>.<span class=\"hljs-params\">FormEvent<\/span>&lt;<span class=\"hljs-params\">HTMLFormElement<\/span>&gt;<\/span>) =&gt;<\/span> {\n  event.preventDefault();\n  <span class=\"hljs-built_in\">console<\/span>.log(contact);\n};<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-24\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image size-full\"><noscript><img decoding=\"async\" width=\"800\" height=\"500\" src=\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/15_console_log.gif\" alt=\"Console Log\" class=\"wp-image-28677\"><\/noscript><img decoding=\"async\" width=\"800\" height=\"500\" src=\"data:image\/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%20800%20500%22%3E%3C%2Fsvg%3E\" alt=\"Console Log\" class=\"wp-image-28677 lazyload\" data-src=\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/15_console_log.gif\"><\/figure>\n\n\n\n<p>As you can see, we&#8217;re correctly able to store the details in the state with the name <code>contact<\/code> and display it on the console.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How to Use useReducer Hook For Storing Contacts<\/h2>\n\n\n\n<p><span style=\"font-weight: 400;\">Now, we need to display the added contacts on the page.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">To do that, we first need to store all the contacts together.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">So, we will use the <code>useReducer<\/code> hook for that using which we can easily handle the edit and delete contact functionality.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">If you&#8217;re not aware of the <code>useReducer<\/code> hook, then check out this <\/span><a href=\"https:\/\/beta.reactjs.org\/reference\/react\/useReducer\" rel=\"nofollow noopener\"><span style=\"font-weight: 400;\">article<\/span><\/a><span style=\"font-weight: 400;\">.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">Create a new folder with the name <code>reducer<\/code> inside the <code>src<\/code> folder and create a <code>contactsReducer.ts<\/code> file inside it with the following contents:<\/span><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-25\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\"><span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-keyword\">const<\/span> contactsReducer = <span class=\"hljs-function\">(<span class=\"hljs-params\"><span class=\"hljs-params\">state<\/span>, <span class=\"hljs-params\">action<\/span><\/span>) =&gt;<\/span> {\n  <span class=\"hljs-keyword\">switch<\/span> (action.type) {\n    <span class=\"hljs-keyword\">case<\/span> <span class=\"hljs-string\">'ADD_CONTACT'<\/span>:\n      <span class=\"hljs-keyword\">return<\/span> {\n        ...state,\n        contacts: &#091;...state.contacts, action.payload]\n      };\n    <span class=\"hljs-keyword\">default<\/span>:\n      <span class=\"hljs-keyword\">return<\/span> state;\n  }\n};<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-25\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Note that the file extension is <code>.ts<\/code> and not .<code>tsx<\/code> because there is no JSX code inside it.<\/p>\n\n\n\n<p>When you save the file, you will see a red underline for the <code>state<\/code> and <code>action<\/code> parameters as shown below:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><noscript><img decoding=\"async\" width=\"800\" height=\"500\" src=\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/16_state_action_error.gif\" alt=\"State Action Error\" class=\"wp-image-28678\"><\/noscript><img decoding=\"async\" width=\"800\" height=\"500\" src=\"data:image\/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%20800%20500%22%3E%3C%2Fsvg%3E\" alt=\"State Action Error\" class=\"wp-image-28678 lazyload\" data-src=\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/16_state_action_error.gif\"><\/figure>\n\n\n\n<p>So let&#8217;s define the TypeScript types for the <code>state<\/code> and <code>action<\/code> parameters as shown below:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-26\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\"><span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-keyword\">interface<\/span> Contact {\n  firstName: <span class=\"hljs-built_in\">string<\/span>;\n  lastName: <span class=\"hljs-built_in\">string<\/span>;\n  phone: <span class=\"hljs-built_in\">string<\/span>;\n}\n<span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-keyword\">interface<\/span> Action {\n  <span class=\"hljs-keyword\">type<\/span>: <span class=\"hljs-string\">'ADD_CONTACT'<\/span>\n  payload: Contact;\n}\n<span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-keyword\">interface<\/span> State {\n  contacts: Contact&#091;];\n}\n<span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-keyword\">const<\/span> contactsReducer = (state: State, action: Action): <span class=\"hljs-function\"><span class=\"hljs-params\">State<\/span> =&gt;<\/span> {\n  <span class=\"hljs-keyword\">switch<\/span> (action.type) {\n    <span class=\"hljs-keyword\">case<\/span> <span class=\"hljs-string\">'ADD_CONTACT'<\/span>:\n      <span class=\"hljs-keyword\">return<\/span> {\n        ...state,\n        contacts: &#091;...state.contacts, action.payload]\n      };\n    <span class=\"hljs-keyword\">default<\/span>:\n      <span class=\"hljs-keyword\">return<\/span> state;\n  }\n};<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-26\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In the above code, we&#8217;re also explicitly defining <code>State<\/code> as the return type of the <code>contactsReducer<\/code> function as can be seen after the arrow syntax (=&gt;):<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-27\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\"><span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-keyword\">const<\/span> contactsReducer = (state: State, action: Action): <span class=\"hljs-function\"><span class=\"hljs-params\">State<\/span> =&gt;<\/span> {<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-27\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><span style=\"font-weight: 400;\">If you have worked with redux before, you might know that <code>action<\/code> always contains a <code>type<\/code> property and an optional <code>payload<\/code>.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">So we have defined an <code>Action<\/code> interface to indicate the <code>action<\/code> type.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">As the contact form contains the <code>firstName<\/code>, <code>lastName<\/code>, and <code>phone<\/code> properties, we&#8217;re declaring an interface <code>Contact<\/code> indicating that it will be the type of payload.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">We&#8217;re also exporting those interfaces so we can use them in other files if required.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">Also, we have defined <code>state<\/code> as an object with a <code>contacts<\/code> property.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">The <code>contacts<\/code> property will be an array containing only the <code>firstName<\/code>, <code>lastName<\/code>, and <code>phone<\/code> properties so we have defined it as shown below:<\/span><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-28\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\"><span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-keyword\">interface<\/span> State {\n  contacts: Contact&#091;];\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-28\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>So the <code>contacts<\/code> property will always contain an array of objects of the <code>Contact<\/code> type.<\/p>\n\n\n\n<p>Now, open the <code>App.tsx<\/code> file and use the <code>contactsReducer<\/code> as the first argument for the <code>useReducer<\/code> hook as shown below:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-29\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\"><span class=\"hljs-keyword\">import<\/span> { useReducer } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'react'<\/span>;\n<span class=\"hljs-keyword\">import<\/span> ContactForm <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'.\/components\/ContactForm'<\/span>;\n<span class=\"hljs-keyword\">import<\/span> Header <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'.\/components\/Header'<\/span>;\n<span class=\"hljs-keyword\">import<\/span> { contactsReducer, State } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'.\/reducer\/contactsReducer'<\/span>;\n<span class=\"hljs-keyword\">const<\/span> initialState: State = {\n  contacts: &#091;]\n};\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">App<\/span>(<span class=\"hljs-params\"><\/span>) <\/span>{\n  <span class=\"hljs-keyword\">const<\/span> &#091;state, dispatch] = useReducer(contactsReducer, initialState);\n  <span class=\"hljs-keyword\">return<\/span> (\n    &lt;div className=<span class=\"hljs-string\">'App'<\/span>&gt;\n      &lt;Header \/&gt;\n      &lt;div className=<span class=\"hljs-string\">'main-container'<\/span>&gt;\n        &lt;ContactForm \/&gt;\n      &lt;<span class=\"hljs-regexp\">\/div&gt;\n    &lt;\/<\/span>div&gt;\n  );\n}\n<span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-keyword\">default<\/span> App;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-29\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>useReducer<\/code> hook returns an array containing two values. The first value of the array will be the actual state value and the second value will be the dispatch function which we can use to dispatch actions to the reducer.<\/p>\n\n\n\n<p>So we&#8217;re using array destructuring to access the <code>state<\/code> value and <code>dispatch<\/code> function.<\/p>\n\n\n\n<p>In the above code, we have defined <code>initialState<\/code> of type <code>State<\/code> which is imported from the <code>contactsReducer.ts<\/code> file.<\/p>\n\n\n\n<p>Now, let&#8217;s pass the <code>dispatch<\/code> function as a prop to the <code>ContactForm<\/code> component so we can dispatch the action to add a contact to the <code>contacts<\/code> array as shown below:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-30\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\"><span class=\"hljs-comment\">\/\/ ...<\/span>\n<span class=\"hljs-keyword\">const<\/span> &#091;state, dispatch] = useReducer(contactsReducer, initialState);\n<span class=\"hljs-keyword\">return<\/span> (\n  &lt;div className=<span class=\"hljs-string\">'App'<\/span>&gt;\n    &lt;Header \/&gt;\n    &lt;div className=<span class=\"hljs-string\">'main-container'<\/span>&gt;\n      &lt;ContactForm dispatch={dispatch} \/&gt;\n    &lt;<span class=\"hljs-regexp\">\/div&gt;\n  &lt;\/<\/span>div&gt;\n);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-30\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>After passing the prop, you will see a TypeScript error as shown below:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><noscript><img decoding=\"async\" width=\"800\" height=\"500\" src=\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/17_dispatch_prop_error.gif\" alt=\"Dispatch Prop Error\" class=\"wp-image-28679\"><\/noscript><img decoding=\"async\" width=\"800\" height=\"500\" src=\"data:image\/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%20800%20500%22%3E%3C%2Fsvg%3E\" alt=\"Dispatch Prop Error\" class=\"wp-image-28679 lazyload\" data-src=\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/17_dispatch_prop_error.gif\"><\/figure>\n\n\n\n<p>We&#8217;re getting a TypeScript error because whenever we pass any prop to the component, we need to mention the type of the `dispatch` prop in the component props list.<\/p>\n\n\n\n<p>If you mouse over the dispatch coming from the useReducer hook, you will see the actual type of dispatch as can be seen below:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><noscript><img decoding=\"async\" width=\"800\" height=\"500\" src=\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/18_dispatch_type.gif\" alt=\"Dispatch Type\" class=\"wp-image-28680\"><\/noscript><img decoding=\"async\" width=\"800\" height=\"500\" src=\"data:image\/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%20800%20500%22%3E%3C%2Fsvg%3E\" alt=\"Dispatch Type\" class=\"wp-image-28680 lazyload\" data-src=\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/18_dispatch_type.gif\"><\/figure>\n\n\n\n<p>To define the type of the <code>dispatch<\/code> prop, open the <code>ContactForm.tsx<\/code> file and declare a <code>ContactFormProps<\/code> interface above the component declaration like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-31\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\"><span class=\"hljs-keyword\">interface<\/span> ContactFormProps {\n  dispatch: React.Dispatch&lt;Action&gt;;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-31\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>And use it inside the angle brackets after <code>: FC<\/code>.<\/p>\n\n\n\n<p>So instead of the below code:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-32\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\"><span class=\"hljs-keyword\">const<\/span> ContactForm: FC = <span class=\"hljs-function\"><span class=\"hljs-params\">()<\/span> =&gt;<\/span> {<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-32\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Use the below code:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-33\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\"><span class=\"hljs-keyword\">const<\/span> ContactForm: FC&lt;ContactFormProps&gt; = <span class=\"hljs-function\"><span class=\"hljs-params\">()<\/span> =&gt;<\/span> {<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-33\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>So the above line will indicate that the <code>ContactForm<\/code> component will receive a prop with the name <code>dispatch<\/code> whose type is <code>React.Dispatch&lt;Action&gt;<\/code>.<\/p>\n\n\n\n<p>Also, don&#8217;t forget to add an import for the <code>Action<\/code> type at the top of the file like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-34\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\"><span class=\"hljs-keyword\">import<\/span> { Action } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'..\/reducer\/contactsReducer'<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-34\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Now, we can use the <code>dispatch<\/code> prop like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-35\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\"><span class=\"hljs-keyword\">const<\/span> ContactForm: FC&lt;ContactFormProps&gt; = <span class=\"hljs-function\">(<span class=\"hljs-params\"><span class=\"hljs-params\">props<\/span><\/span>) =&gt;<\/span> {\n  <span class=\"hljs-keyword\">const<\/span> { dispatch } = props;\n  <span class=\"hljs-comment\">\/\/ ....<\/span>\n};<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-35\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><span style=\"font-weight: 400;\">Or use inline destructuring like this:<\/span><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-36\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\"><span class=\"hljs-keyword\">const<\/span> ContactForm: FC&lt;ContactFormProps&gt; = <span class=\"hljs-function\">(<span class=\"hljs-params\">{ <span class=\"hljs-params\">dispatch<\/span> }<\/span>) =&gt;<\/span> {\n  <span class=\"hljs-comment\">\/\/ ....<\/span>\n};<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-36\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Now, inside the <code>handleOnSubmit<\/code> method, we can dispatch the <code>ADD_CONTACT<\/code> action.<\/p>\n\n\n\n<p>So, change the <code>handleOnSubmit<\/code> method to the below code:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-37\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\"><span class=\"hljs-keyword\">const<\/span> handleOnSubmit = <span class=\"hljs-function\">(<span class=\"hljs-params\"><span class=\"hljs-params\">event<\/span>: <span class=\"hljs-params\">React<\/span>.<span class=\"hljs-params\">FormEvent<\/span>&lt;<span class=\"hljs-params\">HTMLFormElement<\/span>&gt;<\/span>) =&gt;<\/span> {\n  event.preventDefault();\n  dispatch({\n    <span class=\"hljs-keyword\">type<\/span>: <span class=\"hljs-string\">'ADD_CONTACT'<\/span>,\n    payload: contact\n  });\n};<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-37\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Here, we&#8217;re passing the <code>contact<\/code> state value as a payload property to the <code>contactsReducer<\/code>.<\/p>\n\n\n\n<p>So when we dispatch the <code>ADD_CONTACT<\/code> action, the first switch case from the <code>contactsReducer<\/code> function will be executed where we&#8217;re returning a new object by spreading out all the properties of initialState (currently only contacts) and we&#8217;re adding new contact like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-38\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\">{\n  ...state,\n  contacts: &#091;...state.contacts, action.payload]\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-38\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Now, open the <code>App.tsx<\/code> file and add console.log after the <code>useReducer<\/code> hook line to display the <code>state<\/code> value as shown below:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-39\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\"><span class=\"hljs-built_in\">console<\/span>.log(<span class=\"hljs-string\">'state'<\/span>, state);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-39\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>So your <code>App.tsx<\/code> file will look like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-40\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\"><span class=\"hljs-keyword\">import<\/span> { useReducer } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'react'<\/span>;\n<span class=\"hljs-keyword\">import<\/span> ContactForm <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'.\/components\/ContactForm'<\/span>;\n<span class=\"hljs-keyword\">import<\/span> Header <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'.\/components\/Header'<\/span>;\n<span class=\"hljs-keyword\">import<\/span> { contactsReducer, State } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'.\/reducer\/contactsReducer'<\/span>;\n<span class=\"hljs-keyword\">const<\/span> initialState: State = {\n  contacts: &#091;]\n};\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">App<\/span>(<span class=\"hljs-params\"><\/span>) <\/span>{\n  <span class=\"hljs-keyword\">const<\/span> &#091;state, dispatch] = useReducer(contactsReducer, initialState);\n  <span class=\"hljs-built_in\">console<\/span>.log(<span class=\"hljs-string\">'state'<\/span>, state);\n  <span class=\"hljs-keyword\">return<\/span> (\n    &lt;div className=<span class=\"hljs-string\">'App'<\/span>&gt;\n      &lt;Header \/&gt;\n      &lt;div className=<span class=\"hljs-string\">'main-container'<\/span>&gt;\n        &lt;ContactForm dispatch={dispatch} \/&gt;\n      &lt;<span class=\"hljs-regexp\">\/div&gt;\n    &lt;\/<\/span>div&gt;\n  );\n}\n<span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-keyword\">default<\/span> App;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-40\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Now, If you check the application, you can see the state value coming from the <code>useReducer<\/code> hook.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><noscript><img decoding=\"async\" width=\"800\" height=\"500\" src=\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/19_state_log.gif\" alt=\"State Log\" class=\"wp-image-28681\"><\/noscript><img decoding=\"async\" width=\"800\" height=\"500\" src=\"data:image\/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%20800%20500%22%3E%3C%2Fsvg%3E\" alt=\"State Log\" class=\"wp-image-28681 lazyload\" data-src=\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/19_state_log.gif\"><\/figure>\n\n\n\n<p>As you can see, the provided contact details are successfully added in the <code>contacts<\/code> property which is declared in the <code>initialState<\/code> object of the <code>App.tsx<\/code> file.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How to Display Added Contacts List<\/h2>\n\n\n\n<p>Now, instead of displaying the contacts in the console, let&#8217;s create a component to display that list.<\/p>\n\n\n\n<p>Create a new file <code>ContactList.tsx<\/code> inside the <code>components<\/code> folder with the following code:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-41\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\"><span class=\"hljs-keyword\">import<\/span> { FC } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'react'<\/span>;\n<span class=\"hljs-keyword\">import<\/span> { Contact } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'..\/reducer\/contactsReducer'<\/span>;\n<span class=\"hljs-keyword\">interface<\/span> ContactListProps {\n  contacts: Contact&#091;];\n}\n<span class=\"hljs-keyword\">const<\/span> ContactList: FC&lt;ContactListProps&gt; = <span class=\"hljs-function\">(<span class=\"hljs-params\">{ <span class=\"hljs-params\">contacts<\/span> }<\/span>) =&gt;<\/span> {\n  <span class=\"hljs-keyword\">return<\/span> (\n    &lt;div className=<span class=\"hljs-string\">'contacts-list'<\/span>&gt;\n      &lt;h3 className=<span class=\"hljs-string\">'contacts-list-title'<\/span>&gt;List of Contacts&lt;<span class=\"hljs-regexp\">\/h3&gt;\n      &lt;div className='contacts-list-table-container'&gt;\n        &lt;table className='contacts-list-table'&gt;\n          &lt;thead className='contacts-list-header'&gt;\n            &lt;tr&gt;\n              &lt;th&gt;First Name&lt;\/<\/span>th&gt;\n              &lt;th&gt;Last Name&lt;<span class=\"hljs-regexp\">\/th&gt;\n              &lt;th&gt;Phone&lt;\/<\/span>th&gt;\n            &lt;<span class=\"hljs-regexp\">\/tr&gt;\n          &lt;\/<\/span>thead&gt;\n          &lt;tbody&gt;\n            {contacts.map(<span class=\"hljs-function\">(<span class=\"hljs-params\">{ <span class=\"hljs-params\">firstName<\/span>, <span class=\"hljs-params\">lastName<\/span>, <span class=\"hljs-params\">phone<\/span> }<\/span>) =&gt;<\/span> (\n              &lt;tr&gt;\n                &lt;td&gt;{firstName}&lt;<span class=\"hljs-regexp\">\/td&gt;\n                &lt;td&gt;{lastName}&lt;\/<\/span>td&gt;\n                &lt;td&gt;{phone}&lt;<span class=\"hljs-regexp\">\/td&gt;\n              &lt;\/<\/span>tr&gt;\n            ))}\n          &lt;<span class=\"hljs-regexp\">\/tbody&gt;\n        &lt;\/<\/span>table&gt;\n      &lt;<span class=\"hljs-regexp\">\/div&gt;\n    &lt;\/<\/span>div&gt;\n  );\n};\n<span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-keyword\">default<\/span> ContactList;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-41\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Now, open the <code>App.tsx<\/code> file and use the <code>ContactList<\/code> component after the <code>ContactForm<\/code> component. So the <code>App.tsx<\/code> file will look like this now:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-42\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\"><span class=\"hljs-keyword\">import<\/span> { useReducer } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'react'<\/span>;\n<span class=\"hljs-keyword\">import<\/span> ContactForm <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'.\/components\/ContactForm'<\/span>;\n<span class=\"hljs-keyword\">import<\/span> ContactList <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'.\/components\/ContactList'<\/span>;\n<span class=\"hljs-keyword\">import<\/span> Header <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'.\/components\/Header'<\/span>;\n<span class=\"hljs-keyword\">import<\/span> { contactsReducer, State } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'.\/reducer\/contactsReducer'<\/span>;\n<span class=\"hljs-keyword\">const<\/span> initialState: State = {\n  contacts: &#091;]\n};\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">App<\/span>(<span class=\"hljs-params\"><\/span>) <\/span>{\n  <span class=\"hljs-keyword\">const<\/span> &#091;state, dispatch] = useReducer(contactsReducer, initialState);\n  <span class=\"hljs-built_in\">console<\/span>.log(<span class=\"hljs-string\">'state'<\/span>, state);\n  <span class=\"hljs-keyword\">return<\/span> (\n    &lt;div className=<span class=\"hljs-string\">'App'<\/span>&gt;\n      &lt;Header \/&gt;\n      &lt;div className=<span class=\"hljs-string\">'main-container'<\/span>&gt;\n        &lt;ContactForm dispatch={dispatch} \/&gt;\n        &lt;hr \/&gt;\n        {state.contacts.length &gt; <span class=\"hljs-number\">0<\/span> &amp;&amp; &lt;ContactList contacts={state.contacts} \/&gt;}\n      &lt;<span class=\"hljs-regexp\">\/div&gt;\n    &lt;\/<\/span>div&gt;\n  );\n}\n<span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-keyword\">default<\/span> App;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-42\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In the above code, we&#8217;re checking If the <code>contacts<\/code> array in the <code>state<\/code> has a length greater than zero.<\/p>\n\n\n\n<p>If yes, then we&#8217;re passing the <code>contacts<\/code>` as a prop to the <code>ContactList<\/code> component.<\/p>\n\n\n\n<p>And as we have already declared <code>contacts<\/code> as a prop inside the <code>ContactListProps<\/code> interface of the <code>ContactList<\/code> component, we will not get any TypeScript error.<\/p>\n\n\n\n<p>Now, If you check the application, you can see the contact list displayed on the right side of the screen.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><noscript><img decoding=\"async\" width=\"800\" height=\"500\" src=\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/20_contact_list.gif\" alt=\"Contact List\" class=\"wp-image-28682\"><\/noscript><img decoding=\"async\" width=\"800\" height=\"500\" src=\"data:image\/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%20800%20500%22%3E%3C%2Fsvg%3E\" alt=\"Contact List\" class=\"wp-image-28682 lazyload\" data-src=\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/20_contact_list.gif\"><\/figure>\n\n\n\n<p>But If you check the console, you will see a warning in the console as shown below:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><noscript><img decoding=\"async\" width=\"1024\" height=\"351\" src=\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/21_key_warning-1024x351.png\" alt=\"Key Warning\" class=\"wp-image-28683\" srcset=\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/21_key_warning-1024x351.png 1024w, https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/21_key_warning-300x103.png 300w, https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/21_key_warning-768x263.png 768w, https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/21_key_warning-640x220.png 640w, https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/21_key_warning-150x51.png 150w, https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/21_key_warning.png 1440w\" sizes=\"(max-width: 1024px) 100vw, 1024px\"><\/noscript><img decoding=\"async\" width=\"1024\" height=\"351\" src=\"data:image\/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%201024%20351%22%3E%3C%2Fsvg%3E\" alt=\"Key Warning\" class=\"wp-image-28683 lazyload\" srcset=\"data:image\/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%201024%20351%22%3E%3C%2Fsvg%3E 1024w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" data-srcset=\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/21_key_warning-1024x351.png 1024w, https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/21_key_warning-300x103.png 300w, https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/21_key_warning-768x263.png 768w, https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/21_key_warning-640x220.png 640w, https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/21_key_warning-150x51.png 150w, https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/21_key_warning.png 1440w\" data-src=\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/21_key_warning-1024x351.png\"><\/figure>\n\n\n\n<p>If you don&#8217;t add a unique key while using the array map method during rendering, then it will cause a performance issue, If the order of the items displayed using the map method is changed.<\/p>\n\n\n\n<p>So to fix this, you might think of using the array <code>index<\/code> as the key in the <code>ContactList.tsx<\/code> file like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-43\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\">{\n  contacts.map(<span class=\"hljs-function\">(<span class=\"hljs-params\">{ <span class=\"hljs-params\">firstName<\/span>, <span class=\"hljs-params\">lastName<\/span>, <span class=\"hljs-params\">phone<\/span> }, <span class=\"hljs-params\">index<\/span><\/span>) =&gt;<\/span> (\n    &lt;tr key={index}&gt;\n      &lt;td&gt;{firstName}&lt;<span class=\"hljs-regexp\">\/td&gt;\n      &lt;td&gt;{lastName}&lt;\/<\/span>td&gt;\n      &lt;td&gt;{phone}&lt;<span class=\"hljs-regexp\">\/td&gt;\n    &lt;\/<\/span>tr&gt;\n  ));\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-43\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Even though it will fix the issue for now, it will cause an issue later once the order of the displayed contacts changes when we add the delete contact functionality.<\/p>\n\n\n\n<p>So to permanently fix this issue, we need to define a unique id for each contact that we can use as a key.<\/p>\n\n\n\n<p>## How to Add a Unique Id For Each Contact<\/p>\n\n\n\n<p>Open the <code>ContactForm.tsx<\/code> file and change the below code:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-44\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\">dispatch({\n  <span class=\"hljs-keyword\">type<\/span>: <span class=\"hljs-string\">'ADD_CONTACT'<\/span>,\n  payload: contact\n});<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-44\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>to this code:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-45\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\">dispatch({\n  <span class=\"hljs-keyword\">type<\/span>: <span class=\"hljs-string\">'ADD_CONTACT'<\/span>,\n  payload: {\n    id: <span class=\"hljs-built_in\">Date<\/span>.now(), <span class=\"hljs-comment\">\/\/ returns current timestamp<\/span>\n    ...contact\n  }\n});<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-45\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>So now, for the payload, we will have extra id property in addition to the <code>firstName<\/code>, <code>lastName<\/code>, and <code>phone<\/code> properties.<\/p>\n\n\n\n<p>But if you save the file, you will see the TypeScript error as shown below:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><noscript><img decoding=\"async\" width=\"1024\" height=\"783\" src=\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/22_id_error-1024x783.png\" alt=\"Id Error\" class=\"wp-image-28684\" srcset=\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/22_id_error-1024x783.png 1024w, https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/22_id_error-300x229.png 300w, https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/22_id_error-768x587.png 768w, https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/22_id_error-640x489.png 640w, https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/22_id_error-150x115.png 150w, https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/22_id_error.png 1083w\" sizes=\"(max-width: 1024px) 100vw, 1024px\"><\/noscript><img decoding=\"async\" width=\"1024\" height=\"783\" src=\"data:image\/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%201024%20783%22%3E%3C%2Fsvg%3E\" alt=\"Id Error\" class=\"wp-image-28684 lazyload\" srcset=\"data:image\/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%201024%20783%22%3E%3C%2Fsvg%3E 1024w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" data-srcset=\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/22_id_error-1024x783.png 1024w, https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/22_id_error-300x229.png 300w, https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/22_id_error-768x587.png 768w, https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/22_id_error-640x489.png 640w, https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/22_id_error-150x115.png 150w, https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/22_id_error.png 1083w\" data-src=\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/22_id_error-1024x783.png\"><\/figure>\n\n\n\n<p>As the error says, the <code>Contact<\/code> interface we defined in the <code>contactsReducer.ts<\/code> file does not include the <code>id<\/code> property.<\/p>\n\n\n\n<p>So let&#8217;s add that.<\/p>\n\n\n\n<p>Open the <code>contactsReducer.ts<\/code> file and add <code>id<\/code> to the <code>Contact<\/code> interface like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-46\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\"><span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-keyword\">interface<\/span> Contact {\n  id: <span class=\"hljs-built_in\">number<\/span>;\n  firstName: <span class=\"hljs-built_in\">string<\/span>;\n  lastName: <span class=\"hljs-built_in\">string<\/span>;\n  phone: <span class=\"hljs-built_in\">string<\/span>;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-46\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Now, the error from the <code>ContactForm.tsx<\/code> file will be gone.<\/p>\n\n\n\n<p>Now open the <code>ContactList.tsx<\/code> file and use <code>id<\/code> as the key as shown below:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-47\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\">{contacts.map(<span class=\"hljs-function\">(<span class=\"hljs-params\">{ <span class=\"hljs-params\">id<\/span>, <span class=\"hljs-params\">firstName<\/span>, <span class=\"hljs-params\">lastName<\/span>, <span class=\"hljs-params\">phone<\/span> }<\/span>) =&gt;<\/span> (\n  &lt;tr key={id}&gt;\n    &lt;td&gt;{firstName}&lt;<span class=\"hljs-regexp\">\/td&gt;\n    &lt;td&gt;{lastName}&lt;\/<\/span>td&gt;\n    &lt;td&gt;{phone}&lt;<span class=\"hljs-regexp\">\/td&gt;\n  &lt;\/<\/span>tr&gt;\n))}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-47\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>This is the correct way to assign a value for the key prop.<\/p>\n\n\n\n<p>So while using the array map method, always make sure to have a unique id for each individual item that is displayed.<\/p>\n\n\n\n<p>Now, If you check the application, you will not see any warning in the console after adding a contact.<\/p>\n\n\n\n<p>So now, we&#8217;re successfully able to add and display contacts on the screen.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Creating a Separate Component For Displaying Individual Contacts<\/h2>\n\n\n\n<p>When working in React, you should always create a separate component for displaying different information so it&#8217;s easy to re-use and test that component when required.<\/p>\n\n\n\n<p>So let&#8217;s create a <code>ContactItem.tsx<\/code> file inside the <code>components<\/code> folder and move the contents from the map method of the <code>ContactList.tsx<\/code> file inside it.<\/p>\n\n\n\n<p>So your <code>ContactItem.tsx<\/code> file will look like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-48\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\"><span class=\"hljs-keyword\">import<\/span> { FC } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'react'<\/span>;\n<span class=\"hljs-keyword\">import<\/span> { Contact } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'..\/reducer\/contactsReducer'<\/span>;\n<span class=\"hljs-keyword\">const<\/span> ContactItem: FC&lt;Contact&gt; = <span class=\"hljs-function\">(<span class=\"hljs-params\">{ <span class=\"hljs-params\">firstName<\/span>, <span class=\"hljs-params\">lastName<\/span>, <span class=\"hljs-params\">phone<\/span> }<\/span>) =&gt;<\/span> {\n  <span class=\"hljs-keyword\">return<\/span> (\n    &lt;tr&gt;\n      &lt;td&gt;{firstName}&lt;<span class=\"hljs-regexp\">\/td&gt;\n      &lt;td&gt;{lastName}&lt;\/<\/span>td&gt;\n      &lt;td&gt;{phone}&lt;<span class=\"hljs-regexp\">\/td&gt;\n    &lt;\/<\/span>tr&gt;\n  );\n};\n<span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-keyword\">default<\/span> ContactItem;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-48\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>And change the map method from the <code>ContactList.tsx<\/code> file from the below code:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-49\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\">{\n  contacts.map(<span class=\"hljs-function\">(<span class=\"hljs-params\">{ <span class=\"hljs-params\">id<\/span>, <span class=\"hljs-params\">firstName<\/span>, <span class=\"hljs-params\">lastName<\/span>, <span class=\"hljs-params\">phone<\/span> }<\/span>) =&gt;<\/span> (\n    &lt;tr key={id}&gt;\n      &lt;td&gt;{firstName}&lt;<span class=\"hljs-regexp\">\/td&gt;\n      &lt;td&gt;{lastName}&lt;\/<\/span>td&gt;\n      &lt;td&gt;{phone}&lt;<span class=\"hljs-regexp\">\/td&gt;\n    &lt;\/<\/span>tr&gt;\n  ));\n}\n<span class=\"hljs-string\">``<\/span><span class=\"hljs-string\">`\nto this code:\n`<\/span><span class=\"hljs-string\">``<\/span>js\n{\n  contacts.map(<span class=\"hljs-function\">(<span class=\"hljs-params\">{ <span class=\"hljs-params\">id<\/span>, <span class=\"hljs-params\">firstName<\/span>, <span class=\"hljs-params\">lastName<\/span>, <span class=\"hljs-params\">phone<\/span> }<\/span>) =&gt;<\/span> (\n    &lt;ContactItem\n      key={id}\n      id={id}\n      firstName={firstName}\n      lastName={lastName}\n      phone={phone}\n    \/&gt;\n  ));\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-49\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Note that, we have added a key to the <code>ContactItem<\/code> component instead of adding it to the <code>tr<\/code> tag.<\/p>\n\n\n\n<p>Also, import the <code>ContactItem<\/code> component at the top of the <code>ContactList.tsx<\/code> file like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-50\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\"><span class=\"hljs-keyword\">import<\/span> ContactItem <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'.\/ContactItem'<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-50\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In the above code, we&#8217;re passing individual props to the <code>ContactItem<\/code> component from inside the array map method.<\/p>\n\n\n\n<p>There is an easier way to achieve the same using the below code:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-51\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\">{\n  contacts.map(<span class=\"hljs-function\">(<span class=\"hljs-params\"><span class=\"hljs-params\">props<\/span><\/span>) =&gt;<\/span> &lt;ContactItem key={props.id} {...props} \/&gt;)\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-51\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Here, instead of inline destructuring, we&#8217;re using the <code>props<\/code> parameter and we&#8217;re spreading out all the properties of the props object using the spread operator.<\/p>\n\n\n\n<p>Now, If you check the application, you will see that it still works without any issues.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How to Create UI for Edit and Delete Functionality<\/h2>\n\n\n\n<p>For displaying the edit and delete button, let&#8217;s use the [react-icons](https:\/\/react-icons.github.io\/react-icons\/) library which is a popular way to include icons in your React app.<\/p>\n\n\n\n<p>We have already installed the <code>react-icons<\/code> npm package at the start of this article.<\/p>\n\n\n\n<p>Now, open the <code>ContactList.tsx<\/code> file and add two extra <code>th<\/code>  for <code>edit<\/code> and <code>delete<\/code> in the <code>thead<\/code> like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-52\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\">&lt;thead className=<span class=\"hljs-string\">'contacts-list-header'<\/span>&gt;\n  &lt;tr&gt;\n    &lt;th&gt;First Name&lt;<span class=\"hljs-regexp\">\/th&gt;\n    &lt;th&gt;Last Name&lt;\/<\/span>th&gt;\n    &lt;th&gt;Phone&lt;<span class=\"hljs-regexp\">\/th&gt;\n    &lt;th&gt;Edit&lt;\/<\/span>th&gt;\n    &lt;th&gt;Delete&lt;<span class=\"hljs-regexp\">\/th&gt;\n  &lt;\/<\/span>tr&gt;\n&lt;<span class=\"hljs-regexp\">\/thead&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-52\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Now, open the `<code>ContactItem.tsx<\/code>` file and replace it with the following contents:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-53\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\"><span class=\"hljs-keyword\">import<\/span> { FC } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'react'<\/span>;\n<span class=\"hljs-keyword\">import<\/span> { AiFillDelete, AiFillEdit } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'react-icons\/ai'<\/span>;\n<span class=\"hljs-keyword\">import<\/span> { Contact } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'..\/reducer\/contactsReducer'<\/span>;\n<span class=\"hljs-keyword\">const<\/span> ContactItem: FC&lt;Contact&gt; = <span class=\"hljs-function\">(<span class=\"hljs-params\">{ <span class=\"hljs-params\">firstName<\/span>, <span class=\"hljs-params\">lastName<\/span>, <span class=\"hljs-params\">phone<\/span> }<\/span>) =&gt;<\/span> {\n  <span class=\"hljs-keyword\">return<\/span> (\n    &lt;tr&gt;\n      &lt;td&gt;{firstName}&lt;<span class=\"hljs-regexp\">\/td&gt;\n      &lt;td&gt;{lastName}&lt;\/<\/span>td&gt;\n      &lt;td&gt;{phone}&lt;<span class=\"hljs-regexp\">\/td&gt;\n      &lt;td&gt;\n        &lt;AiFillEdit size={20} className='icon' \/<\/span>&gt;\n      &lt;<span class=\"hljs-regexp\">\/td&gt;\n      &lt;td&gt;\n        &lt;AiFillDelete size={20} className='icon' \/<\/span>&gt;\n      &lt;<span class=\"hljs-regexp\">\/td&gt;\n    &lt;\/<\/span>tr&gt;\n  );\n};\n<span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-keyword\">default<\/span> ContactItem;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-53\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Now, If you check the application, you can see the edit and delete icons in the contacts list table as shown below:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><noscript><img decoding=\"async\" width=\"1024\" height=\"473\" src=\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/23_edit_delete_icons-1024x473.png\" alt=\"Edit Delete Icons\" class=\"wp-image-28685\" srcset=\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/23_edit_delete_icons-1024x473.png 1024w, https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/23_edit_delete_icons-300x138.png 300w, https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/23_edit_delete_icons-768x354.png 768w, https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/23_edit_delete_icons-640x295.png 640w, https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/23_edit_delete_icons-150x69.png 150w, https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/23_edit_delete_icons.png 1428w\" sizes=\"(max-width: 1024px) 100vw, 1024px\"><\/noscript><img decoding=\"async\" width=\"1024\" height=\"473\" src=\"data:image\/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%201024%20473%22%3E%3C%2Fsvg%3E\" alt=\"Edit Delete Icons\" class=\"wp-image-28685 lazyload\" srcset=\"data:image\/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%201024%20473%22%3E%3C%2Fsvg%3E 1024w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" data-srcset=\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/23_edit_delete_icons-1024x473.png 1024w, https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/23_edit_delete_icons-300x138.png 300w, https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/23_edit_delete_icons-768x354.png 768w, https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/23_edit_delete_icons-640x295.png 640w, https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/23_edit_delete_icons-150x69.png 150w, https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/23_edit_delete_icons.png 1428w\" data-src=\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/23_edit_delete_icons-1024x473.png\"><\/figure>\n\n\n\n<p>Now let&#8217;s add the actual functionality to edit the contact.<\/p>\n\n\n\n<p>To display the edit modal popup, we will use the <a href=\"https:\/\/react-bootstrap.netlify.app\/components\/modal\/#rb-docs-content\" rel=\"nofollow noopener\">react-bootstrap modal component<\/a>&nbsp;<\/p>\n\n\n\n<p>So, create a new file <code>EditModal.tsx<\/code> inside the <code>components<\/code> folder with the following content inside it:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-54\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\"><span class=\"hljs-keyword\">import<\/span> { FC } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'react'<\/span>;\n<span class=\"hljs-keyword\">import<\/span> { Modal } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'react-bootstrap'<\/span>;\n<span class=\"hljs-keyword\">import<\/span> { Action, Contact } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'..\/reducer\/contactsReducer'<\/span>;\n<span class=\"hljs-keyword\">import<\/span> ContactForm <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'.\/ContactForm'<\/span>;\n<span class=\"hljs-keyword\">interface<\/span> EditModalProps {\n  showModal: <span class=\"hljs-built_in\">boolean<\/span>;\n  dataToEdit: Contact | <span class=\"hljs-literal\">undefined<\/span>;\n  toggleModal: <span class=\"hljs-function\"><span class=\"hljs-params\">()<\/span> =&gt;<\/span> <span class=\"hljs-built_in\">void<\/span>;\n  dispatch: React.Dispatch&lt;Action&gt;;\n}\n<span class=\"hljs-keyword\">const<\/span> EditModal: FC&lt;EditModalProps&gt; = ({\n  toggleModal,\n  dataToEdit,\n  showModal,\n  dispatch\n}) =&gt; {\n  <span class=\"hljs-keyword\">return<\/span> (\n    &lt;Modal show={showModal} onHide={toggleModal}&gt;\n      &lt;Modal.Header closeButton&gt;\n        &lt;Modal.Title&gt;Update Contact&lt;<span class=\"hljs-regexp\">\/Modal.Title&gt;\n      &lt;\/<\/span>Modal.Header&gt;\n      &lt;Modal.Body&gt;\n        &lt;ContactForm\n          dispatch={dispatch}\n          dataToEdit={dataToEdit}\n          toggleModal={toggleModal}\n        \/&gt;\n      &lt;<span class=\"hljs-regexp\">\/Modal.Body&gt;\n    &lt;\/<\/span>Modal&gt;\n  );\n};\n<span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-keyword\">default<\/span> EditModal;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-54\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>As you can see above, we&#8217;re re-using the <code>ContactForm<\/code> component inside the <code>Modal<\/code> component so as to avoid writing the same code again and again.<\/p>\n\n\n\n<p>The <code>EditModal<\/code> component accepts four props:<\/p>\n\n\n\n<p>&#8211; <code>toggleModal<\/code> which is a function that will toggle the state value used to hide or show the modal<\/p>\n\n\n\n<p>&#8211; <code>dataToEdit<\/code> will be an object containing contact details of the contact to be edited<\/p>\n\n\n\n<p>&#8211; <code>showModal<\/code> will be a boolean value indicating whether to show or hide the modal<\/p>\n\n\n\n<p>&#8211; <code>dispatch<\/code> function which will be helpful to dispatch the edit contact action<\/p>\n\n\n\n<p>Therefore, we have also declared the interface specifying all the props and their types:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-55\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\"><span class=\"hljs-keyword\">interface<\/span> EditModalProps {\n  showModal: <span class=\"hljs-built_in\">boolean<\/span>;\n  dataToEdit: Contact | <span class=\"hljs-literal\">undefined<\/span>;\n  toggleModal: <span class=\"hljs-function\"><span class=\"hljs-params\">()<\/span> =&gt;<\/span> <span class=\"hljs-built_in\">void<\/span>;\n  dispatch: React.Dispatch&lt;Action&gt;;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-55\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><span style=\"font-weight: 400;\">Right now, the <code>ContactForm<\/code> component accepts only the <code>dispatch<\/code> prop so let&#8217;s add the remaining required prop in the definition of the type.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">Open the <code>ContactForm.tsx<\/code> file and change the interface to the below code:<\/span><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-56\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\"><span class=\"hljs-keyword\">interface<\/span> ContactFormProps {\n  dispatch: React.Dispatch&lt;Action&gt;;\n  dataToEdit: Contact | <span class=\"hljs-literal\">undefined<\/span>;\n  toggleModal: <span class=\"hljs-function\"><span class=\"hljs-params\">()<\/span> =&gt;<\/span> <span class=\"hljs-built_in\">void<\/span>;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-56\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Also, add an import for the <code>Contact<\/code> and <code>Action<\/code> at the top of the file:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-57\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\"><span class=\"hljs-keyword\">import<\/span> { Action, Contact } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'..\/reducer\/contactsReducer'<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-57\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><span style=\"font-weight: 400;\">Update the <code>ContactForm<\/code> component to the below code:<\/span><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-58\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\"><span class=\"hljs-keyword\">const<\/span> ContactForm: FC&lt;ContactFormProps&gt; = ({\n  dispatch,\n  dataToEdit,\n  toggleModal\n}) =&gt; {\n  <span class=\"hljs-keyword\">const<\/span> &#091;contact, setContact] = useState({\n    firstName: dataToEdit?.firstName ? dataToEdit.firstName : <span class=\"hljs-string\">''<\/span>,\n    lastName: dataToEdit?.lastName ? dataToEdit.lastName : <span class=\"hljs-string\">''<\/span>,\n    phone: dataToEdit?.phone ? dataToEdit.phone : <span class=\"hljs-string\">''<\/span>\n  });\n  <span class=\"hljs-comment\">\/\/ ...<\/span>\n};<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-58\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In the above code, we&#8217;re accessing <code>dataToEdit<\/code> and <code>toggleModal<\/code> as props and in the <code>useState<\/code> hook, we&#8217;re checking for each property using the ternary operator to check If it exists.<\/p>\n\n\n\n<p>If it exists, then we&#8217;re using that value as the input field value otherwise we&#8217;re using an empty value.<\/p>\n\n\n\n<p>Now, change the <code>handleOnSubmit<\/code> method to the below code:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-59\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\"><span class=\"hljs-keyword\">const<\/span> handleOnSubmit = <span class=\"hljs-function\">(<span class=\"hljs-params\"><span class=\"hljs-params\">event<\/span>: <span class=\"hljs-params\">React<\/span>.<span class=\"hljs-params\">FormEvent<\/span>&lt;<span class=\"hljs-params\">HTMLFormElement<\/span>&gt;<\/span>) =&gt;<\/span> {\n  event.preventDefault();\n  <span class=\"hljs-keyword\">if<\/span> (!dataToEdit) {\n    dispatch({\n      <span class=\"hljs-keyword\">type<\/span>: <span class=\"hljs-string\">'ADD_CONTACT'<\/span>,\n      payload: {\n        id: <span class=\"hljs-built_in\">Date<\/span>.now(), <span class=\"hljs-comment\">\/\/ returns current timestamp<\/span>\n        ...contact\n      }\n    });\n  } <span class=\"hljs-keyword\">else<\/span> {\n    <span class=\"hljs-comment\">\/\/ dispatch edit contact action<\/span>\n    toggleModal();\n  }\n};<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-59\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>When the application is loaded and If we have not clicked on the <code>edit<\/code> icon, then the <code>dataToEdit<\/code> will be <code>undefined<\/code> otherwise it will include the details of the contact to be edited.<\/p>\n\n\n\n<p>So we&#8217;re first checking If we&#8217;re editing the contact. If not, then we&#8217;re dispatching the <code>ADD_CONTACT<\/code> action otherwise we&#8217;re just closing the modal for now.<\/p>\n\n\n\n<p>We will add code to dispatch edit action soon.<\/p>\n\n\n\n<p>Here, we&#8217;re calling the <code>toggleModal<\/code> function to close the modal once we dispatch the <code>ADD_CONTACT<\/code> action.<\/p>\n\n\n\n<p>Now, change the <code>Add Contact<\/code> button from this code:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-60\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\">&lt;Button variant=<span class=\"hljs-string\">'primary'<\/span> <span class=\"hljs-keyword\">type<\/span>=<span class=\"hljs-string\">'submit'<\/span> className=<span class=\"hljs-string\">'submit-btn'<\/span>&gt;\n  Add Contact\n&lt;<span class=\"hljs-regexp\">\/Button&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-60\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>to this code:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-61\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\">&lt;Button variant=<span class=\"hljs-string\">'primary'<\/span> <span class=\"hljs-keyword\">type<\/span>=<span class=\"hljs-string\">'submit'<\/span> className=<span class=\"hljs-string\">'submit-btn'<\/span>&gt;\n  {dataToEdit ? <span class=\"hljs-string\">'Update Contact'<\/span> : <span class=\"hljs-string\">'Add Contact'<\/span>}\n&lt;<span class=\"hljs-regexp\">\/Button&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-61\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>So, by default we will show the <code>Add Contact<\/code> button text for the contact form and If we click on the edit icon, we will set the <code>dataToEdit<\/code> prop value so we will see the <code>Update Contact<\/code> text in the modal while editing the contact.<\/p>\n\n\n\n<p>Now, open the <code>App.tsx<\/code> file and replace it with the following content:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-62\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\"><span class=\"hljs-keyword\">import<\/span> { useEffect, useReducer, useState } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'react'<\/span>;\n<span class=\"hljs-keyword\">import<\/span> ContactForm <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'.\/components\/ContactForm'<\/span>;\n<span class=\"hljs-keyword\">import<\/span> ContactList <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'.\/components\/ContactList'<\/span>;\n<span class=\"hljs-keyword\">import<\/span> EditModal <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'.\/components\/EditModal'<\/span>;\n<span class=\"hljs-keyword\">import<\/span> Header <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'.\/components\/Header'<\/span>;\n<span class=\"hljs-keyword\">import<\/span> { Contact, contactsReducer, State } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'.\/reducer\/contactsReducer'<\/span>;\n<span class=\"hljs-keyword\">const<\/span> initialState: State = {\n  contacts: &#091;]\n};\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">App<\/span>(<span class=\"hljs-params\"><\/span>) <\/span>{\n  <span class=\"hljs-keyword\">const<\/span> &#091;state, dispatch] = useReducer(contactsReducer, initialState);\n  <span class=\"hljs-keyword\">const<\/span> &#091;showModal, setShowModal] = useState(<span class=\"hljs-literal\">false<\/span>);\n  <span class=\"hljs-keyword\">const<\/span> &#091;dataToEdit, setDataToEdit] = useState&lt;Contact | <span class=\"hljs-literal\">undefined<\/span>&gt;(<span class=\"hljs-literal\">undefined<\/span>);\n  useEffect(<span class=\"hljs-function\"><span class=\"hljs-params\">()<\/span> =&gt;<\/span> {\n    <span class=\"hljs-keyword\">if<\/span> (!showModal) {\n      setDataToEdit(<span class=\"hljs-literal\">undefined<\/span>);\n    }\n  }, &#091;showModal]);\n  <span class=\"hljs-keyword\">const<\/span> toggleModal = <span class=\"hljs-function\"><span class=\"hljs-params\">()<\/span> =&gt;<\/span> {\n    setShowModal(<span class=\"hljs-function\">(<span class=\"hljs-params\"><span class=\"hljs-params\">show<\/span><\/span>) =&gt;<\/span> !show);\n  };\n  <span class=\"hljs-keyword\">const<\/span> handleEdit = <span class=\"hljs-function\">(<span class=\"hljs-params\"><span class=\"hljs-params\">id<\/span>: <span class=\"hljs-params\">number<\/span><\/span>) =&gt;<\/span> {\n    setDataToEdit(state.contacts.find(<span class=\"hljs-function\">(<span class=\"hljs-params\"><span class=\"hljs-params\">contact<\/span><\/span>) =&gt;<\/span> contact.id === id));\n    toggleModal();\n  };\n  <span class=\"hljs-keyword\">return<\/span> (\n    &lt;div className=<span class=\"hljs-string\">'App'<\/span>&gt;\n      &lt;Header \/&gt;\n      &lt;div className=<span class=\"hljs-string\">'main-container'<\/span>&gt;\n        &lt;ContactForm\n          dispatch={dispatch}\n          dataToEdit={dataToEdit}\n          toggleModal={toggleModal}\n        \/&gt;\n        &lt;hr \/&gt;\n        {state.contacts.length &gt; <span class=\"hljs-number\">0<\/span> &amp;&amp; (\n          &lt;ContactList\n            contacts={state.contacts}\n            handleEdit={handleEdit}\n            dispatch={dispatch}\n          \/&gt;\n        )}\n      &lt;<span class=\"hljs-regexp\">\/div&gt;\n      &lt;EditModal\n        showModal={showModal}\n        dataToEdit={dataToEdit}\n        toggleModal={toggleModal}\n        dispatch={dispatch}\n      \/<\/span>&gt;\n    &lt;<span class=\"hljs-regexp\">\/div&gt;\n  );\n}\nexport default App;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-62\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In the above code, we have declared two extra states:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-63\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\"><span class=\"hljs-keyword\">const<\/span> &#091;showModal, setShowModal] = useState(<span class=\"hljs-literal\">false<\/span>);\n<span class=\"hljs-keyword\">const<\/span> &#091;dataToEdit, setDataToEdit] = useState&lt;Contact | <span class=\"hljs-literal\">undefined<\/span>&gt;(<span class=\"hljs-literal\">undefined<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-63\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><span style=\"font-weight: 400;\">The first <code>useState<\/code> will handle the showing or hiding of the modal and the second <code>useState<\/code> will hold the value of the edited contact which will be an object.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">The object will be <code>undefined<\/code> initially and If the value is set, it will be of type <code>Contact<\/code> with <code>id<\/code>, <code>firstName<\/code>, <code>lastName<\/code>, and <code>phone<\/code> properties.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">That&#8217;s why we have declared the type initially using angle brackets so we will not get any TypeScript error in the future while using <code>dataToEdit<\/code> or <code>setDataToEdit<\/code>.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">Next, in the <code>useEffect<\/code> hook, we&#8217;re setting the <code>dataToEdit<\/code> value back to <code>undefined<\/code> once we close the modal i.e when <code>showModal<\/code> value is <code>false<\/code>.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">Then, in the <code>toggleModal<\/code> function we&#8217;re using updater syntax of state to change the value of <code>showModal<\/code> state from <code>true<\/code> to <code>false<\/code> and vice-versa.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">So when we call the <code>toggleModal<\/code> function, If the modal is already open because <code>showModal<\/code> is <code>true<\/code> then the modal will be closed and if it&#8217;s already closed then calling the <code>toggleModal<\/code> function will display that modal.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">If you were curious, why we declared the <code>dataToEdit<\/code> state type as <code>Contact | undefined<\/code> like this:<\/span><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-64\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\"><span class=\"hljs-keyword\">const<\/span> &#091;dataToEdit, setDataToEdit] = useState&lt;Contact | <span class=\"hljs-literal\">undefined<\/span>&gt;(<span class=\"hljs-literal\">undefined<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-64\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Then the answer lies in the <code>handleEdit<\/code> function:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-65\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\"><span class=\"hljs-keyword\">const<\/span> handleEdit = <span class=\"hljs-function\">(<span class=\"hljs-params\"><span class=\"hljs-params\">id<\/span>: <span class=\"hljs-params\">number<\/span><\/span>) =&gt;<\/span> {\n  setDataToEdit(state.contacts.find(<span class=\"hljs-function\">(<span class=\"hljs-params\"><span class=\"hljs-params\">contact<\/span><\/span>) =&gt;<\/span> contact.id === id));\n  toggleModal();\n};<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-65\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Inside the <code>handleEdit<\/code> function, we&#8217;re checking If the passed contact id matches with any of the already added contacts in the state and we&#8217;re setting it to the <code>dataToEdit<\/code> value using the <code>setDataToEdit<\/code> method.<\/p>\n\n\n\n<p>The array <code>find<\/code> method returns <code>undefined<\/code> if it couldn&#8217;t find the match.<\/p>\n\n\n\n<p>So TypeScript will throw an error If we don&#8217;t define `undefined` as one of the values for the <code>dataToEdit<\/code> state.<\/p>\n\n\n\n<p>That&#8217;s the reason, we included `undefined` as one of the values of data stored in the <code>dataToEdit<\/code> state variable.<\/p>\n\n\n\n<p>As you can see in the <code>App.tsx<\/code> file, we&#8217;re passing <code>dispatch<\/code> and <code>handleEdit<\/code> as props to the <code>ContactList<\/code> component.<\/p>\n\n\n\n<p>So let&#8217;s accept them in the <code>ContactList<\/code> component.<\/p>\n\n\n\n<p>Open the <code>ContactList.tsx<\/code> file and replace it with the following contents:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-66\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\"><span class=\"hljs-keyword\">import<\/span> { FC } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'react'<\/span>;\n<span class=\"hljs-keyword\">import<\/span> { Action, Contact } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'..\/reducer\/contactsReducer'<\/span>;\n<span class=\"hljs-keyword\">import<\/span> ContactItem <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'.\/ContactItem'<\/span>;\n<span class=\"hljs-keyword\">interface<\/span> ContactListProps {\n  contacts: Contact&#091;];\n  handleEdit: <span class=\"hljs-function\">(<span class=\"hljs-params\"><span class=\"hljs-params\">id<\/span>: <span class=\"hljs-params\">number<\/span><\/span>) =&gt;<\/span> <span class=\"hljs-built_in\">void<\/span>;\n  dispatch: React.Dispatch&lt;Action&gt;;\n}\n<span class=\"hljs-keyword\">const<\/span> ContactList: FC&lt;ContactListProps&gt; = ({\n  contacts,\n  handleEdit,\n  dispatch\n}) =&gt; {\n  <span class=\"hljs-keyword\">return<\/span> (\n    &lt;div className=<span class=\"hljs-string\">'contacts-list'<\/span>&gt;\n      &lt;h3 className=<span class=\"hljs-string\">'contacts-list-title'<\/span>&gt;List of Contacts&lt;<span class=\"hljs-regexp\">\/h3&gt;\n      &lt;div className='contacts-list-table-container'&gt;\n        &lt;table className='contacts-list-table'&gt;\n          &lt;thead className='contacts-list-header'&gt;\n            &lt;tr&gt;\n              &lt;th&gt;First Name&lt;\/<\/span>th&gt;\n              &lt;th&gt;Last Name&lt;<span class=\"hljs-regexp\">\/th&gt;\n              &lt;th&gt;Phone&lt;\/<\/span>th&gt;\n              &lt;th&gt;Edit&lt;<span class=\"hljs-regexp\">\/th&gt;\n              &lt;th&gt;Delete&lt;\/<\/span>th&gt;\n            &lt;<span class=\"hljs-regexp\">\/tr&gt;\n          &lt;\/<\/span>thead&gt;\n          &lt;tbody&gt;\n            {contacts.map(<span class=\"hljs-function\">(<span class=\"hljs-params\"><span class=\"hljs-params\">contact<\/span><\/span>) =&gt;<\/span> (\n              &lt;ContactItem\n                key={contact.id}\n                {...contact}\n                handleEdit={handleEdit}\n                dispatch={dispatch}\n              \/&gt;\n            ))}\n          &lt;<span class=\"hljs-regexp\">\/tbody&gt;\n        &lt;\/<\/span>table&gt;\n      &lt;<span class=\"hljs-regexp\">\/div&gt;\n    &lt;\/<\/span>div&gt;\n  );\n};\n<span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-keyword\">default<\/span> ContactList;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-66\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>As we know, <code>handleEdit<\/code> will accept the <code>id<\/code> as a parameter and it does not return anything, therefore, we&#8217;ve defined it in the <code>ContactListProps<\/code> interface as shown below:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-67\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\"><span class=\"hljs-keyword\">interface<\/span> ContactListProps {\n  <span class=\"hljs-comment\">\/\/ ...<\/span>\n  handleEdit: <span class=\"hljs-function\">(<span class=\"hljs-params\"><span class=\"hljs-params\">id<\/span>: <span class=\"hljs-params\">number<\/span><\/span>) =&gt;<\/span> <span class=\"hljs-built_in\">void<\/span>;\n  <span class=\"hljs-comment\">\/\/ ...<\/span>\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-67\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Then we&#8217;re taking out the <code>handleEdit<\/code> and <code>dispatch<\/code> as props and we&#8217;re passing them to the <code>ContactItem<\/code> component as shown below:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-68\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\">&lt;ContactItem\n  key={contact.id}\n  {...contact}\n  handleEdit={handleEdit}\n  dispatch={dispatch}\n\/&gt;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-68\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Now, open the <code>ContactItem.tsx<\/code> file and replace it with the following content:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-69\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\"><span class=\"hljs-keyword\">import<\/span> { FC } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'react'<\/span>;\n<span class=\"hljs-keyword\">import<\/span> { AiFillDelete, AiFillEdit } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'react-icons\/ai'<\/span>;\n<span class=\"hljs-keyword\">import<\/span> { Action, Contact } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'..\/reducer\/contactsReducer'<\/span>;\n<span class=\"hljs-keyword\">interface<\/span> ExtraProps {\n  handleEdit: <span class=\"hljs-function\">(<span class=\"hljs-params\"><span class=\"hljs-params\">id<\/span>: <span class=\"hljs-params\">number<\/span><\/span>) =&gt;<\/span> <span class=\"hljs-built_in\">void<\/span>;\n  dispatch: React.Dispatch&lt;Action&gt;;\n}\n<span class=\"hljs-keyword\">const<\/span> ContactItem: FC&lt;Contact &amp; ExtraProps&gt; = ({\n  id,\n  firstName,\n  lastName,\n  phone,\n  handleEdit,\n  dispatch\n}) =&gt; {\n  <span class=\"hljs-keyword\">return<\/span> (\n    &lt;tr&gt;\n      &lt;td&gt;{firstName}&lt;<span class=\"hljs-regexp\">\/td&gt;\n      &lt;td&gt;{lastName}&lt;\/<\/span>td&gt;\n      &lt;td&gt;{phone}&lt;<span class=\"hljs-regexp\">\/td&gt;\n      &lt;td&gt;\n        &lt;AiFillEdit size={20} onClick={() =&gt; handleEdit(id)} className='icon' \/<\/span>&gt;\n      &lt;<span class=\"hljs-regexp\">\/td&gt;\n      &lt;td&gt;\n        &lt;AiFillDelete\n          size={20}\n          onClick={() =&gt; {\n            const confirmDelete = window.confirm(\n              `Are you sure you want to delete contact for user ${firstName} ${lastName}?`\n            );\n            if (confirmDelete) {\n              \/<\/span><span class=\"hljs-regexp\">\/ dispatch action\n            }\n          }}\n          className='icon'\n        \/<\/span>&gt;\n      &lt;<span class=\"hljs-regexp\">\/td&gt;\n    &lt;\/<\/span>tr&gt;\n  );\n};\n<span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-keyword\">default<\/span> ContactItem;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-69\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Previously, the <code>ContactItem<\/code> component was only accepting properties from the <code>Contact<\/code> interface so we declared the <code>ContactItem<\/code> component like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-70\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\"><span class=\"hljs-keyword\">const<\/span> ContactItem: FC&lt;Contact&gt; = (\n  {\n    <span class=\"hljs-comment\">\/\/ ...<\/span>\n  }\n) =&gt; {\n  <span class=\"hljs-comment\">\/\/ ...<\/span>\n};<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-70\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>But now, we&#8217;re also passing <code>handleEdit<\/code> and <code>dispatch<\/code> props, so we have declared an additional interface like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-71\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\"><span class=\"hljs-keyword\">interface<\/span> ExtraProps {\n  handleEdit: <span class=\"hljs-function\">(<span class=\"hljs-params\"><span class=\"hljs-params\">id<\/span>: <span class=\"hljs-params\">number<\/span><\/span>) =&gt;<\/span> <span class=\"hljs-built_in\">void<\/span>;\n  dispatch: React.Dispatch&lt;Action&gt;;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-71\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>And we&#8217;re using the TypeScript intersection operator <code>&amp;<\/code> to combine types together like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-72\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\"><span class=\"hljs-keyword\">const<\/span> ContactItem: FC&lt;Contact &amp; ExtraProps&gt; = (\n  {\n    <span class=\"hljs-comment\">\/\/ ...<\/span>\n  }\n) =&gt; {\n  <span class=\"hljs-comment\">\/\/ ...<\/span>\n};<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-72\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>If you want, you can also create a new type and use it like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-73\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\"><span class=\"hljs-keyword\">interface<\/span> ExtraProps {\n  handleEdit: <span class=\"hljs-function\">(<span class=\"hljs-params\"><span class=\"hljs-params\">id<\/span>: <span class=\"hljs-params\">number<\/span><\/span>) =&gt;<\/span> <span class=\"hljs-built_in\">void<\/span>;\n  dispatch: React.Dispatch&lt;Action&gt;;\n}\n<span class=\"hljs-keyword\">type<\/span> ContactProps = ContactType &amp; ExtraProps;\n<span class=\"hljs-keyword\">const<\/span> Contact: FC&lt;ContactProps&gt; = (\n  {\n    <span class=\"hljs-comment\">\/\/ ...<\/span>\n  }\n) =&gt; {\n  <span class=\"hljs-comment\">\/\/ ...<\/span>\n};<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-73\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Now, If you check the application, you can see that we&#8217;re able to see the contact details of the contact we&#8217;re editing in the modal as shown below:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><noscript><img decoding=\"async\" width=\"800\" height=\"500\" src=\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/24_edit_modal.gif\" alt=\"Edit Modal\" class=\"wp-image-28686\"><\/noscript><img decoding=\"async\" width=\"800\" height=\"500\" src=\"data:image\/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%20800%20500%22%3E%3C%2Fsvg%3E\" alt=\"Edit Modal\" class=\"wp-image-28686 lazyload\" data-src=\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/24_edit_modal.gif\"><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">How to Implement Edit Contact Functionality<\/h2>\n\n\n\n<p>Now, we have the UI for edit and delete, let&#8217;s add code for actual edit and delete functionality.<\/p>\n\n\n\n<p>Open the <code>contactsReducer.ts<\/code> file and add a switch case for the <code>UPDATE_CONTACT<\/code> action.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-74\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\"><span class=\"hljs-keyword\">case<\/span> <span class=\"hljs-string\">'UPDATE_CONTACT'<\/span>:\n  <span class=\"hljs-keyword\">const<\/span> { id, updates } = action.payload;\n  <span class=\"hljs-keyword\">return<\/span> {\n    ...state,\n    contacts: state.contacts.map(<span class=\"hljs-function\">(<span class=\"hljs-params\"><span class=\"hljs-params\">contact<\/span><\/span>) =&gt;<\/span> {\n      <span class=\"hljs-keyword\">if<\/span> (contact.id === id) {\n        <span class=\"hljs-keyword\">return<\/span> {\n          ...contact,\n          ...updates\n        };\n      }\n      <span class=\"hljs-keyword\">return<\/span> contact;\n    })\n  };<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-74\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>As you can see above, we&#8217;re sending the <code>id<\/code> of the contact to update and the actual <code>updates<\/code> object which contains the updated data.<\/p>\n\n\n\n<p>And we&#8217;re using the array map method to update the contact with a matching id.<\/p>\n\n\n\n<p>But after saving the file, you can see that, we&#8217;re getting a TypeScript error after adding this case.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><noscript><img decoding=\"async\" width=\"993\" height=\"868\" src=\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/25_update_switch_error.png\" alt=\"Update Switch Error\" class=\"wp-image-28687\" srcset=\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/25_update_switch_error.png 993w, https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/25_update_switch_error-300x262.png 300w, https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/25_update_switch_error-768x671.png 768w, https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/25_update_switch_error-640x559.png 640w, https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/25_update_switch_error-150x131.png 150w\" sizes=\"(max-width: 993px) 100vw, 993px\"><\/noscript><img decoding=\"async\" width=\"993\" height=\"868\" src=\"data:image\/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%20993%20868%22%3E%3C%2Fsvg%3E\" alt=\"Update Switch Error\" class=\"wp-image-28687 lazyload\" srcset=\"data:image\/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%20993%20868%22%3E%3C%2Fsvg%3E 993w\" sizes=\"(max-width: 993px) 100vw, 993px\" data-srcset=\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/25_update_switch_error.png 993w, https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/25_update_switch_error-300x262.png 300w, https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/25_update_switch_error-768x671.png 768w, https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/25_update_switch_error-640x559.png 640w, https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/25_update_switch_error-150x131.png 150w\" data-src=\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/25_update_switch_error.png\"><\/figure>\n\n\n\n<p>The error is coming because, in the <code>Action<\/code> interface, we mentioned that the <code>payload<\/code> is of type <code>Contact<\/code> with <code>id<\/code>, <code>firstName<\/code>, <code>lastName<\/code>, and <code>phone<\/code> properties.<\/p>\n\n\n\n<p>But in the <code>UPDATE_CONTACT<\/code> switch case, we&#8217;re destructuring the <code>id<\/code> and <code>updates<\/code> properties because that&#8217;s what we need.<\/p>\n\n\n\n<p>So to fix the error, we need to tell TypeScript that, the payload can either be of <code>Contact<\/code> type or a type which has <code>id<\/code> and <code>updates<\/code> properties.<\/p>\n\n\n\n<p>Therefore, create a new interface above the <code>Action<\/code> as shown below:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-75\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\"><span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-keyword\">interface<\/span> Update {\n  id: <span class=\"hljs-built_in\">number<\/span>;\n  updates: Contact;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-75\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>And change the <code>Action<\/code> interface from this code:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-76\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\"><span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-keyword\">interface<\/span> Action {\n  <span class=\"hljs-keyword\">type<\/span>: <span class=\"hljs-string\">'ADD_CONTACT'<\/span>\n  payload: Contact;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-76\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>to this code:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-77\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\"><span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-keyword\">interface<\/span> Action {\n  <span class=\"hljs-keyword\">type<\/span>: <span class=\"hljs-string\">'ADD_CONTACT'<\/span> | <span class=\"hljs-string\">'UPDATE_CONTACT'<\/span>\n  payload: Contact | Update;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-77\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>So now, <code>payload<\/code> can be either of <code>Contact<\/code> type or <code>Update<\/code> type and <code>type<\/code> can be either <code>ADD_CONTACT<\/code> or <code>UPDATE_CONTACT<\/code>.<\/p>\n\n\n\n<p>But after making this change, you will see other TypeScript errors as shown below:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><noscript><img decoding=\"async\" width=\"800\" height=\"500\" src=\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/26_update_error.gif\" alt=\"Update Error\" class=\"wp-image-28688\"><\/noscript><img decoding=\"async\" width=\"800\" height=\"500\" src=\"data:image\/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%20800%20500%22%3E%3C%2Fsvg%3E\" alt=\"Update Error\" class=\"wp-image-28688 lazyload\" data-src=\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/26_update_error.gif\"><\/figure>\n\n\n\n<p>The errors are coming because TypeScript is not able to differentiate whether to use <code>Contact<\/code> or <code>Update<\/code> as the payload type.<\/p>\n\n\n\n<p>So change <code>ADD_CONTACT<\/code> switch case from the below code:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-78\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\"><span class=\"hljs-keyword\">case<\/span> <span class=\"hljs-string\">'ADD_CONTACT'<\/span>:\n  <span class=\"hljs-keyword\">return<\/span> {\n    ...state,\n    contacts: &#091;...state.contacts, action.payload]\n  };\n<span class=\"hljs-string\">``<\/span><span class=\"hljs-string\">`\nto this code:\n`<\/span><span class=\"hljs-string\">``<\/span>js\n<span class=\"hljs-keyword\">case<\/span> <span class=\"hljs-string\">'ADD_CONTACT'<\/span>:\n  <span class=\"hljs-keyword\">return<\/span> {\n    ...state,\n    contacts: &#091;...state.contacts, action.payload <span class=\"hljs-keyword\">as<\/span> Contact]\n  };<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-78\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Here, we&#8217;re doing type casting using the <code>as<\/code> keyword to tell TypeScript the type of <code>action.payload<\/code> specifically.<\/p>\n\n\n\n<p>And now change the <code>UPDATE_CONTACT<\/code> switch case from the below code:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-79\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\"><span class=\"hljs-keyword\">case<\/span> <span class=\"hljs-string\">'UPDATE_CONTACT'<\/span>:\n  <span class=\"hljs-keyword\">const<\/span> { id, updates } = action.payload;\n    <span class=\"hljs-keyword\">return<\/span> {\n      ...state,\n      contacts: state.contacts.map(<span class=\"hljs-function\">(<span class=\"hljs-params\"><span class=\"hljs-params\">contact<\/span><\/span>) =&gt;<\/span> {\n        <span class=\"hljs-keyword\">if<\/span> (contact.id === id) {\n          <span class=\"hljs-keyword\">return<\/span> {\n            ...contact,\n            ...updates\n          };\n        }\n        <span class=\"hljs-keyword\">return<\/span> contact;\n      })\n    };<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-79\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>to this code:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-80\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\"><span class=\"hljs-keyword\">case<\/span> <span class=\"hljs-string\">'UPDATE_CONTACT'<\/span>:\n  <span class=\"hljs-keyword\">const<\/span> { id, updates } = action.payload <span class=\"hljs-keyword\">as<\/span> Update;\n    <span class=\"hljs-keyword\">return<\/span> {\n      ...state,\n      contacts: state.contacts.map(<span class=\"hljs-function\">(<span class=\"hljs-params\"><span class=\"hljs-params\">contact<\/span><\/span>) =&gt;<\/span> {\n        <span class=\"hljs-keyword\">if<\/span> (contact.id === id) {\n          <span class=\"hljs-keyword\">return<\/span> {\n            ...contact,\n            ...updates\n          };\n        }\n        <span class=\"hljs-keyword\">return<\/span> contact;\n      })\n    };<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-80\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Here, we have specified the <code>action.payload<\/code> type as <code>Update<\/code>.<\/p>\n\n\n\n<p>Now, you can see there are no TypeScript errors coming.<\/p>\n\n\n\n<p>So your entire <code>contactsReducer.ts<\/code> file will look like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-81\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\"><span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-keyword\">interface<\/span> Contact {\n  id: <span class=\"hljs-built_in\">number<\/span>;\n  firstName: <span class=\"hljs-built_in\">string<\/span>;\n  lastName: <span class=\"hljs-built_in\">string<\/span>;\n  phone: <span class=\"hljs-built_in\">string<\/span>;\n}\n<span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-keyword\">interface<\/span> Update {\n  id: <span class=\"hljs-built_in\">number<\/span>;\n  updates: Contact;\n}\n<span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-keyword\">interface<\/span> Action {\n  <span class=\"hljs-keyword\">type<\/span>: <span class=\"hljs-string\">'ADD_CONTACT'<\/span> | <span class=\"hljs-string\">'UPDATE_CONTACT'<\/span>\n  payload: Contact | Update;\n}\n<span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-keyword\">interface<\/span> State {\n  contacts: Contact&#091;];\n}\n<span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-keyword\">const<\/span> contactsReducer = (state: State, action: Action): <span class=\"hljs-function\"><span class=\"hljs-params\">State<\/span> =&gt;<\/span> {\n  <span class=\"hljs-keyword\">switch<\/span> (action.type) {\n    <span class=\"hljs-keyword\">case<\/span> <span class=\"hljs-string\">'ADD_CONTACT'<\/span>:\n      <span class=\"hljs-keyword\">return<\/span> {\n        ...state,\n        contacts: &#091;...state.contacts, action.payload <span class=\"hljs-keyword\">as<\/span> Contact]\n      };\n    <span class=\"hljs-keyword\">case<\/span> <span class=\"hljs-string\">'UPDATE_CONTACT'<\/span>:\n      <span class=\"hljs-keyword\">const<\/span> { id, updates } = action.payload <span class=\"hljs-keyword\">as<\/span> Update;\n      <span class=\"hljs-keyword\">return<\/span> {\n        ...state,\n        contacts: state.contacts.map(<span class=\"hljs-function\">(<span class=\"hljs-params\"><span class=\"hljs-params\">contact<\/span><\/span>) =&gt;<\/span> {\n          <span class=\"hljs-keyword\">if<\/span> (contact.id === id) {\n            <span class=\"hljs-keyword\">return<\/span> {\n              ...contact,\n              ...updates\n            };\n          }\n          <span class=\"hljs-keyword\">return<\/span> contact;\n        })\n      };\n    <span class=\"hljs-keyword\">default<\/span>:\n      <span class=\"hljs-keyword\">return<\/span> state;\n  }\n};<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-81\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Now, let&#8217;s dispatch the <code>UPDATE_CONTACT<\/code> action to actually update the contact.<\/p>\n\n\n\n<p>Open the <code>ContactForm.tsx<\/code> file and inside the <code>handleOnSubmit<\/code> method, in the else block, before the <code>toggleModal<\/code> function call, add the below code:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-82\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\">dispatch({\n  <span class=\"hljs-keyword\">type<\/span>: <span class=\"hljs-string\">'UPDATE_CONTACT'<\/span>,\n  payload: {\n    id: dataToEdit.id,\n    updates: {\n      id: <span class=\"hljs-built_in\">Date<\/span>.now(),\n      ...contact\n    }\n  }\n});<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-82\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>And inside the if condition after the dispatch call, add the below code:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-83\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\">setContact({\n  firstName: <span class=\"hljs-string\">''<\/span>,\n  lastName: <span class=\"hljs-string\">''<\/span>,\n  phone: <span class=\"hljs-string\">''<\/span>\n});<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-83\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Here, we&#8217;re resetting the form state, so once we dispatch the <code>ADD_CONTACT<\/code> action, we will clear out all the user-entered data.<\/p>\n\n\n\n<p>so your <code>handleOnSubmit<\/code> method will look like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-84\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\"><span class=\"hljs-keyword\">const<\/span> handleOnSubmit = <span class=\"hljs-function\">(<span class=\"hljs-params\"><span class=\"hljs-params\">event<\/span>: <span class=\"hljs-params\">React<\/span>.<span class=\"hljs-params\">FormEvent<\/span>&lt;<span class=\"hljs-params\">HTMLFormElement<\/span>&gt;<\/span>) =&gt;<\/span> {\n  event.preventDefault();\n  <span class=\"hljs-keyword\">if<\/span> (!dataToEdit) {\n    dispatch({\n      <span class=\"hljs-keyword\">type<\/span>: <span class=\"hljs-string\">'ADD_CONTACT'<\/span>,\n      payload: {\n        id: <span class=\"hljs-built_in\">Date<\/span>.now(), <span class=\"hljs-comment\">\/\/ returns current timestamp<\/span>\n        ...contact\n      }\n    });\n    setContact({\n      firstName: <span class=\"hljs-string\">''<\/span>,\n      lastName: <span class=\"hljs-string\">''<\/span>,\n      phone: <span class=\"hljs-string\">''<\/span>\n    });\n  } <span class=\"hljs-keyword\">else<\/span> {\n    dispatch({\n      <span class=\"hljs-keyword\">type<\/span>: <span class=\"hljs-string\">'UPDATE_CONTACT'<\/span>,\n      payload: {\n        id: dataToEdit.id,\n        updates: {\n          id: <span class=\"hljs-built_in\">Date<\/span>.now(),\n          ...contact\n        }\n      }\n    });\n    toggleModal();\n  }\n};<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-84\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Now, If you check the application, you can see that, we&#8217;re successfully able to edit the contact.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><noscript><img decoding=\"async\" width=\"800\" height=\"500\" src=\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/27_working_edit.gif\" alt=\"Working Edit\" class=\"wp-image-28689\"><\/noscript><img decoding=\"async\" width=\"800\" height=\"500\" src=\"data:image\/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%20800%20500%22%3E%3C%2Fsvg%3E\" alt=\"Working Edit\" class=\"wp-image-28689 lazyload\" data-src=\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/27_working_edit.gif\"><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">How to Implement Delete Contact Functionality<\/h2>\n\n\n\n<p>Now, let&#8217;s work on adding delete contact functionality.<\/p>\n\n\n\n<p>Open the <code>contactsReducer.ts<\/code> file and add a switch case for the <code>DELETE_CONTACT<\/code> action.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-85\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\"><span class=\"hljs-keyword\">case<\/span> <span class=\"hljs-string\">'DELETE_CONTACT'<\/span>: {\n    <span class=\"hljs-keyword\">const<\/span> { id } = action.payload;\n    <span class=\"hljs-keyword\">return<\/span> {\n      ...state,\n      contacts: state.contacts.filter(<span class=\"hljs-function\">(<span class=\"hljs-params\"><span class=\"hljs-params\">contact<\/span><\/span>) =&gt;<\/span> contact.id !== id)\n    };\n  }<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-85\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>For deleting a contact, we&#8217;re sending only the <code>id<\/code> of the contact to be deleted in the <code>payload<\/code> property and using the array filter method to remove that specific contact.<\/p>\n\n\n\n<p>Now, in the same file, change the <code>Action<\/code> interface to the following code:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-86\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\"><span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-keyword\">interface<\/span> Action {\n  <span class=\"hljs-keyword\">type<\/span>: <span class=\"hljs-string\">'ADD_CONTACT'<\/span> | <span class=\"hljs-string\">'UPDATE_CONTACT'<\/span> | <span class=\"hljs-string\">'DELETE_CONTACT'<\/span>\n  payload: Contact | Update;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-86\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Here, we&#8217;ve just added the <code>DELETE_CONTACT<\/code> as an extra type.<\/p>\n\n\n\n<p>Now, Open the <code>ContactItem.tsx<\/code> file, and inside the <code>confirmDelete<\/code> if condition dispatch the <code>DELETE_CONTACT<\/code> action as shown below:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-87\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\"><span class=\"hljs-keyword\">if<\/span> (confirmDelete) {\n  dispatch({\n    <span class=\"hljs-keyword\">type<\/span>: <span class=\"hljs-string\">'DELETE_CONTACT'<\/span>,\n    payload: { id }\n  });\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-87\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>But If you save the file, you will see a TypeScript error as shown below:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><noscript><img decoding=\"async\" width=\"1024\" height=\"779\" src=\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/28_delete_missing-1024x779.png\" alt=\"Delete Missing Error\" class=\"wp-image-28690\" srcset=\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/28_delete_missing-1024x779.png 1024w, https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/28_delete_missing-300x228.png 300w, https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/28_delete_missing-768x584.png 768w, https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/28_delete_missing-640x487.png 640w, https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/28_delete_missing-150x114.png 150w, https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/28_delete_missing.png 1158w\" sizes=\"(max-width: 1024px) 100vw, 1024px\"><\/noscript><img decoding=\"async\" width=\"1024\" height=\"779\" src=\"data:image\/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%201024%20779%22%3E%3C%2Fsvg%3E\" alt=\"Delete Missing Error\" class=\"wp-image-28690 lazyload\" srcset=\"data:image\/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%201024%20779%22%3E%3C%2Fsvg%3E 1024w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" data-srcset=\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/28_delete_missing-1024x779.png 1024w, https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/28_delete_missing-300x228.png 300w, https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/28_delete_missing-768x584.png 768w, https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/28_delete_missing-640x487.png 640w, https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/28_delete_missing-150x114.png 150w, https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/28_delete_missing.png 1158w\" data-src=\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/28_delete_missing-1024x779.png\"><\/figure>\n\n\n\n<p>We&#8217;re getting the error because, in the <code>Action<\/code> interface of the <code>contactsReducer.ts<\/code> file, we&#8217;re saying that the payload can be either <code>Contact<\/code> or <code>Update<\/code> but for payload, we&#8217;re just passing an object with <code>id<\/code> inside it for <code>DELETE_CONTACT<\/code> action and we&#8217;re missing the <code>updates<\/code> property.<\/p>\n\n\n\n<p>So to fix this error, change the <code>Update<\/code> interface from this code:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-88\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\"><span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-keyword\">interface<\/span> Update {\n  id: <span class=\"hljs-built_in\">number<\/span>;\n  updates: Contact;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-88\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>to this code:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-89\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\"><span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-keyword\">interface<\/span> Update {\n  id: <span class=\"hljs-built_in\">number<\/span>;\n  updates?: Contact;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-89\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Here, we&#8217;ve made the <code>updates<\/code> property optional by adding a question mark symbol. So TypeScript is happy now as we&#8217;re providing id and updates property can be ignored.<\/p>\n\n\n\n<p>With this change, the error in the <code>ContactItem.tsx<\/code> file will go away.<\/p>\n\n\n\n<p>Now, If you check the application, you can see that the delete contact functionality is working fine.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><noscript><img decoding=\"async\" width=\"800\" height=\"500\" src=\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/29_working_delete.gif\" alt=\"Working Delete\" class=\"wp-image-28691\"><\/noscript><img decoding=\"async\" width=\"800\" height=\"500\" src=\"data:image\/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%20800%20500%22%3E%3C%2Fsvg%3E\" alt=\"Working Delete\" class=\"wp-image-28691 lazyload\" data-src=\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/29_working_delete.gif\"><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">How to Add Form Validations<\/h2>\n\n\n\n<p>Now, we&#8217;re done with the CRUD (Create, Read, Update, and Delete) functionality of the application.<\/p>\n\n\n\n<p>But we&#8217;re missing the validation part while adding or updating the contact.<\/p>\n\n\n\n<p>So let&#8217;s add that.<\/p>\n\n\n\n<p>Declare a new state inside the <code>ContactForm.tsx<\/code> file:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-90\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\"><span class=\"hljs-keyword\">const<\/span> &#091;errorMsg, setErrorMsg] = useState(<span class=\"hljs-string\">''<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-90\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>And change the <code>handleOnSubmit<\/code> method to the below code:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-91\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\"><span class=\"hljs-keyword\">const<\/span> handleOnSubmit = <span class=\"hljs-function\">(<span class=\"hljs-params\"><span class=\"hljs-params\">event<\/span>: <span class=\"hljs-params\">React<\/span>.<span class=\"hljs-params\">FormEvent<\/span>&lt;<span class=\"hljs-params\">HTMLFormElement<\/span>&gt;<\/span>) =&gt;<\/span> {\n  event.preventDefault();\n  <span class=\"hljs-keyword\">const<\/span> { firstName, lastName, phone } = contact;\n  <span class=\"hljs-keyword\">if<\/span> (\n    firstName.trim() === <span class=\"hljs-string\">''<\/span> ||\n    lastName.trim() === <span class=\"hljs-string\">''<\/span> ||\n    phone.trim() === <span class=\"hljs-string\">''<\/span>\n  ) {\n    setErrorMsg(<span class=\"hljs-string\">'All the fields are required.'<\/span>);\n    <span class=\"hljs-keyword\">return<\/span>;\n  } <span class=\"hljs-keyword\">else<\/span> <span class=\"hljs-keyword\">if<\/span> (!phone.trim().match(<span class=\"hljs-regexp\">\/^\\d{10}$\/g<\/span>)) {\n    setErrorMsg(<span class=\"hljs-string\">'Please enter a valid 10 digit phone number.'<\/span>);\n    <span class=\"hljs-keyword\">return<\/span>;\n  }\n  <span class=\"hljs-keyword\">if<\/span> (!dataToEdit) {\n    dispatch({\n      <span class=\"hljs-keyword\">type<\/span>: <span class=\"hljs-string\">'ADD_CONTACT'<\/span>,\n      payload: {\n        id: <span class=\"hljs-built_in\">Date<\/span>.now(), <span class=\"hljs-comment\">\/\/ returns current timestamp<\/span>\n        ...contact\n      }\n    });\n    setContact({\n      firstName: <span class=\"hljs-string\">''<\/span>,\n      lastName: <span class=\"hljs-string\">''<\/span>,\n      phone: <span class=\"hljs-string\">''<\/span>\n    });\n    setErrorMsg(<span class=\"hljs-string\">''<\/span>);\n  } <span class=\"hljs-keyword\">else<\/span> {\n    dispatch({\n      <span class=\"hljs-keyword\">type<\/span>: <span class=\"hljs-string\">'UPDATE_CONTACT'<\/span>,\n      payload: {\n        id: dataToEdit.id,\n        updates: {\n          id: <span class=\"hljs-built_in\">Date<\/span>.now(),\n          ...contact\n        }\n      }\n    });\n    toggleModal();\n  }\n};<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-91\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><span style=\"font-weight: 400;\">In the above code, we&#8217;re checking If the user has entered all the values in the form.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">If any of the values are missing, then we&#8217;re setting the error message using the <code>setErrorMsg<\/code> function and we&#8217;re skipping the next code execution using the return statement.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">So the next code will not be executed If there is some error.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">We&#8217;re also checking If the phone number is having a length of 10 characters or not.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">And If it&#8217;s not an edit mode, then after dispatching the <code>ADD_CONTACT<\/code> action, we&#8217;re clearing the error message by calling <code>setErrorMsg('');<\/code><\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">So, If there was any previous error message, It will be cleared out, after the contact is added successfully.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">Now, let&#8217;s display the error message on the screen.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">while returning the JSX, inside the `Form` tag and before the <code>Form.Group<\/code> tag, add the following statement:<\/span><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-92\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\">{errorMsg &amp;&amp; &lt;p className=<span class=\"hljs-string\">'errorMsg'<\/span>&gt;{errorMsg}&lt;<span class=\"hljs-regexp\">\/p&gt;}<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-92\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>So your entire <code>ContactForm.tsx<\/code> file will look like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-93\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\"><span class=\"hljs-keyword\">import<\/span> { FC, useState } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'react'<\/span>;\n<span class=\"hljs-keyword\">import<\/span> { Button, Form } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'react-bootstrap'<\/span>;\n<span class=\"hljs-keyword\">import<\/span> { Action, Contact } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'..\/reducer\/contactsReducer'<\/span>;\n<span class=\"hljs-keyword\">interface<\/span> ContactFormProps {\n  dispatch: React.Dispatch&lt;Action&gt;;\n  dataToEdit: Contact | <span class=\"hljs-literal\">undefined<\/span>;\n  toggleModal: <span class=\"hljs-function\"><span class=\"hljs-params\">()<\/span> =&gt;<\/span> <span class=\"hljs-built_in\">void<\/span>;\n}\n<span class=\"hljs-keyword\">const<\/span> ContactForm: FC&lt;ContactFormProps&gt; = ({\n  dispatch,\n  dataToEdit,\n  toggleModal\n}) =&gt; {\n  <span class=\"hljs-keyword\">const<\/span> &#091;contact, setContact] = useState({\n    firstName: dataToEdit?.firstName ? dataToEdit.firstName : <span class=\"hljs-string\">''<\/span>,\n    lastName: dataToEdit?.lastName ? dataToEdit.lastName : <span class=\"hljs-string\">''<\/span>,\n    phone: dataToEdit?.phone ? dataToEdit.phone : <span class=\"hljs-string\">''<\/span>\n  });\n  <span class=\"hljs-keyword\">const<\/span> &#091;errorMsg, setErrorMsg] = useState(<span class=\"hljs-string\">''<\/span>);\n  <span class=\"hljs-keyword\">const<\/span> handleOnChange = <span class=\"hljs-function\">(<span class=\"hljs-params\"><span class=\"hljs-params\">event<\/span>: <span class=\"hljs-params\">React<\/span>.<span class=\"hljs-params\">ChangeEvent<\/span>&lt;<span class=\"hljs-params\">HTMLInputElement<\/span>&gt;<\/span>) =&gt;<\/span> {\n    <span class=\"hljs-keyword\">const<\/span> { name, value } = event.target;\n    setContact(<span class=\"hljs-function\">(<span class=\"hljs-params\"><span class=\"hljs-params\">prevState<\/span><\/span>) =&gt;<\/span> {\n      <span class=\"hljs-keyword\">return<\/span> {\n        ...prevState,\n        &#091;name]: value\n      };\n    });\n  };\n  <span class=\"hljs-keyword\">const<\/span> handleOnSubmit = <span class=\"hljs-function\">(<span class=\"hljs-params\"><span class=\"hljs-params\">event<\/span>: <span class=\"hljs-params\">React<\/span>.<span class=\"hljs-params\">FormEvent<\/span>&lt;<span class=\"hljs-params\">HTMLFormElement<\/span>&gt;<\/span>) =&gt;<\/span> {\n    event.preventDefault();\n    <span class=\"hljs-keyword\">const<\/span> { firstName, lastName, phone } = contact;\n    <span class=\"hljs-keyword\">if<\/span> (\n      firstName.trim() === <span class=\"hljs-string\">''<\/span> ||\n      lastName.trim() === <span class=\"hljs-string\">''<\/span> ||\n      phone.trim() === <span class=\"hljs-string\">''<\/span>\n    ) {\n      setErrorMsg(<span class=\"hljs-string\">'All the fields are required.'<\/span>);\n      <span class=\"hljs-keyword\">return<\/span>;\n    } <span class=\"hljs-keyword\">else<\/span> <span class=\"hljs-keyword\">if<\/span> (!phone.trim().match(<span class=\"hljs-regexp\">\/^\\d{10}$\/g<\/span>)) {\n      setErrorMsg(<span class=\"hljs-string\">'Please enter a valid 10 digit phone number.'<\/span>);\n      <span class=\"hljs-keyword\">return<\/span>;\n    }\n    <span class=\"hljs-keyword\">if<\/span> (!dataToEdit) {\n      dispatch({\n        <span class=\"hljs-keyword\">type<\/span>: <span class=\"hljs-string\">'ADD_CONTACT'<\/span>,\n        payload: {\n          id: <span class=\"hljs-built_in\">Date<\/span>.now(), <span class=\"hljs-comment\">\/\/ returns current timestamp<\/span>\n          ...contact\n        }\n      });\n      setContact({\n        firstName: <span class=\"hljs-string\">''<\/span>,\n        lastName: <span class=\"hljs-string\">''<\/span>,\n        phone: <span class=\"hljs-string\">''<\/span>\n      });\n      setErrorMsg(<span class=\"hljs-string\">''<\/span>);\n    } <span class=\"hljs-keyword\">else<\/span> {\n      dispatch({\n        <span class=\"hljs-keyword\">type<\/span>: <span class=\"hljs-string\">'UPDATE_CONTACT'<\/span>,\n        payload: {\n          id: dataToEdit.id,\n          updates: {\n            id: <span class=\"hljs-built_in\">Date<\/span>.now(),\n            ...contact\n          }\n        }\n      });\n      toggleModal();\n    }\n  };\n  <span class=\"hljs-keyword\">return<\/span> (\n    &lt;Form onSubmit={handleOnSubmit} className=<span class=\"hljs-string\">'contact-form'<\/span>&gt;\n      {errorMsg &amp;&amp; &lt;p className=<span class=\"hljs-string\">'errorMsg'<\/span>&gt;{errorMsg}&lt;<span class=\"hljs-regexp\">\/p&gt;}\n      &lt;Form.Group controlId='firstName'&gt;\n        &lt;Form.Label&gt;First Name&lt;\/<\/span>Form.Label&gt;\n        &lt;Form.Control\n          className=<span class=\"hljs-string\">'firstName'<\/span>\n          name=<span class=\"hljs-string\">'firstName'<\/span>\n          value={contact.firstName}\n          <span class=\"hljs-keyword\">type<\/span>=<span class=\"hljs-string\">'text'<\/span>\n          onChange={handleOnChange}\n        \/&gt;\n      &lt;<span class=\"hljs-regexp\">\/Form.Group&gt;\n      &lt;Form.Group controlId='lastName'&gt;\n        &lt;Form.Label&gt;Last Name&lt;\/<\/span>Form.Label&gt;\n        &lt;Form.Control\n          className=<span class=\"hljs-string\">'lastName'<\/span>\n          name=<span class=\"hljs-string\">'lastName'<\/span>\n          value={contact.lastName}\n          <span class=\"hljs-keyword\">type<\/span>=<span class=\"hljs-string\">'text'<\/span>\n          onChange={handleOnChange}\n        \/&gt;\n      &lt;<span class=\"hljs-regexp\">\/Form.Group&gt;\n      &lt;Form.Group controlId='phone'&gt;\n        &lt;Form.Label&gt;Phone&lt;\/<\/span>Form.Label&gt;\n        &lt;Form.Control\n          className=<span class=\"hljs-string\">'phone'<\/span>\n          name=<span class=\"hljs-string\">'phone'<\/span>\n          value={contact.phone}\n          <span class=\"hljs-keyword\">type<\/span>=<span class=\"hljs-string\">'number'<\/span>\n          onChange={handleOnChange}\n        \/&gt;\n      &lt;<span class=\"hljs-regexp\">\/Form.Group&gt;\n      &lt;Form.Group controlId='submit'&gt;\n        &lt;Button variant='primary' type='submit' className='submit-btn'&gt;\n          {dataToEdit ? 'Update Contact' : 'Add Contact'}\n        &lt;\/<\/span>Button&gt;\n      &lt;<span class=\"hljs-regexp\">\/Form.Group&gt;\n    &lt;\/<\/span>Form&gt;\n  );\n};\n<span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-keyword\">default<\/span> ContactForm;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-93\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><span style=\"font-weight: 400;\">Now, If you check the application, you can see the validation is working as expected.<\/span><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><noscript><img decoding=\"async\" width=\"800\" height=\"500\" src=\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/30_working_validations.gif\" alt=\"Working Validations\" class=\"wp-image-28692\"><\/noscript><img decoding=\"async\" width=\"800\" height=\"500\" src=\"data:image\/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%20800%20500%22%3E%3C%2Fsvg%3E\" alt=\"Working Validations\" class=\"wp-image-28692 lazyload\" data-src=\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/30_working_validations.gif\"><\/figure>\n\n\n\n<p>So with these changes, we&#8217;re done with our contact manager app built with React + TypeScript.<\/p>\n\n\n\n<p>Thanks for reading!<br>You can see a live demo of the deployed application <a href=\"https:\/\/contact-manager-app-14c32.web.app\/\" rel=\"nofollow noopener\">here<\/a>. Also, if you want to refer the code for this entire project, you can download it from <a href=\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/contact-manager-app-typescript.zip\" download>here<\/a>.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/mobisoftinfotech.com\/contact-us?utm_source=blog&amp;utm_medium=referral&amp;utm_campaign=learn-typescript-with-react-by-building-a-crud-application-cta1 \"><noscript><img decoding=\"async\" width=\"800\" height=\"340\" src=\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2025\/03\/cta2.jpg\" alt=\"React and TypeScript development services call to action\" class=\"wp-image-37688\"><\/noscript><img decoding=\"async\" width=\"800\" height=\"340\" src=\"data:image\/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%20800%20340%22%3E%3C%2Fsvg%3E\" alt=\"React and TypeScript development services call to action\" class=\"wp-image-37688 lazyload\" data-src=\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2025\/03\/cta2.jpg\"><\/a><\/figure>\n\n\n<div class=\"modern-author-card\">\n    <div class=\"author-card-content\">\n        <div class=\"author-info-section\">\n            <div class=\"author-avatar\">\n                <noscript><img decoding=\"async\" src=\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/11\/mobisoftteam.png\" alt=\"Mobisoft Team\"><\/noscript><img decoding=\"async\" src=\"data:image\/gif;base64,R0lGODlhAQABAIAAAAAAAP\/\/\/yH5BAEAAAAALAAAAAABAAEAAAIBRAA7\" alt=\"Mobisoft Team\" data-src=\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/11\/mobisoftteam.png\" class=\" lazyload\">\n            <\/div>\n            <div class=\"author-details\">\n                <h3 class=\"author-name\">Mobisoft Team<\/h3>\n                <p class=\"author-title\">Technology Team<\/p>\n                <a href=\"javascript:void(0);\" class=\"read-more-link read-more-btn\" onclick=\"toggleAuthorBio(this); return false;\">Read more <noscript><img decoding=\"async\" src=\"\/assets\/images\/blog\/Vector.png\" alt=\"expand\" class=\"read-more-arrow down-arrow\"><\/noscript><img decoding=\"async\" src=\"data:image\/gif;base64,R0lGODlhAQABAIAAAAAAAP\/\/\/yH5BAEAAAAALAAAAAABAAEAAAIBRAA7\" alt=\"expand\" class=\"read-more-arrow down-arrow lazyload\" data-src=\"\/assets\/images\/blog\/Vector.png\"><\/a>\n                <div class=\"author-bio-expanded\">\n                    <p>Get the latest insights, industry trends, and expert perspectives from the <a href=\"https:\/\/mobisoftinfotech.com?utm_source=blog&amp;utm_medium=internal_link&amp;utm_campaign=cloud-vs-dedicated-gpu-hosting-providers_blog&amp;utm_content=home-page\">Mobisoft Infotech<\/a> team. Stay updated with our teams collective knowledge, discoveries, and innovations in the dynamic realm of technology.<\/p>\n                    <div class=\"author-social-links\"><div class=\"social-icon\"><a href=\"https:\/\/www.linkedin.com\/company\/mobisoft-infotech\/mycompany\/\" target=\"_blank\" rel=\"nofollow noopener\"><i class=\"icon-sprite linkedin\"><\/i><\/a>\n                     <a href=\"https:\/\/x.com\/MobisoftInfo\" target=\"_blank\" rel=\"nofollow noopener\"><i class=\"icon-sprite twitter\"><\/i><\/a>\n                     <a href=\"https:\/\/www.instagram.com\/mobisoftinfotech\" target=\"_blank\" rel=\"nofollow noopener\"><i class=\"icon-sprite instagram\"><\/i><\/a><\/div><\/div>\n                    <a href=\"javascript:void(0);\" class=\"read-more-link read-less-btn\" onclick=\"toggleAuthorBio(this); return false;\" style=\"display: none;\">Read less <noscript><img decoding=\"async\" src=\"\/assets\/images\/blog\/Vector.png\" alt=\"collapse\" class=\"read-more-arrow up-arrow\"><\/noscript><img decoding=\"async\" src=\"data:image\/gif;base64,R0lGODlhAQABAIAAAAAAAP\/\/\/yH5BAEAAAAALAAAAAABAAEAAAIBRAA7\" alt=\"collapse\" class=\"read-more-arrow up-arrow lazyload\" data-src=\"\/assets\/images\/blog\/Vector.png\"><\/a>\n                <\/div>\n            <\/div>\n        <\/div>\n        <div class=\"share-section\">\n            <span class=\"share-label\">Share Article<\/span>\n            <div class=\"social-share-buttons\">\n                <a href=\"https:\/\/www.facebook.com\/sharer\/sharer.php?u=https%3A%2F%2Fmobisoftinfotech.com%2Fresources%2Fblog%2Flearn-typescript-with-react-by-building-a-crud-application\" target=\"_blank\" class=\"share-btn facebook-share\"><i class=\"fa fa-facebook-f\"><\/i><\/a>\n                <a href=\"https:\/\/www.linkedin.com\/sharing\/share-offsite\/?url=https%3A%2F%2Fmobisoftinfotech.com%2Fresources%2Fblog%2Flearn-typescript-with-react-by-building-a-crud-application\" target=\"_blank\" class=\"share-btn linkedin-share\"><i class=\"fa fa-linkedin\"><\/i><\/a>\n            <\/div>\n        <\/div>\n    <\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>TypeScript is a powerful programming language that enhances the development of complex web applications by adding a static type system to JavaScript. React is a popular JavaScript library used for building user interfaces. Combining these two technologies can take your web development to the next level.&nbsp; In this article, you will learn the basics of [&hellip;]<\/p>\n","protected":false},"author":77,"featured_media":28694,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_s2mail":"","footnotes":""},"categories":[286],"tags":[3842,3840,3844,3841,3843],"class_list":["post-28653","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog","tag-how-to-learn-typescript","tag-learn-typescript","tag-react-and-typescript","tag-react-with-typescript","tag-typescript-with-react"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.2 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Learn TypeScript with React By Building a CRUD Application<\/title>\n<meta name=\"description\" content=\"Follow step-by-step instructions and code examples to learn TypeScript with React by building a CRUD (Create, Read, Update, Delete) application.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/mobisoftinfotech.com\/resources\/blog\/learn-typescript-with-react-by-building-a-crud-application\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Learn TypeScript with React By Building a CRUD Application\" \/>\n<meta property=\"og:description\" content=\"Follow step-by-step instructions and code examples to learn TypeScript with React by building a CRUD (Create, Read, Update, Delete) application.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/mobisoftinfotech.com\/resources\/blog\/learn-typescript-with-react-by-building-a-crud-application\" \/>\n<meta property=\"og:site_name\" content=\"Mobisoft Infotech\" \/>\n<meta property=\"article:published_time\" content=\"2023-02-22T10:48:52+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-04-22T06:46:48+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/og-Learn-TypeScript-with-React-By-Building-a-CRUD-Application.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1000\" \/>\n\t<meta property=\"og:image:height\" content=\"525\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Mobisoft Technical Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:image\" content=\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/og-Learn-TypeScript-with-React-By-Building-a-CRUD-Application.png\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Mobisoft Technical Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"27 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/mobisoftinfotech.com\/resources\/blog\/learn-typescript-with-react-by-building-a-crud-application#article\",\"isPartOf\":{\"@id\":\"https:\/\/mobisoftinfotech.com\/resources\/blog\/learn-typescript-with-react-by-building-a-crud-application\"},\"author\":{\"name\":\"Mobisoft Technical Team\",\"@id\":\"https:\/\/mobisoftinfotech.com\/resources\/#\/schema\/person\/cff87f61f66e80eb27013df3769dea9b\"},\"headline\":\"Learn TypeScript with React By Building a CRUD Application\",\"datePublished\":\"2023-02-22T10:48:52+00:00\",\"dateModified\":\"2026-04-22T06:46:48+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/mobisoftinfotech.com\/resources\/blog\/learn-typescript-with-react-by-building-a-crud-application\"},\"wordCount\":4685,\"image\":{\"@id\":\"https:\/\/mobisoftinfotech.com\/resources\/blog\/learn-typescript-with-react-by-building-a-crud-application#primaryimage\"},\"thumbnailUrl\":\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/Learn-TypeScript-with-React-By-Building-a-CRUD-Application.png\",\"keywords\":[\"how to learn typescript\",\"learn typescript\",\"react and typescript\",\"react with typescript\",\"typescript with react\"],\"articleSection\":[\"Blog\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/mobisoftinfotech.com\/resources\/blog\/learn-typescript-with-react-by-building-a-crud-application\",\"url\":\"https:\/\/mobisoftinfotech.com\/resources\/blog\/learn-typescript-with-react-by-building-a-crud-application\",\"name\":\"Learn TypeScript with React By Building a CRUD Application\",\"isPartOf\":{\"@id\":\"https:\/\/mobisoftinfotech.com\/resources\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/mobisoftinfotech.com\/resources\/blog\/learn-typescript-with-react-by-building-a-crud-application#primaryimage\"},\"image\":{\"@id\":\"https:\/\/mobisoftinfotech.com\/resources\/blog\/learn-typescript-with-react-by-building-a-crud-application#primaryimage\"},\"thumbnailUrl\":\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/Learn-TypeScript-with-React-By-Building-a-CRUD-Application.png\",\"datePublished\":\"2023-02-22T10:48:52+00:00\",\"dateModified\":\"2026-04-22T06:46:48+00:00\",\"author\":{\"@id\":\"https:\/\/mobisoftinfotech.com\/resources\/#\/schema\/person\/cff87f61f66e80eb27013df3769dea9b\"},\"description\":\"Follow step-by-step instructions and code examples to learn TypeScript with React by building a CRUD (Create, Read, Update, Delete) application.\",\"breadcrumb\":{\"@id\":\"https:\/\/mobisoftinfotech.com\/resources\/blog\/learn-typescript-with-react-by-building-a-crud-application#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/mobisoftinfotech.com\/resources\/blog\/learn-typescript-with-react-by-building-a-crud-application\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/mobisoftinfotech.com\/resources\/blog\/learn-typescript-with-react-by-building-a-crud-application#primaryimage\",\"url\":\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/Learn-TypeScript-with-React-By-Building-a-CRUD-Application.png\",\"contentUrl\":\"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/Learn-TypeScript-with-React-By-Building-a-CRUD-Application.png\",\"width\":855,\"height\":392,\"caption\":\"Learn TypeScript with React By Building a CRUD Application\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/mobisoftinfotech.com\/resources\/blog\/learn-typescript-with-react-by-building-a-crud-application#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/mobisoftinfotech.com\/resources\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Learn TypeScript with React By Building a CRUD Application\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/mobisoftinfotech.com\/resources\/#website\",\"url\":\"https:\/\/mobisoftinfotech.com\/resources\/\",\"name\":\"Mobisoft Infotech\",\"description\":\"Discover Mobility\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/mobisoftinfotech.com\/resources\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/mobisoftinfotech.com\/resources\/#\/schema\/person\/cff87f61f66e80eb27013df3769dea9b\",\"name\":\"Mobisoft Technical Team\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/secure.gravatar.com\/avatar\/06d33e852f3b3f6408be632d00fab205afcf44ad01def4698690cb92516c3a6f?s=96&r=g\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/06d33e852f3b3f6408be632d00fab205afcf44ad01def4698690cb92516c3a6f?s=96&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/06d33e852f3b3f6408be632d00fab205afcf44ad01def4698690cb92516c3a6f?s=96&r=g\",\"caption\":\"Mobisoft Technical Team\"},\"sameAs\":[\"https:\/\/mobisoftinfotech.com\/\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Learn TypeScript with React By Building a CRUD Application","description":"Follow step-by-step instructions and code examples to learn TypeScript with React by building a CRUD (Create, Read, Update, Delete) application.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/mobisoftinfotech.com\/resources\/blog\/learn-typescript-with-react-by-building-a-crud-application","og_locale":"en_US","og_type":"article","og_title":"Learn TypeScript with React By Building a CRUD Application","og_description":"Follow step-by-step instructions and code examples to learn TypeScript with React by building a CRUD (Create, Read, Update, Delete) application.","og_url":"https:\/\/mobisoftinfotech.com\/resources\/blog\/learn-typescript-with-react-by-building-a-crud-application","og_site_name":"Mobisoft Infotech","article_published_time":"2023-02-22T10:48:52+00:00","article_modified_time":"2026-04-22T06:46:48+00:00","og_image":[{"width":1000,"height":525,"url":"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/og-Learn-TypeScript-with-React-By-Building-a-CRUD-Application.png","type":"image\/png"}],"author":"Mobisoft Technical Team","twitter_card":"summary_large_image","twitter_image":"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/og-Learn-TypeScript-with-React-By-Building-a-CRUD-Application.png","twitter_misc":{"Written by":"Mobisoft Technical Team","Est. reading time":"27 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/mobisoftinfotech.com\/resources\/blog\/learn-typescript-with-react-by-building-a-crud-application#article","isPartOf":{"@id":"https:\/\/mobisoftinfotech.com\/resources\/blog\/learn-typescript-with-react-by-building-a-crud-application"},"author":{"name":"Mobisoft Technical Team","@id":"https:\/\/mobisoftinfotech.com\/resources\/#\/schema\/person\/cff87f61f66e80eb27013df3769dea9b"},"headline":"Learn TypeScript with React By Building a CRUD Application","datePublished":"2023-02-22T10:48:52+00:00","dateModified":"2026-04-22T06:46:48+00:00","mainEntityOfPage":{"@id":"https:\/\/mobisoftinfotech.com\/resources\/blog\/learn-typescript-with-react-by-building-a-crud-application"},"wordCount":4685,"image":{"@id":"https:\/\/mobisoftinfotech.com\/resources\/blog\/learn-typescript-with-react-by-building-a-crud-application#primaryimage"},"thumbnailUrl":"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/Learn-TypeScript-with-React-By-Building-a-CRUD-Application.png","keywords":["how to learn typescript","learn typescript","react and typescript","react with typescript","typescript with react"],"articleSection":["Blog"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/mobisoftinfotech.com\/resources\/blog\/learn-typescript-with-react-by-building-a-crud-application","url":"https:\/\/mobisoftinfotech.com\/resources\/blog\/learn-typescript-with-react-by-building-a-crud-application","name":"Learn TypeScript with React By Building a CRUD Application","isPartOf":{"@id":"https:\/\/mobisoftinfotech.com\/resources\/#website"},"primaryImageOfPage":{"@id":"https:\/\/mobisoftinfotech.com\/resources\/blog\/learn-typescript-with-react-by-building-a-crud-application#primaryimage"},"image":{"@id":"https:\/\/mobisoftinfotech.com\/resources\/blog\/learn-typescript-with-react-by-building-a-crud-application#primaryimage"},"thumbnailUrl":"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/Learn-TypeScript-with-React-By-Building-a-CRUD-Application.png","datePublished":"2023-02-22T10:48:52+00:00","dateModified":"2026-04-22T06:46:48+00:00","author":{"@id":"https:\/\/mobisoftinfotech.com\/resources\/#\/schema\/person\/cff87f61f66e80eb27013df3769dea9b"},"description":"Follow step-by-step instructions and code examples to learn TypeScript with React by building a CRUD (Create, Read, Update, Delete) application.","breadcrumb":{"@id":"https:\/\/mobisoftinfotech.com\/resources\/blog\/learn-typescript-with-react-by-building-a-crud-application#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/mobisoftinfotech.com\/resources\/blog\/learn-typescript-with-react-by-building-a-crud-application"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/mobisoftinfotech.com\/resources\/blog\/learn-typescript-with-react-by-building-a-crud-application#primaryimage","url":"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/Learn-TypeScript-with-React-By-Building-a-CRUD-Application.png","contentUrl":"https:\/\/mobisoftinfotech.com\/resources\/wp-content\/uploads\/2023\/02\/Learn-TypeScript-with-React-By-Building-a-CRUD-Application.png","width":855,"height":392,"caption":"Learn TypeScript with React By Building a CRUD Application"},{"@type":"BreadcrumbList","@id":"https:\/\/mobisoftinfotech.com\/resources\/blog\/learn-typescript-with-react-by-building-a-crud-application#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/mobisoftinfotech.com\/resources\/"},{"@type":"ListItem","position":2,"name":"Learn TypeScript with React By Building a CRUD Application"}]},{"@type":"WebSite","@id":"https:\/\/mobisoftinfotech.com\/resources\/#website","url":"https:\/\/mobisoftinfotech.com\/resources\/","name":"Mobisoft Infotech","description":"Discover Mobility","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/mobisoftinfotech.com\/resources\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/mobisoftinfotech.com\/resources\/#\/schema\/person\/cff87f61f66e80eb27013df3769dea9b","name":"Mobisoft Technical Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/06d33e852f3b3f6408be632d00fab205afcf44ad01def4698690cb92516c3a6f?s=96&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/06d33e852f3b3f6408be632d00fab205afcf44ad01def4698690cb92516c3a6f?s=96&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/06d33e852f3b3f6408be632d00fab205afcf44ad01def4698690cb92516c3a6f?s=96&r=g","caption":"Mobisoft Technical Team"},"sameAs":["https:\/\/mobisoftinfotech.com\/"]}]}},"_links":{"self":[{"href":"https:\/\/mobisoftinfotech.com\/resources\/wp-json\/wp\/v2\/posts\/28653","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mobisoftinfotech.com\/resources\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mobisoftinfotech.com\/resources\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mobisoftinfotech.com\/resources\/wp-json\/wp\/v2\/users\/77"}],"replies":[{"embeddable":true,"href":"https:\/\/mobisoftinfotech.com\/resources\/wp-json\/wp\/v2\/comments?post=28653"}],"version-history":[{"count":97,"href":"https:\/\/mobisoftinfotech.com\/resources\/wp-json\/wp\/v2\/posts\/28653\/revisions"}],"predecessor-version":[{"id":49176,"href":"https:\/\/mobisoftinfotech.com\/resources\/wp-json\/wp\/v2\/posts\/28653\/revisions\/49176"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/mobisoftinfotech.com\/resources\/wp-json\/wp\/v2\/media\/28694"}],"wp:attachment":[{"href":"https:\/\/mobisoftinfotech.com\/resources\/wp-json\/wp\/v2\/media?parent=28653"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mobisoftinfotech.com\/resources\/wp-json\/wp\/v2\/categories?post=28653"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mobisoftinfotech.com\/resources\/wp-json\/wp\/v2\/tags?post=28653"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}