{"id":7159,"date":"2019-06-09T20:35:10","date_gmt":"2019-06-09T20:35:10","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=7159"},"modified":"2019-06-09T20:35:10","modified_gmt":"2019-06-09T20:35:10","slug":"creating-react-components-in-typescript","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/creating-react-components-in-typescript\/","title":{"rendered":"Creating React components in TypeScript"},"content":{"rendered":"<p>By default we&#8217;ll usually create a React component (in a .tsx file) using a React.Component base class, for example<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nexport default class GridSampleComponent extends Component&lt;{}, {}&gt; {\r\n   render() {\r\n      return (\r\n         &lt;div&gt;&lt;\/div&gt;\r\n      );\r\n   }\r\n}\r\n<\/pre>\n<p>But as per my post <a href=\"http:\/\/putridparrot.com\/blog\/react-material-ui-invalid-hook-call-hooks-can-only-be-called-inside-of-the-body-of-a-function-component\/\" rel=\"noopener noreferrer\" target=\"_blank\">here<\/a> we can also create components through functions, which ofcourse makes sense when you see a .jsx file which tend to default to using functions.<\/p>\n<p>Hence we can create the following<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nconst MyButton = (props: any) =&gt; {\r\n   return \r\n      &lt;Button variant=&quot;contained&quot; \r\n         color=&quot;secondary&quot;&gt;{props.children}&lt;\/Button&gt;;\r\n};\r\n<\/pre>\n<p>In the code above we&#8217;ve basically created a new component from the function which takes properties which are implicitly passed to the function via React, i.e. the code for using the above might look like this<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n&lt;MyButton&gt;Hello&lt;\/MyButton&gt;\r\n<\/pre>\n<p>The child elements are passed through to our function. In this case it&#8217;s just the string &#8220;Hello&#8221;.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>By default we&#8217;ll usually create a React component (in a .tsx file) using a React.Component base class, for example export default class GridSampleComponent extends Component&lt;{}, {}&gt; { render() { return ( &lt;div&gt;&lt;\/div&gt; ); } } But as per my post here we can also create components through functions, which ofcourse makes sense when you see [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[243],"tags":[],"class_list":["post-7159","post","type-post","status-publish","format-standard","hentry","category-react"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/7159","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=7159"}],"version-history":[{"count":2,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/7159\/revisions"}],"predecessor-version":[{"id":7161,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/7159\/revisions\/7161"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=7159"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=7159"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=7159"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}