{"id":30318,"date":"2023-01-14T13:18:59","date_gmt":"2023-01-14T07:48:59","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-functional-component-to-class-component\/"},"modified":"2023-01-14T13:18:59","modified_gmt":"2023-01-14T07:48:59","slug":"solved-functional-component-to-class-component","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-functional-component-to-class-component\/","title":{"rendered":"[Solved] functional component to class component"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-61864288\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"61864288\" data-parentid=\"61863949\" data-score=\"0\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>You <em>should<\/em> be able to use a functional component regardless of really understanding how they work, but converting functional components is fairly trivial if you understand react state\/props\/lifecycle.<\/p>\n<p>Basic steps:<\/p>\n<ol>\n<li>Convert all state hooks to single state object and update all state update functions to now use <code>this.setState<\/code> with the correct state to be merged.<\/li>\n<li>Convert effect hooks to appropriate lifecycle function (usually <code>componentDidMount<\/code>, <code>componentDidUpdate<\/code>, and\/or <code>componentWillUnmount<\/code>.<\/li>\n<li>Convert ref hook to regular react.createRef`.<\/li>\n<li>Convert all callback and utility functions to be instance functions (remove <code>const<\/code>).<\/li>\n<li>Move previous return into <code>render<\/code> lifecycle function<\/li>\n<li>Update all state\/prop, and callback and utility functions to correctly access <code>this<\/code> of the component, either for function invocation or state\/prop access.<\/li>\n<\/ol>\n<p>Class-based component<\/p>\n<pre><code>import React, { Component, createRef } from \"react\";\nimport Button from \"@material-ui\/core\/Button\";\nimport Dialog from \"@material-ui\/core\/Dialog\";\nimport DialogActions from \"@material-ui\/core\/DialogActions\";\nimport DialogContent from \"@material-ui\/core\/DialogContent\";\nimport DialogContentText from \"@material-ui\/core\/DialogContentText\";\nimport DialogTitle from \"@material-ui\/core\/DialogTitle\";\n\nexport default class ScrollDialog extends Component {\n  state = {\n    open: false,\n    scroll: \"paper\"\n  };\n\n  handleClickOpen = scrollType =&gt; () =&gt; {\n    this.setState({\n      open: true,\n      scroll: scrollType\n    });\n  };\n\n  handleClose = () =&gt; {\n    this.setState({ open: false });\n  };\n\n  descriptionElementRef = createRef();\n\n  componentDidUpdate(prevProps, prevState) {\n    if (this.state.open) {\n      const { current: descriptionElement } = this.descriptionElementRef;\n      if (descriptionElement !== null) {\n        descriptionElement.focus();\n      }\n    }\n  }\n\n  render() {\n    const { open, scroll } = this.state;\n    return (\n      &lt;div&gt;\n        &lt;Button onClick={this.handleClickOpen(\"paper\")}&gt;scroll=paper&lt;\/Button&gt;\n        &lt;Button onClick={this.handleClickOpen(\"body\")}&gt;scroll=body&lt;\/Button&gt;\n        &lt;Dialog\n          open={open}\n          onClose={this.handleClose}\n          scroll={scroll}\n          aria-labelledby=\"scroll-dialog-title\"\n          aria-describedby=\"scroll-dialog-description\"\n        &gt;\n          &lt;DialogTitle id=\"scroll-dialog-title\"&gt;Subscribe&lt;\/DialogTitle&gt;\n          &lt;DialogContent dividers={scroll === \"paper\"}&gt;\n            &lt;DialogContentText\n              id=\"scroll-dialog-description\"\n              ref={this.descriptionElementRef}\n              tabIndex={-1}\n            &gt;\n              {[...new Array(50)]\n                .map(\n                  () =&gt; `Cras mattis consectetur purus sit amet fermentum.\n  Cras justo odio, dapibus ac facilisis in, egestas eget quam.\n  Morbi leo risus, porta ac consectetur ac, vestibulum at eros.\n  Praesent commodo cursus magna, vel scelerisque nisl consectetur et.`\n                )\n                .join(\"\\n\")}\n            &lt;\/DialogContentText&gt;\n          &lt;\/DialogContent&gt;\n          &lt;DialogActions&gt;\n            &lt;Button onClick={this.handleClose} color=\"primary\"&gt;\n              Cancel\n            &lt;\/Button&gt;\n            &lt;Button onClick={this.handleClose} color=\"primary\"&gt;\n              Subscribe\n            &lt;\/Button&gt;\n          &lt;\/DialogActions&gt;\n        &lt;\/Dialog&gt;\n      &lt;\/div&gt;\n    );\n  }\n}\n<\/code><\/pre>\n<p><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/codesandbox.io\/s\/snowy-platform-t1xox?fontsize=14&amp;hidenavigation=1&amp;module=%2Fsrc%2FScrollDialog.jsx&amp;theme=dark\"><img decoding=\"async\" src=\"https:\/\/jassweb.com\/solved\/wp-content\/uploads\/2022\/10\/Solved-convert-the-following-piece-of-react-code-to-JSX.svg.svg+xml\" alt=\"Edit snowy-platform-t1xox\"><\/a><\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">2<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved functional component to class component <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] You should be able to use a functional component regardless of really understanding how they work, but converting functional components is fairly trivial if you understand react state\/props\/lifecycle. Basic steps: Convert all state hooks to single state object and update all state update functions to now use this.setState with the correct state to be &#8230; <a title=\"[Solved] functional component to class component\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-functional-component-to-class-component\/\" aria-label=\"More on [Solved] functional component to class component\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[784],"class_list":["post-30318","post","type-post","status-publish","format-standard","hentry","category-solved","tag-reactjs"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] functional component to class component - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-functional-component-to-class-component\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] functional component to class component - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] You should be able to use a functional component regardless of really understanding how they work, but converting functional components is fairly trivial if you understand react state\/props\/lifecycle. Basic steps: Convert all state hooks to single state object and update all state update functions to now use this.setState with the correct state to be ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-functional-component-to-class-component\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2023-01-14T07:48:59+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/jassweb.com\/solved\/wp-content\/uploads\/2022\/10\/Solved-convert-the-following-piece-of-react-code-to-JSX.svg.svg+xml\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-functional-component-to-class-component\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-functional-component-to-class-component\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] functional component to class component\",\"datePublished\":\"2023-01-14T07:48:59+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-functional-component-to-class-component\/\"},\"wordCount\":129,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-functional-component-to-class-component\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/uploads\/2022\/10\/Solved-convert-the-following-piece-of-react-code-to-JSX.svg.svg+xml\",\"keywords\":[\"reactjs\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-functional-component-to-class-component\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-functional-component-to-class-component\/\",\"name\":\"[Solved] functional component to class component - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-functional-component-to-class-component\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-functional-component-to-class-component\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/uploads\/2022\/10\/Solved-convert-the-following-piece-of-react-code-to-JSX.svg.svg+xml\",\"datePublished\":\"2023-01-14T07:48:59+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-functional-component-to-class-component\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-functional-component-to-class-component\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-functional-component-to-class-component\/#primaryimage\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/uploads\/2022\/10\/Solved-convert-the-following-piece-of-react-code-to-JSX.svg.svg+xml\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/uploads\/2022\/10\/Solved-convert-the-following-piece-of-react-code-to-JSX.svg.svg+xml\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-functional-component-to-class-component\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] functional component to class component\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] functional component to class component - JassWeb","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:\/\/jassweb.com\/solved\/solved-functional-component-to-class-component\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] functional component to class component - JassWeb","og_description":"[ad_1] You should be able to use a functional component regardless of really understanding how they work, but converting functional components is fairly trivial if you understand react state\/props\/lifecycle. Basic steps: Convert all state hooks to single state object and update all state update functions to now use this.setState with the correct state to be ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-functional-component-to-class-component\/","og_site_name":"JassWeb","article_published_time":"2023-01-14T07:48:59+00:00","og_image":[{"url":"https:\/\/jassweb.com\/solved\/wp-content\/uploads\/2022\/10\/Solved-convert-the-following-piece-of-react-code-to-JSX.svg.svg+xml","type":"","width":"","height":""}],"author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-functional-component-to-class-component\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-functional-component-to-class-component\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] functional component to class component","datePublished":"2023-01-14T07:48:59+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-functional-component-to-class-component\/"},"wordCount":129,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"image":{"@id":"https:\/\/jassweb.com\/solved\/solved-functional-component-to-class-component\/#primaryimage"},"thumbnailUrl":"https:\/\/jassweb.com\/solved\/wp-content\/uploads\/2022\/10\/Solved-convert-the-following-piece-of-react-code-to-JSX.svg.svg+xml","keywords":["reactjs"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-functional-component-to-class-component\/","url":"https:\/\/jassweb.com\/solved\/solved-functional-component-to-class-component\/","name":"[Solved] functional component to class component - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"primaryImageOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-functional-component-to-class-component\/#primaryimage"},"image":{"@id":"https:\/\/jassweb.com\/solved\/solved-functional-component-to-class-component\/#primaryimage"},"thumbnailUrl":"https:\/\/jassweb.com\/solved\/wp-content\/uploads\/2022\/10\/Solved-convert-the-following-piece-of-react-code-to-JSX.svg.svg+xml","datePublished":"2023-01-14T07:48:59+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-functional-component-to-class-component\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-functional-component-to-class-component\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/solved-functional-component-to-class-component\/#primaryimage","url":"https:\/\/jassweb.com\/solved\/wp-content\/uploads\/2022\/10\/Solved-convert-the-following-piece-of-react-code-to-JSX.svg.svg+xml","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/uploads\/2022\/10\/Solved-convert-the-following-piece-of-react-code-to-JSX.svg.svg+xml"},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-functional-component-to-class-component\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] functional component to class component"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/30318","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=30318"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/30318\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=30318"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=30318"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=30318"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}