{"id":1356,"date":"2023-05-05T11:03:20","date_gmt":"2023-05-05T14:03:20","guid":{"rendered":"https:\/\/benjaminray.com\/codebase\/?p=1356"},"modified":"2023-05-05T11:18:20","modified_gmt":"2023-05-05T14:18:20","slug":"react-component-with-children","status":"publish","type":"post","link":"https:\/\/benjaminray.com\/codebase\/react-component-with-children\/","title":{"rendered":"React Component with Children"},"content":{"rendered":"<p>Creating a React component that contains children can be as easy as including a <code>children<\/code> property and including <code>{props.children}<\/code> in the Return function. But knowing the proper type to use for the <code>children<\/code> property (when using TypeScript) can get complicated, so it's better to use one of the two officially supported types.<\/p>\n<p>While we can type the <code>children<\/code> property as <code>ReactNode<\/code>, my preference is to use the <code>PropsWithChildren<\/code> type, which handles the children property and its typing for us.<\/p>\n<p>TestComponent.tsx:<\/p>\n<pre><code>import React, { PropsWithChildren } from \"react\";\n\ntype TestWrappingComponentProps = {\n    beforeText?: string;\n    afterText?: string;\n};\n\nexport const TestWrappingComponent = (props: PropsWithChildren&lt;TestWrappingComponentProps&gt;) =&gt; {\n\n    return (\n        &lt;&gt;\n            &lt;div&gt;Before: {props.beforeText}&lt;\/div&gt;\n            {props.children}\n            &lt;div&gt;After: {props.afterText}&lt;\/div&gt;\n        &lt;\/&gt;\n    )\n}\n<\/code><\/pre>\n<p>Using it in another component:<\/p>\n<pre><code>import { TestWrappingComponent } from \"somePath\/TestWrappingComponent\";\n\n&lt;TestWrappingComponent beforeText=\"text before children\" afterText=\"text after children\" &gt;\n    &lt;button onClick={() =&gt; { alert('clicked'); }}&gt;test button&lt;\/button&gt;\n    &lt;div&gt;test div&lt;\/div&gt;\n    Some plain text...\n&lt;\/TestWrappingComponent&gt;\n<\/code><\/pre>\n<h2>Credit<\/h2>\n<p>With help from: <a href=\"https:\/\/blog.logrocket.com\/using-react-children-prop-with-typescript\/\" rel=\"noopener\" target=\"_blank\">https:\/\/blog.logrocket.com\/using-react-children-prop-with-typescript\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Creating a React component that contains children can be as easy as including a children property and including {props.children} in the Return function. But knowing the proper type to use  [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[6,14,28,29],"tags":[],"class_list":["post-1356","post","type-post","status-publish","format-standard","hentry","category-development","category-javascript","category-react","category-typescript"],"acf":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9GNjN-lS","jetpack_likes_enabled":false,"_links":{"self":[{"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/posts\/1356","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/comments?post=1356"}],"version-history":[{"count":8,"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/posts\/1356\/revisions"}],"predecessor-version":[{"id":1364,"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/posts\/1356\/revisions\/1364"}],"wp:attachment":[{"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/media?parent=1356"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/categories?post=1356"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/tags?post=1356"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}