{"componentChunkName":"component---src-gatsby-theme-try-ghost-templates-post-js","path":"/10-reasons-why-you-should-use-react-for-building-interactive-user-interfaces/","result":{"data":{"customPost":{"id":"Ghost__Post__61bc7669c393bb64592d8688","title":"10 Reasons Why You Should Use React for Building Interactive User Interfaces","slug":"10-reasons-why-you-should-use-react-for-building-interactive-user-interfaces","featured":false,"feature_image":"https://backend.shahednasser.com/content/images/2021/12/lautaro-andreani-xkBaqlcqeb4-unsplash-2.jpg","excerpt":"Web Page design takes time, effort, and attention to detail, but React simplifies the user interface design and developers’ lives.","custom_excerpt":"Web Page design takes time, effort, and attention to detail, but React simplifies the user interface design and developers’ lives.","visibility":"public","created_at_pretty":"17 Dec 2021","published_at_pretty":"17 Dec 2021","updated_at_pretty":"17 Dec 2021","created_at":"2021-12-17T11:37:13.000+00:00","published_at":"2021-12-17T12:04:36.000+00:00","updated_at":"2021-12-17T12:04:36.000+00:00","meta_title":null,"meta_description":null,"og_description":null,"og_image":null,"og_title":null,"twitter_description":null,"twitter_image":null,"twitter_title":null,"authors":[{"slug":"harikrishna","url":"https://backend.shahednasser.com/author/harikrishna/","name":"Harikrishna Kundariya","bio":"A marketer, developer, IoT, ChatBot & Blockchain savvy, designer, co-founder, Director of eSparkBiz Technologies. His 10+ experience enables him to provide digital solutions based on IoT and ChatBot.","cover_image":null,"profile_image":"https://backend.shahednasser.com/content/images/2021/10/Author-Headshot.jpg","location":null,"website":"https://www.esparkinfo.com","twitter":null,"facebook":null,"meta_title":null,"meta_description":null,"coverImageSharp":null,"profileImageSharp":null}],"primary_author":{"slug":"harikrishna","url":"https://backend.shahednasser.com/author/harikrishna/","name":"Harikrishna Kundariya","bio":"A marketer, developer, IoT, ChatBot & Blockchain savvy, designer, co-founder, Director of eSparkBiz Technologies. His 10+ experience enables him to provide digital solutions based on IoT and ChatBot.","cover_image":null,"profile_image":"https://backend.shahednasser.com/content/images/2021/10/Author-Headshot.jpg","location":null,"website":"https://www.esparkinfo.com","twitter":null,"facebook":null,"meta_title":null,"meta_description":null,"coverImageSharp":null,"profileImageSharp":{"base":"Author-Headshot.jpg","publicURL":"/static/e393bccca9ad04da9f27a0afd06cca7d/Author-Headshot.jpg","imageMeta":{"width":250,"height":250},"childImageSharp":{"fluid":{"base64":"data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAUABQDASIAAhEBAxEB/8QAGQABAAIDAAAAAAAAAAAAAAAAAAQFAgMG/8QAFgEBAQEAAAAAAAAAAAAAAAAAAQIA/9oADAMBAAIQAxAAAAGzj4VdHRoairghtwL/AP/EABsQAAICAwEAAAAAAAAAAAAAAAECAwQAERIz/9oACAEBAAEFAptiOEt1lmfgCZkeOwrrc98QbH//xAAVEQEBAAAAAAAAAAAAAAAAAAAgQf/aAAgBAwEBPwGD/8QAFREBAQAAAAAAAAAAAAAAAAAAIEH/2gAIAQIBAT8Bo//EAB8QAAEDAwUAAAAAAAAAAAAAAAEAAhAREmEhIjFBgf/aAAgBAQAGPwLaiC4uGYs7Kq3jKqdCvJ//xAAdEAEBAQACAgMAAAAAAAAAAAABEQAxURAhQaGx/9oACAEBAAE/IU5UaFDjClDR5eDhKPt6M4S/ZgqHzG/DlZLgq97/2gAMAwEAAgADAAAAELcAvv/EABcRAQEBAQAAAAAAAAAAAAAAAAERABD/2gAIAQMBAT8QJ0BN/8QAFxEAAwEAAAAAAAAAAAAAAAAAAAEREP/aAAgBAgEBPxCPVP/EABwQAQACAwEBAQAAAAAAAAAAAAEAESExQWFR8P/aAAgBAQABPxB6zouIXKH2LTwHKN5LOS52lQ0zv1ipNFZ4do1Lq4pXT55E3/O46xUaL1HFl4Yan//Z","aspectRatio":1,"src":"/static/e393bccca9ad04da9f27a0afd06cca7d/31709/Author-Headshot.jpg","srcSet":"/static/e393bccca9ad04da9f27a0afd06cca7d/f340b/Author-Headshot.jpg 28w,\n/static/e393bccca9ad04da9f27a0afd06cca7d/22d64/Author-Headshot.jpg 55w,\n/static/e393bccca9ad04da9f27a0afd06cca7d/31709/Author-Headshot.jpg 110w,\n/static/e393bccca9ad04da9f27a0afd06cca7d/aa249/Author-Headshot.jpg 165w,\n/static/e393bccca9ad04da9f27a0afd06cca7d/0dc33/Author-Headshot.jpg 220w,\n/static/e393bccca9ad04da9f27a0afd06cca7d/84d81/Author-Headshot.jpg 250w","srcWebp":"/static/e393bccca9ad04da9f27a0afd06cca7d/8678c/Author-Headshot.webp","srcSetWebp":"/static/e393bccca9ad04da9f27a0afd06cca7d/59cda/Author-Headshot.webp 28w,\n/static/e393bccca9ad04da9f27a0afd06cca7d/7da75/Author-Headshot.webp 55w,\n/static/e393bccca9ad04da9f27a0afd06cca7d/8678c/Author-Headshot.webp 110w,\n/static/e393bccca9ad04da9f27a0afd06cca7d/f282e/Author-Headshot.webp 165w,\n/static/e393bccca9ad04da9f27a0afd06cca7d/a7b21/Author-Headshot.webp 220w,\n/static/e393bccca9ad04da9f27a0afd06cca7d/1d872/Author-Headshot.webp 250w","sizes":"(max-width: 110px) 100vw, 110px"}}}},"primary_tag":{"slug":"react","url":"https://backend.shahednasser.com/tag/react/","name":"React","visibility":"public","feature_image":"https://images.unsplash.com/photo-1581276879432-15e50529f34b?crop=entropy&cs=tinysrgb&fit=max&fm=jpg&ixid=MnwxMTc3M3wwfDF8c2VhcmNofDN8fHJlYWN0fGVufDB8fHx8MTYyMjYzMzI0MA&ixlib=rb-1.2.1&q=80&w=2000","description":"Learn more about React through tutorials, articles, and tips.","meta_title":null,"meta_description":null,"featureImageSharp":{"base":"photo-1581276879432-15e50529f34b.jpg","publicURL":"/static/7140ea32e1157ba61402d5d67ab846d4/photo-1581276879432-15e50529f34b.jpg","imageMeta":{"width":2000,"height":1333},"childImageSharp":{"fluid":{"base64":"data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAANABQDASIAAhEBAxEB/8QAFwAAAwEAAAAAAAAAAAAAAAAAAAIDBf/EABUBAQEAAAAAAAAAAAAAAAAAAAAB/9oADAMBAAIQAxAAAAHJai1MBP/EABcQAAMBAAAAAAAAAAAAAAAAAAABEBH/2gAIAQEAAQUCuDU//8QAFBEBAAAAAAAAAAAAAAAAAAAAEP/aAAgBAwEBPwE//8QAFREBAQAAAAAAAAAAAAAAAAAAEBH/2gAIAQIBAT8Bh//EABQQAQAAAAAAAAAAAAAAAAAAACD/2gAIAQEABj8CX//EABgQAQEBAQEAAAAAAAAAAAAAAAABESFR/9oACAEBAAE/IYy+rHKDH//aAAwDAQACAAMAAAAQC9//xAAVEQEBAAAAAAAAAAAAAAAAAAAQEf/aAAgBAwEBPxCn/8QAFREBAQAAAAAAAAAAAAAAAAAAARD/2gAIAQIBAT8QUT//xAAaEAADAQADAAAAAAAAAAAAAAAAAREhQWFx/9oACAEBAAE/EMY0neijzwMrrVEm7auUQPNGlP/Z","aspectRatio":1.5028901734104045,"src":"/static/7140ea32e1157ba61402d5d67ab846d4/d5c54/photo-1581276879432-15e50529f34b.jpg","srcSet":"/static/7140ea32e1157ba61402d5d67ab846d4/65d8c/photo-1581276879432-15e50529f34b.jpg 260w,\n/static/7140ea32e1157ba61402d5d67ab846d4/c5f21/photo-1581276879432-15e50529f34b.jpg 520w,\n/static/7140ea32e1157ba61402d5d67ab846d4/d5c54/photo-1581276879432-15e50529f34b.jpg 1040w,\n/static/7140ea32e1157ba61402d5d67ab846d4/81a53/photo-1581276879432-15e50529f34b.jpg 1560w,\n/static/7140ea32e1157ba61402d5d67ab846d4/4e5f3/photo-1581276879432-15e50529f34b.jpg 2000w","srcWebp":"/static/7140ea32e1157ba61402d5d67ab846d4/e4875/photo-1581276879432-15e50529f34b.webp","srcSetWebp":"/static/7140ea32e1157ba61402d5d67ab846d4/dc8f3/photo-1581276879432-15e50529f34b.webp 260w,\n/static/7140ea32e1157ba61402d5d67ab846d4/2db4b/photo-1581276879432-15e50529f34b.webp 520w,\n/static/7140ea32e1157ba61402d5d67ab846d4/e4875/photo-1581276879432-15e50529f34b.webp 1040w,\n/static/7140ea32e1157ba61402d5d67ab846d4/f5845/photo-1581276879432-15e50529f34b.webp 1560w,\n/static/7140ea32e1157ba61402d5d67ab846d4/49d6b/photo-1581276879432-15e50529f34b.webp 2000w","sizes":"(max-width: 1040px) 100vw, 1040px"}}}},"tags":[{"slug":"react","url":"https://backend.shahednasser.com/tag/react/","name":"React","visibility":"public","feature_image":"https://images.unsplash.com/photo-1581276879432-15e50529f34b?crop=entropy&cs=tinysrgb&fit=max&fm=jpg&ixid=MnwxMTc3M3wwfDF8c2VhcmNofDN8fHJlYWN0fGVufDB8fHx8MTYyMjYzMzI0MA&ixlib=rb-1.2.1&q=80&w=2000","description":"Learn more about React through tutorials, articles, and tips.","meta_title":null,"meta_description":null,"featureImageSharp":null},{"slug":"js","url":"https://backend.shahednasser.com/tag/js/","name":"Javascript","visibility":"public","feature_image":"https://backend.shahednasser.com/content/images/2022/01/photo-1592609931095-54a2168ae893-2.jpeg","description":"Learn more about Javascript through tutorials, articles, and tips.","meta_title":null,"meta_description":"Learn more about Javascript through tutorials, articles and tips.","featureImageSharp":null}],"plaintext":"Creating interactive end-user Interfaces is difficult regardless of the\nprogramming platform. Web Page design takes time, effort, and attention to\ndetail, but React [https://reactjs.org] simplifies the user interface design and\ndevelopers’ lives.\n\nYou may be curious about using React to build User Interfaces (UI). With its\nexcellent JavaScript structure, the growth process is considerably more\nmanageable.\n\nBig corporations like Facebook, Airbnb, Netflix, and PayPal use it. You surely\ndon't want to ruin your chance of creating an excellent UI for your project.\n\nYou should hire the best React development company\n[https://www.esparkinfo.com/web-app-development/react-js.html] to get your hands\non highly efficient and competitive React designers.\n\nWhat is React\nFacebook created and maintained React, a JavaScript library. React is a\npractical, straightforward, and adaptable open-source JavaScript library for\ndeveloping simple, quick, and scalable online apps.\n\nReact is a widely sought-after option for developing user-friendly and engaging\nweb pages and apps that allow developers to be more creative.\n\nWhat makes React so popular among businesses and brands?\n\nReact Interactive UI Features\nSimple to use\nReact is easy to use and offers dynamism to any UI layout. It also enables rapid\napplication progress with good quality, saving time for both clients and\ndesigners.\n\nDeclarative\nReact allows significant data changes to affect specified UI elements\nautomatically. Because of this advanced feature, you do not need to do anything\nfurther to upgrade your UI.\n\nReusable Elements\nReusability is a cure for developers. React offers tools and frameworks that\ndevelopers may reuse to build new apps. This platform allows contrivers to reuse\nsegments made for other apps with similar functionality. It reduces growth time\nand ensures faultless functioning.\n\nLibraries for JavaScript programming\nA potent combination of JavaScript and HTML code is constantly employed,\nstreamlining the whole coding process. The JS library has numerous methods that\ntranslate HTML elements into needed functions and simplify the overall project.\n\nAssistance with Components\nReact combines JavaScript and HTML tags. Then React acts as a mediator,\nrepresenting the DOM and helping decide which element needs to be changed. The\nHTML elements and JS codes make dealing with large amounts of data easier.\n\nSEO-friendly\nFacebook put a lot of work on React. Likely the best way to construct great,\nSEO-friendly user experiences spanning browsers and engines.\n\nLightweight DOM integration\nBecause React is so simple, it perfectly implements the DOM (document object\nmodel). It does not utilize the design but allows developers to change the DOM\nversion in memory. This feature automatically improves application performance.\n\nExpert Data Binding\nReact lags one-way data binding. It means anybody can trace all modifications\nmade to any data section. Its simplicity is reflected here.\n\nOther Benefits of React\n * Facilitates coding of JavaScript\n * Outstanding cross-platform support\n * Manages dependencies\n * Easy template design\n * UI-driven designs\n * Simple to adopt\n\nTen Reasons to Use React in Your Project\nIf you've heard of React but aren't sure whether to utilize it in your project,\nhere are the top ten reasons why React could be the best choice.\n\n1. It is simple to learn.\nUnlike Angular and Vue, React is considerably easier to understand. It's one of\nthe critical reasons React proliferated. It helps firms construct projects\nrapidly. The more challenging technology or structure is to comprehend, the\nlengthier it takes to develop. And we humans prefer to shun complex tasks.\nHowever, because React is a straightforward platform, enterprises and major\nbrands are more tempted to use it.\n\n2. Creating rich user interfaces\nThe UI of a program is critical nowadays. A poorly designed UI reduces an\napplication's chances of success. However, a high-quality UI increases the\nlikelihood of people using your program. So, creating rich end-UIs is crucial\nfor a program to exist. The helpful information is that React's declarative\nelements allow us to construct high-quality, rich UXs.\n\n3. It supports custom components\nJSX, a voluntary semantic extension, allows you to design your elements for\nReact. These elements allow HTML quoting and make subcomponent rendering easy\nfor contrivers. JSX is already used to design bespoke elements, construct\nhigh-volume apps, and transform HTML mock-ups into ReactElement trees.\n\n4. It boosts developers' output\nFrequent updates can pose problems with complex logic since a single move might\nsubstantially affect other aspects. Facebook fixed the issue by making React's\nelements reusable. Reusable elements allow formulators to reuse digital\nmaterials. It's also simple to add essential components like keys, input fields,\nand a checkbox to wrapper elements, which will then be moved to the root\ncomponent. Because each element in React has its internal logic quickly changed,\nthis strategy increases application growth productivity.\n\n5. It provides quick rendering\nDefining the app's structure early in the design process is crucial. The DOM\nmodel is, in a nutshell, a tree-structured model. Slight UI changes at higher\nlevels might have significant consequences. A virtual DOM created by Facebook\ncan address such differences. As its name indicates, the virtual DOM is a\nvirtual representation that allows testing and evaluating risks before making\nany changes. Faster app performance and a better UX.\n\n6. It's search engine optimized.\nSEO is the key to any internet business's success. Google favors apps that load\nquickly and render quickly. React's quick rendering minimizes webpage load time\ncompared to other platforms, which helps businesses rank higher on Google Search\nEngine Result Pages.\n\n7. It includes a growth toolbox\nIncorporating new technology into projects may be enjoyable and helpful, but\nonly when done correctly. Facebook recognizes this and has introduced\nmuch-needed React and Chrome dev tools to its React structure. These React tools\nlet designers find the child and parent elements, investigate components'\nhierarchical structures, and examine elements' states and properties.\n\n8. Supportive community\nOne of the advantages of utilizing React, like Angular, is the strong community.\nEvery day, thousands of React designers improve the web platform. The project\nhas 136,079 stars and 1,331 regular patrons, at the time of writing this.\nExperts often provide unlimited React tutorial videos and in-depth React papers\nand blogs. Also, React experts frequently answer questions on QA sites, so you\ncan always receive professional help.\n\n9. It improves code stability\nReact ensures that changes in the child structure do not impact the parent\nstructure. So, if a designer modifies an object, they need to edit its states.\nThey will upgrade only one element. This flow of data and architecture improves\nthe code's stability and the application's pace.\n\n10. Numerous Fortune 500 companies use it.\nStill undecided about React?\n\nCheck out some instances of React solutions that are inspiring. Many Fortune 500\norganizations use React for their web pages and mobile apps. Several\nhigh-profile firms use the React framework, including Netflix, PayPal, the BBC,\nLyft, and the Times New York, to cite just a few. Tencent QQ, Walmart, Tesla,\nand Airbnb are just a few well-known companies that have used the React\nstructure in their mobile applications. To put it simply, React is a convenient\nweb and mobile app growth platform.\n\nConclusion\nNow that you know the benefits and best practices of React, you can see why it\nis an excellent framework. Large and mid-sized businesses alike are using React\nto create their web pages' UIs, which should come as no surprise.","html":"<p>Creating interactive end-user Interfaces is difficult regardless of the programming platform. Web Page design takes time, effort, and attention to detail, but <a href=\"https://reactjs.org\">React</a> simplifies the user interface design and developers’ lives.</p><p>You may be curious about using React to build User Interfaces (UI). With its excellent JavaScript structure, the growth process is considerably more manageable.</p><p>Big corporations like Facebook, Airbnb, Netflix, and PayPal use it. You surely don't want to ruin your chance of creating an excellent UI for your project.</p><p>You should <a href=\"https://www.esparkinfo.com/web-app-development/react-js.html\">hire the best React development company</a> to get your hands on highly efficient and competitive React designers.</p><h2 id=\"what-is-react\">What is React</h2><figure class=\"kg-card kg-image-card\"><img src=\"https://lh4.googleusercontent.com/hX9hG8wUHdHJ2rsqOkiQb9fPUC9R3ZQ03g2I6ljzu7iq9D03WLpedXFf6UNwg7yuYVeVf2tO6DRomV3v_Eu4ToY78tNjdE5jpoEVHm6FPmv8kKPEZy7TES0mwQabMnbMG-ghUbV2\" class=\"kg-image\" alt loading=\"lazy\"></figure><p>Facebook created and maintained React, a JavaScript library. React is a practical, straightforward, and adaptable open-source JavaScript library for developing simple, quick, and scalable online apps.</p><p>React is a widely sought-after option for developing user-friendly and engaging web pages and apps that allow developers to be more creative.</p><p>What makes React so popular among businesses and brands?</p><h2 id=\"react-interactive-ui-features\">React Interactive UI Features</h2><h3 id=\"simple-to-use\">Simple to use</h3><p>React is easy to use and offers dynamism to any UI layout. It also enables rapid application progress with good quality, saving time for both clients and designers.</p><h3 id=\"declarative\">Declarative</h3><p>React allows significant data changes to affect specified UI elements automatically. Because of this advanced feature, you do not need to do anything further to upgrade your UI.</p><h3 id=\"reusable-elements\">Reusable Elements</h3><p>Reusability is a cure for developers. React offers tools and frameworks that developers may reuse to build new apps. This platform allows contrivers to reuse segments made for other apps with similar functionality. It reduces growth time and ensures faultless functioning.</p><h3 id=\"libraries-for-javascript-programming\">Libraries for JavaScript programming</h3><p>A potent combination of JavaScript and HTML code is constantly employed, streamlining the whole coding process. The JS library has numerous methods that translate HTML elements into needed functions and simplify the overall project.</p><h3 id=\"assistance-with-components\">Assistance with Components</h3><p>React combines JavaScript and HTML tags. Then React acts as a mediator, representing the DOM and helping decide which element needs to be changed. The HTML elements and JS codes make dealing with large amounts of data easier.</p><h3 id=\"seo-friendly\">SEO-friendly</h3><p>Facebook put a lot of work on React. Likely the best way to construct great, SEO-friendly user experiences spanning browsers and engines.</p><h3 id=\"lightweight-dom-integration\">Lightweight DOM integration</h3><p>Because React is so simple, it perfectly implements the DOM (document object model). It does not utilize the design but allows developers to change the DOM version in memory. This feature automatically improves application performance.</p><h3 id=\"expert-data-binding\">Expert Data Binding</h3><p>React lags one-way data binding. It means anybody can trace all modifications made to any data section. Its simplicity is reflected here.</p><h3 id=\"other-benefits-of-react\">Other Benefits of React</h3><ul><li>Facilitates coding of JavaScript</li><li>Outstanding cross-platform support</li><li>Manages dependencies</li><li>Easy template design</li><li>UI-driven designs</li><li>Simple to adopt</li></ul><h2 id=\"ten-reasons-to-use-react-in-your-project\">Ten Reasons to Use React in Your Project</h2><p>If you've heard of React but aren't sure whether to utilize it in your project, here are the top ten reasons why React could be the best choice.</p><h3 id=\"1-it-is-simple-to-learn\">1. It is simple to learn.</h3><p>Unlike Angular and Vue, React is considerably easier to understand. It's one of the critical reasons React proliferated. It helps firms construct projects rapidly. The more challenging technology or structure is to comprehend, the lengthier it takes to develop. And we humans prefer to shun complex tasks. However, because React is a straightforward platform, enterprises and major brands are more tempted to use it.</p><h3 id=\"2-creating-rich-user-interfaces\">2. Creating rich user interfaces</h3><p>The UI of a program is critical nowadays. A poorly designed UI reduces an application's chances of success. However, a high-quality UI increases the likelihood of people using your program. So, creating rich end-UIs is crucial for a program to exist. The helpful information is that React's declarative elements allow us to construct high-quality, rich UXs.</p><h3 id=\"3-it-supports-custom-components\">3. It supports custom components</h3><p>JSX, a voluntary semantic extension, allows you to design your elements for React. These elements allow HTML quoting and make subcomponent rendering easy for contrivers. JSX is already used to design bespoke elements, construct high-volume apps, and transform HTML mock-ups into ReactElement trees.</p><h3 id=\"4-it-boosts-developers-output\">4. It boosts developers' output</h3><p>Frequent updates can pose problems with complex logic since a single move might substantially affect other aspects. Facebook fixed the issue by making React's elements reusable. Reusable elements allow formulators to reuse digital materials. It's also simple to add essential components like keys, input fields, and a checkbox to wrapper elements, which will then be moved to the root component. Because each element in React has its internal logic quickly changed, this strategy increases application growth productivity.</p><h3 id=\"5-it-provides-quick-rendering\">5. It provides quick rendering</h3><p>Defining the app's structure early in the design process is crucial. The DOM model is, in a nutshell, a tree-structured model. Slight UI changes at higher levels might have significant consequences. A virtual DOM created by Facebook can address such differences. As its name indicates, the virtual DOM is a virtual representation that allows testing and evaluating risks before making any changes. Faster app performance and a better UX.</p><h3 id=\"6-its-search-engine-optimized\">6. It's search engine optimized.</h3><p>SEO is the key to any internet business's success. Google favors apps that load quickly and render quickly. React's quick rendering minimizes webpage load time compared to other platforms, which helps businesses rank higher on Google Search Engine Result Pages.</p><h3 id=\"7-it-includes-a-growth-toolbox\">7. It includes a growth toolbox</h3><p>Incorporating new technology into projects may be enjoyable and helpful, but only when done correctly. Facebook recognizes this and has introduced much-needed React and Chrome dev tools to its React structure. These React tools let designers find the child and parent elements, investigate components' hierarchical structures, and examine elements' states and properties.</p><h3 id=\"8-supportive-community\">8. Supportive community</h3><p>One of the advantages of utilizing React, like Angular, is the strong community. Every day, thousands of React designers improve the web platform. The project has 136,079 stars and 1,331 regular patrons, at the time of writing this. Experts often provide unlimited React tutorial videos and in-depth React papers and blogs. Also, React experts frequently answer questions on QA sites, so you can always receive professional help.</p><h3 id=\"9-it-improves-code-stability\">9. It improves code stability</h3><p>React ensures that changes in the child structure do not impact the parent structure. So, if a designer modifies an object, they need to edit its states. They will upgrade only one element. This flow of data and architecture improves the code's stability and the application's pace.</p><h3 id=\"10-numerous-fortune-500-companies-use-it\">10. Numerous Fortune 500 companies use it.</h3><p>Still undecided about React?</p><p>Check out some instances of React solutions that are inspiring. Many Fortune 500 organizations use React for their web pages and mobile apps. Several high-profile firms use the React framework, including Netflix, PayPal, the BBC, Lyft, and the Times New York, to cite just a few. Tencent QQ, Walmart, Tesla, and Airbnb are just a few well-known companies that have used the React structure in their mobile applications. To put it simply, React is a convenient web and mobile app growth platform.</p><h2 id=\"conclusion\">Conclusion</h2><p>Now that you know the benefits and best practices of React, you can see why it is an excellent framework. Large and mid-sized businesses alike are using React to create their web pages' UIs, which should come as no surprise.</p>","url":"https://backend.shahednasser.com/10-reasons-why-you-should-use-react-for-building-interactive-user-interfaces/","canonical_url":null,"uuid":"bbb1997e-8a87-450e-a3ee-623061efa645","codeinjection_foot":null,"codeinjection_head":null,"codeinjection_styles":null,"comment_id":"61bc7669c393bb64592d8688","reading_time":5,"send_email_when_published":null,"email_subject":null,"childHtmlRehype":{"html":"<p>Creating interactive end-user Interfaces is difficult regardless of the programming platform. Web Page design takes time, effort, and attention to detail, but <a href=\"https://reactjs.org\">React</a> simplifies the user interface design and developers’ lives.</p><p>You may be curious about using React to build User Interfaces (UI). With its excellent JavaScript structure, the growth process is considerably more manageable.</p><p>Big corporations like Facebook, Airbnb, Netflix, and PayPal use it. You surely don't want to ruin your chance of creating an excellent UI for your project.</p><p>You should <a href=\"https://www.esparkinfo.com/web-app-development/react-js.html\">hire the best React development company</a> to get your hands on highly efficient and competitive React designers.</p><h2 id=\"what-is-react\">What is React</h2><figure class=\"kg-card kg-image-card\"><img src=\"https://lh4.googleusercontent.com/hX9hG8wUHdHJ2rsqOkiQb9fPUC9R3ZQ03g2I6ljzu7iq9D03WLpedXFf6UNwg7yuYVeVf2tO6DRomV3v_Eu4ToY78tNjdE5jpoEVHm6FPmv8kKPEZy7TES0mwQabMnbMG-ghUbV2\" class=\"kg-image\" alt=\"\" loading=\"lazy\"></figure><p>Facebook created and maintained React, a JavaScript library. React is a practical, straightforward, and adaptable open-source JavaScript library for developing simple, quick, and scalable online apps.</p><p>React is a widely sought-after option for developing user-friendly and engaging web pages and apps that allow developers to be more creative.</p><p>What makes React so popular among businesses and brands?</p><h2 id=\"react-interactive-ui-features\">React Interactive UI Features</h2><h3 id=\"simple-to-use\">Simple to use</h3><p>React is easy to use and offers dynamism to any UI layout. It also enables rapid application progress with good quality, saving time for both clients and designers.</p><h3 id=\"declarative\">Declarative</h3><p>React allows significant data changes to affect specified UI elements automatically. Because of this advanced feature, you do not need to do anything further to upgrade your UI.</p><h3 id=\"reusable-elements\">Reusable Elements</h3><p>Reusability is a cure for developers. React offers tools and frameworks that developers may reuse to build new apps. This platform allows contrivers to reuse segments made for other apps with similar functionality. It reduces growth time and ensures faultless functioning.</p><h3 id=\"libraries-for-javascript-programming\">Libraries for JavaScript programming</h3><p>A potent combination of JavaScript and HTML code is constantly employed, streamlining the whole coding process. The JS library has numerous methods that translate HTML elements into needed functions and simplify the overall project.</p><h3 id=\"assistance-with-components\">Assistance with Components</h3><p>React combines JavaScript and HTML tags. Then React acts as a mediator, representing the DOM and helping decide which element needs to be changed. The HTML elements and JS codes make dealing with large amounts of data easier.</p><h3 id=\"seo-friendly\">SEO-friendly</h3><p>Facebook put a lot of work on React. Likely the best way to construct great, SEO-friendly user experiences spanning browsers and engines.</p><h3 id=\"lightweight-dom-integration\">Lightweight DOM integration</h3><p>Because React is so simple, it perfectly implements the DOM (document object model). It does not utilize the design but allows developers to change the DOM version in memory. This feature automatically improves application performance.</p><h3 id=\"expert-data-binding\">Expert Data Binding</h3><p>React lags one-way data binding. It means anybody can trace all modifications made to any data section. Its simplicity is reflected here.</p><h3 id=\"other-benefits-of-react\">Other Benefits of React</h3><ul><li>Facilitates coding of JavaScript</li><li>Outstanding cross-platform support</li><li>Manages dependencies</li><li>Easy template design</li><li>UI-driven designs</li><li>Simple to adopt</li></ul><h2 id=\"ten-reasons-to-use-react-in-your-project\">Ten Reasons to Use React in Your Project</h2><p>If you've heard of React but aren't sure whether to utilize it in your project, here are the top ten reasons why React could be the best choice.</p><h3 id=\"1-it-is-simple-to-learn\">1. It is simple to learn.</h3><p>Unlike Angular and Vue, React is considerably easier to understand. It's one of the critical reasons React proliferated. It helps firms construct projects rapidly. The more challenging technology or structure is to comprehend, the lengthier it takes to develop. And we humans prefer to shun complex tasks. However, because React is a straightforward platform, enterprises and major brands are more tempted to use it.</p><h3 id=\"2-creating-rich-user-interfaces\">2. Creating rich user interfaces</h3><p>The UI of a program is critical nowadays. A poorly designed UI reduces an application's chances of success. However, a high-quality UI increases the likelihood of people using your program. So, creating rich end-UIs is crucial for a program to exist. The helpful information is that React's declarative elements allow us to construct high-quality, rich UXs.</p><h3 id=\"3-it-supports-custom-components\">3. It supports custom components</h3><p>JSX, a voluntary semantic extension, allows you to design your elements for React. These elements allow HTML quoting and make subcomponent rendering easy for contrivers. JSX is already used to design bespoke elements, construct high-volume apps, and transform HTML mock-ups into ReactElement trees.</p><h3 id=\"4-it-boosts-developers-output\">4. It boosts developers' output</h3><p>Frequent updates can pose problems with complex logic since a single move might substantially affect other aspects. Facebook fixed the issue by making React's elements reusable. Reusable elements allow formulators to reuse digital materials. It's also simple to add essential components like keys, input fields, and a checkbox to wrapper elements, which will then be moved to the root component. Because each element in React has its internal logic quickly changed, this strategy increases application growth productivity.</p><h3 id=\"5-it-provides-quick-rendering\">5. It provides quick rendering</h3><p>Defining the app's structure early in the design process is crucial. The DOM model is, in a nutshell, a tree-structured model. Slight UI changes at higher levels might have significant consequences. A virtual DOM created by Facebook can address such differences. As its name indicates, the virtual DOM is a virtual representation that allows testing and evaluating risks before making any changes. Faster app performance and a better UX.</p><h3 id=\"6-its-search-engine-optimized\">6. It's search engine optimized.</h3><p>SEO is the key to any internet business's success. Google favors apps that load quickly and render quickly. React's quick rendering minimizes webpage load time compared to other platforms, which helps businesses rank higher on Google Search Engine Result Pages.</p><h3 id=\"7-it-includes-a-growth-toolbox\">7. It includes a growth toolbox</h3><p>Incorporating new technology into projects may be enjoyable and helpful, but only when done correctly. Facebook recognizes this and has introduced much-needed React and Chrome dev tools to its React structure. These React tools let designers find the child and parent elements, investigate components' hierarchical structures, and examine elements' states and properties.</p><h3 id=\"8-supportive-community\">8. Supportive community</h3><p>One of the advantages of utilizing React, like Angular, is the strong community. Every day, thousands of React designers improve the web platform. The project has 136,079 stars and 1,331 regular patrons, at the time of writing this. Experts often provide unlimited React tutorial videos and in-depth React papers and blogs. Also, React experts frequently answer questions on QA sites, so you can always receive professional help.</p><h3 id=\"9-it-improves-code-stability\">9. It improves code stability</h3><p>React ensures that changes in the child structure do not impact the parent structure. So, if a designer modifies an object, they need to edit its states. They will upgrade only one element. This flow of data and architecture improves the code's stability and the application's pace.</p><h3 id=\"10-numerous-fortune-500-companies-use-it\">10. Numerous Fortune 500 companies use it.</h3><p>Still undecided about React?</p><p>Check out some instances of React solutions that are inspiring. Many Fortune 500 organizations use React for their web pages and mobile apps. Several high-profile firms use the React framework, including Netflix, PayPal, the BBC, Lyft, and the Times New York, to cite just a few. Tencent QQ, Walmart, Tesla, and Airbnb are just a few well-known companies that have used the React structure in their mobile applications. To put it simply, React is a convenient web and mobile app growth platform.</p><h2 id=\"conclusion\">Conclusion</h2><p>Now that you know the benefits and best practices of React, you can see why it is an excellent framework. Large and mid-sized businesses alike are using React to create their web pages' UIs, which should come as no surprise.</p>","htmlAst":{"type":"root","children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Creating interactive end-user Interfaces is difficult regardless of the programming platform. Web Page design takes time, effort, and attention to detail, but "},{"type":"element","tagName":"a","properties":{"href":"https://reactjs.org"},"children":[{"type":"text","value":"React"}]},{"type":"text","value":" simplifies the user interface design and developers’ lives."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You may be curious about using React to build User Interfaces (UI). With its excellent JavaScript structure, the growth process is considerably more manageable."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Big corporations like Facebook, Airbnb, Netflix, and PayPal use it. You surely don't want to ruin your chance of creating an excellent UI for your project."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You should "},{"type":"element","tagName":"a","properties":{"href":"https://www.esparkinfo.com/web-app-development/react-js.html"},"children":[{"type":"text","value":"hire the best React development company"}]},{"type":"text","value":" to get your hands on highly efficient and competitive React designers."}]},{"type":"element","tagName":"h2","properties":{"id":"what-is-react"},"children":[{"type":"text","value":"What is React"}]},{"type":"element","tagName":"figure","properties":{"className":["kg-card","kg-image-card"]},"children":[{"type":"element","tagName":"img","properties":{"src":"https://lh4.googleusercontent.com/hX9hG8wUHdHJ2rsqOkiQb9fPUC9R3ZQ03g2I6ljzu7iq9D03WLpedXFf6UNwg7yuYVeVf2tO6DRomV3v_Eu4ToY78tNjdE5jpoEVHm6FPmv8kKPEZy7TES0mwQabMnbMG-ghUbV2","className":["kg-image"],"alt":"","loading":"lazy"},"children":[]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Facebook created and maintained React, a JavaScript library. React is a practical, straightforward, and adaptable open-source JavaScript library for developing simple, quick, and scalable online apps."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"React is a widely sought-after option for developing user-friendly and engaging web pages and apps that allow developers to be more creative."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"What makes React so popular among businesses and brands?"}]},{"type":"element","tagName":"h2","properties":{"id":"react-interactive-ui-features"},"children":[{"type":"text","value":"React Interactive UI Features"}]},{"type":"element","tagName":"h3","properties":{"id":"simple-to-use"},"children":[{"type":"text","value":"Simple to use"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"React is easy to use and offers dynamism to any UI layout. It also enables rapid application progress with good quality, saving time for both clients and designers."}]},{"type":"element","tagName":"h3","properties":{"id":"declarative"},"children":[{"type":"text","value":"Declarative"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"React allows significant data changes to affect specified UI elements automatically. Because of this advanced feature, you do not need to do anything further to upgrade your UI."}]},{"type":"element","tagName":"h3","properties":{"id":"reusable-elements"},"children":[{"type":"text","value":"Reusable Elements"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Reusability is a cure for developers. React offers tools and frameworks that developers may reuse to build new apps. This platform allows contrivers to reuse segments made for other apps with similar functionality. It reduces growth time and ensures faultless functioning."}]},{"type":"element","tagName":"h3","properties":{"id":"libraries-for-javascript-programming"},"children":[{"type":"text","value":"Libraries for JavaScript programming"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A potent combination of JavaScript and HTML code is constantly employed, streamlining the whole coding process. The JS library has numerous methods that translate HTML elements into needed functions and simplify the overall project."}]},{"type":"element","tagName":"h3","properties":{"id":"assistance-with-components"},"children":[{"type":"text","value":"Assistance with Components"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"React combines JavaScript and HTML tags. Then React acts as a mediator, representing the DOM and helping decide which element needs to be changed. The HTML elements and JS codes make dealing with large amounts of data easier."}]},{"type":"element","tagName":"h3","properties":{"id":"seo-friendly"},"children":[{"type":"text","value":"SEO-friendly"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Facebook put a lot of work on React. Likely the best way to construct great, SEO-friendly user experiences spanning browsers and engines."}]},{"type":"element","tagName":"h3","properties":{"id":"lightweight-dom-integration"},"children":[{"type":"text","value":"Lightweight DOM integration"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Because React is so simple, it perfectly implements the DOM (document object model). It does not utilize the design but allows developers to change the DOM version in memory. This feature automatically improves application performance."}]},{"type":"element","tagName":"h3","properties":{"id":"expert-data-binding"},"children":[{"type":"text","value":"Expert Data Binding"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"React lags one-way data binding. It means anybody can trace all modifications made to any data section. Its simplicity is reflected here."}]},{"type":"element","tagName":"h3","properties":{"id":"other-benefits-of-react"},"children":[{"type":"text","value":"Other Benefits of React"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Facilitates coding of JavaScript"}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Outstanding cross-platform support"}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Manages dependencies"}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Easy template design"}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"UI-driven designs"}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Simple to adopt"}]}]},{"type":"element","tagName":"h2","properties":{"id":"ten-reasons-to-use-react-in-your-project"},"children":[{"type":"text","value":"Ten Reasons to Use React in Your Project"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If you've heard of React but aren't sure whether to utilize it in your project, here are the top ten reasons why React could be the best choice."}]},{"type":"element","tagName":"h3","properties":{"id":"1-it-is-simple-to-learn"},"children":[{"type":"text","value":"1. It is simple to learn."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Unlike Angular and Vue, React is considerably easier to understand. It's one of the critical reasons React proliferated. It helps firms construct projects rapidly. The more challenging technology or structure is to comprehend, the lengthier it takes to develop. And we humans prefer to shun complex tasks. However, because React is a straightforward platform, enterprises and major brands are more tempted to use it."}]},{"type":"element","tagName":"h3","properties":{"id":"2-creating-rich-user-interfaces"},"children":[{"type":"text","value":"2. Creating rich user interfaces"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The UI of a program is critical nowadays. A poorly designed UI reduces an application's chances of success. However, a high-quality UI increases the likelihood of people using your program. So, creating rich end-UIs is crucial for a program to exist. The helpful information is that React's declarative elements allow us to construct high-quality, rich UXs."}]},{"type":"element","tagName":"h3","properties":{"id":"3-it-supports-custom-components"},"children":[{"type":"text","value":"3. It supports custom components"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"JSX, a voluntary semantic extension, allows you to design your elements for React. These elements allow HTML quoting and make subcomponent rendering easy for contrivers. JSX is already used to design bespoke elements, construct high-volume apps, and transform HTML mock-ups into ReactElement trees."}]},{"type":"element","tagName":"h3","properties":{"id":"4-it-boosts-developers-output"},"children":[{"type":"text","value":"4. It boosts developers' output"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Frequent updates can pose problems with complex logic since a single move might substantially affect other aspects. Facebook fixed the issue by making React's elements reusable. Reusable elements allow formulators to reuse digital materials. It's also simple to add essential components like keys, input fields, and a checkbox to wrapper elements, which will then be moved to the root component. Because each element in React has its internal logic quickly changed, this strategy increases application growth productivity."}]},{"type":"element","tagName":"h3","properties":{"id":"5-it-provides-quick-rendering"},"children":[{"type":"text","value":"5. It provides quick rendering"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Defining the app's structure early in the design process is crucial. The DOM model is, in a nutshell, a tree-structured model. Slight UI changes at higher levels might have significant consequences. A virtual DOM created by Facebook can address such differences. As its name indicates, the virtual DOM is a virtual representation that allows testing and evaluating risks before making any changes. Faster app performance and a better UX."}]},{"type":"element","tagName":"h3","properties":{"id":"6-its-search-engine-optimized"},"children":[{"type":"text","value":"6. It's search engine optimized."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"SEO is the key to any internet business's success. Google favors apps that load quickly and render quickly. React's quick rendering minimizes webpage load time compared to other platforms, which helps businesses rank higher on Google Search Engine Result Pages."}]},{"type":"element","tagName":"h3","properties":{"id":"7-it-includes-a-growth-toolbox"},"children":[{"type":"text","value":"7. It includes a growth toolbox"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Incorporating new technology into projects may be enjoyable and helpful, but only when done correctly. Facebook recognizes this and has introduced much-needed React and Chrome dev tools to its React structure. These React tools let designers find the child and parent elements, investigate components' hierarchical structures, and examine elements' states and properties."}]},{"type":"element","tagName":"h3","properties":{"id":"8-supportive-community"},"children":[{"type":"text","value":"8. Supportive community"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"One of the advantages of utilizing React, like Angular, is the strong community. Every day, thousands of React designers improve the web platform. The project has 136,079 stars and 1,331 regular patrons, at the time of writing this. Experts often provide unlimited React tutorial videos and in-depth React papers and blogs. Also, React experts frequently answer questions on QA sites, so you can always receive professional help."}]},{"type":"element","tagName":"h3","properties":{"id":"9-it-improves-code-stability"},"children":[{"type":"text","value":"9. It improves code stability"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"React ensures that changes in the child structure do not impact the parent structure. So, if a designer modifies an object, they need to edit its states. They will upgrade only one element. This flow of data and architecture improves the code's stability and the application's pace."}]},{"type":"element","tagName":"h3","properties":{"id":"10-numerous-fortune-500-companies-use-it"},"children":[{"type":"text","value":"10. Numerous Fortune 500 companies use it."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Still undecided about React?"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Check out some instances of React solutions that are inspiring. Many Fortune 500 organizations use React for their web pages and mobile apps. Several high-profile firms use the React framework, including Netflix, PayPal, the BBC, Lyft, and the Times New York, to cite just a few. Tencent QQ, Walmart, Tesla, and Airbnb are just a few well-known companies that have used the React structure in their mobile applications. To put it simply, React is a convenient web and mobile app growth platform."}]},{"type":"element","tagName":"h2","properties":{"id":"conclusion"},"children":[{"type":"text","value":"Conclusion"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Now that you know the benefits and best practices of React, you can see why it is an excellent framework. Large and mid-sized businesses alike are using React to create their web pages' UIs, which should come as no surprise."}]}],"data":{"quirksMode":false}},"tableOfContents":[{"id":"what-is-react","heading":"What is React"},{"id":"react-interactive-ui-features","heading":"React Interactive UI Features","items":[{"id":"simple-to-use","heading":"Simple to use"},{"id":"declarative","heading":"Declarative"},{"id":"reusable-elements","heading":"Reusable Elements"},{"id":"libraries-for-javascript-programming","heading":"Libraries for JavaScript programming"},{"id":"assistance-with-components","heading":"Assistance with Components"},{"id":"seo-friendly","heading":"SEO-friendly"},{"id":"lightweight-dom-integration","heading":"Lightweight DOM integration"},{"id":"expert-data-binding","heading":"Expert Data Binding"},{"id":"other-benefits-of-react","heading":"Other Benefits of React"}]},{"id":"ten-reasons-to-use-react-in-your-project","heading":"Ten Reasons to Use React in Your Project","items":[{"id":"1-it-is-simple-to-learn","heading":"1. It is simple to learn."},{"id":"2-creating-rich-user-interfaces","heading":"2. Creating rich user interfaces"},{"id":"3-it-supports-custom-components","heading":"3. It supports custom components"},{"id":"4-it-boosts-developers-output","heading":"4. It boosts developers' output"},{"id":"5-it-provides-quick-rendering","heading":"5. It provides quick rendering"},{"id":"6-its-search-engine-optimized","heading":"6. It's search engine optimized."},{"id":"7-it-includes-a-growth-toolbox","heading":"7. It includes a growth toolbox"},{"id":"8-supportive-community","heading":"8. Supportive community"},{"id":"9-it-improves-code-stability","heading":"9. It improves code stability"},{"id":"10-numerous-fortune-500-companies-use-it","heading":"10. Numerous Fortune 500 companies use it."}]},{"id":"conclusion","heading":"Conclusion"}]},"featureImageSharp":{"base":"lautaro-andreani-xkBaqlcqeb4-unsplash-2.jpg","publicURL":"/static/be66cede7624ebbc675202ad24146138/lautaro-andreani-xkBaqlcqeb4-unsplash-2.jpg","imageMeta":{"width":1920,"height":1280},"childImageSharp":{"fluid":{"base64":"data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAANABQDASIAAhEBAxEB/8QAGAAAAgMAAAAAAAAAAAAAAAAAAAIBAwX/xAAVAQEBAAAAAAAAAAAAAAAAAAABAP/aAAwDAQACEAMQAAAByq3hGFI//8QAGRAAAgMBAAAAAAAAAAAAAAAAAAECERJB/9oACAEBAAEFAqVDUThJ5f8A/8QAFREBAQAAAAAAAAAAAAAAAAAAABH/2gAIAQMBAT8BR//EABQRAQAAAAAAAAAAAAAAAAAAABD/2gAIAQIBAT8BP//EABYQAAMAAAAAAAAAAAAAAAAAAAARIP/aAAgBAQAGPwKUf//EABsQAQACAwEBAAAAAAAAAAAAAAEAESExURCh/9oACAEBAAE/IdwPnl7EdIFodhYy1xqf/9oADAMBAAIAAwAAABDD3//EABcRAAMBAAAAAAAAAAAAAAAAAAABETH/2gAIAQMBAT8QFC0//8QAFREBAQAAAAAAAAAAAAAAAAAAABH/2gAIAQIBAT8Qiv/EABoQAQEAAwEBAAAAAAAAAAAAAAERACExQZH/2gAIAQEAAT8QWKKl1xiFnN7fcASUmVSsfLnLCpsuJWr1F8mf/9k=","aspectRatio":1.5028901734104045,"src":"/static/be66cede7624ebbc675202ad24146138/d5c54/lautaro-andreani-xkBaqlcqeb4-unsplash-2.jpg","srcSet":"/static/be66cede7624ebbc675202ad24146138/65d8c/lautaro-andreani-xkBaqlcqeb4-unsplash-2.jpg 260w,\n/static/be66cede7624ebbc675202ad24146138/c5f21/lautaro-andreani-xkBaqlcqeb4-unsplash-2.jpg 520w,\n/static/be66cede7624ebbc675202ad24146138/d5c54/lautaro-andreani-xkBaqlcqeb4-unsplash-2.jpg 1040w,\n/static/be66cede7624ebbc675202ad24146138/81a53/lautaro-andreani-xkBaqlcqeb4-unsplash-2.jpg 1560w,\n/static/be66cede7624ebbc675202ad24146138/74fd5/lautaro-andreani-xkBaqlcqeb4-unsplash-2.jpg 1920w","srcWebp":"/static/be66cede7624ebbc675202ad24146138/e4875/lautaro-andreani-xkBaqlcqeb4-unsplash-2.webp","srcSetWebp":"/static/be66cede7624ebbc675202ad24146138/dc8f3/lautaro-andreani-xkBaqlcqeb4-unsplash-2.webp 260w,\n/static/be66cede7624ebbc675202ad24146138/2db4b/lautaro-andreani-xkBaqlcqeb4-unsplash-2.webp 520w,\n/static/be66cede7624ebbc675202ad24146138/e4875/lautaro-andreani-xkBaqlcqeb4-unsplash-2.webp 1040w,\n/static/be66cede7624ebbc675202ad24146138/f5845/lautaro-andreani-xkBaqlcqeb4-unsplash-2.webp 1560w,\n/static/be66cede7624ebbc675202ad24146138/6833b/lautaro-andreani-xkBaqlcqeb4-unsplash-2.webp 1920w","sizes":"(max-width: 1040px) 100vw, 1040px"}}}},"ghostPost":{"id":"Ghost__Post__61bc7669c393bb64592d8688","title":"10 Reasons Why You Should Use React for Building Interactive User Interfaces","slug":"10-reasons-why-you-should-use-react-for-building-interactive-user-interfaces","featured":false,"feature_image":"https://backend.shahednasser.com/content/images/2021/12/lautaro-andreani-xkBaqlcqeb4-unsplash-2.jpg","excerpt":"Web Page design takes time, effort, and attention to detail, but React simplifies the user interface design and developers’ lives.","custom_excerpt":"Web Page design takes time, effort, and attention to detail, but React simplifies the user interface design and developers’ lives.","visibility":"public","created_at_pretty":"17 Dec 2021","published_at_pretty":"17 Dec 2021","updated_at_pretty":"17 Dec 2021","created_at":"2021-12-17T11:37:13.000+00:00","published_at":"2021-12-17T12:04:36.000+00:00","updated_at":"2021-12-17T12:04:36.000+00:00","meta_title":null,"meta_description":null,"og_description":null,"og_image":null,"og_title":null,"twitter_description":null,"twitter_image":null,"twitter_title":null,"authors":[{"slug":"harikrishna","url":"https://backend.shahednasser.com/author/harikrishna/","name":"Harikrishna Kundariya","bio":"A marketer, developer, IoT, ChatBot & Blockchain savvy, designer, co-founder, Director of eSparkBiz Technologies. His 10+ experience enables him to provide digital solutions based on IoT and ChatBot.","cover_image":null,"profile_image":"https://backend.shahednasser.com/content/images/2021/10/Author-Headshot.jpg","location":null,"website":"https://www.esparkinfo.com","twitter":null,"facebook":null,"meta_title":null,"meta_description":null,"coverImageSharp":null,"profileImageSharp":null}],"primary_author":{"slug":"harikrishna","url":"https://backend.shahednasser.com/author/harikrishna/","name":"Harikrishna Kundariya","bio":"A marketer, developer, IoT, ChatBot & Blockchain savvy, designer, co-founder, Director of eSparkBiz Technologies. His 10+ experience enables him to provide digital solutions based on IoT and ChatBot.","cover_image":null,"profile_image":"https://backend.shahednasser.com/content/images/2021/10/Author-Headshot.jpg","location":null,"website":"https://www.esparkinfo.com","twitter":null,"facebook":null,"meta_title":null,"meta_description":null,"coverImageSharp":null,"profileImageSharp":{"base":"Author-Headshot.jpg","publicURL":"/static/e393bccca9ad04da9f27a0afd06cca7d/Author-Headshot.jpg","imageMeta":{"width":250,"height":250},"childImageSharp":{"fluid":{"base64":"data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAUABQDASIAAhEBAxEB/8QAGQABAAIDAAAAAAAAAAAAAAAAAAQFAgMG/8QAFgEBAQEAAAAAAAAAAAAAAAAAAQIA/9oADAMBAAIQAxAAAAGzj4VdHRoairghtwL/AP/EABsQAAICAwEAAAAAAAAAAAAAAAECAwQAERIz/9oACAEBAAEFAptiOEt1lmfgCZkeOwrrc98QbH//xAAVEQEBAAAAAAAAAAAAAAAAAAAgQf/aAAgBAwEBPwGD/8QAFREBAQAAAAAAAAAAAAAAAAAAIEH/2gAIAQIBAT8Bo//EAB8QAAEDAwUAAAAAAAAAAAAAAAEAAhAREmEhIjFBgf/aAAgBAQAGPwLaiC4uGYs7Kq3jKqdCvJ//xAAdEAEBAQACAgMAAAAAAAAAAAABEQAxURAhQaGx/9oACAEBAAE/IU5UaFDjClDR5eDhKPt6M4S/ZgqHzG/DlZLgq97/2gAMAwEAAgADAAAAELcAvv/EABcRAQEBAQAAAAAAAAAAAAAAAAERABD/2gAIAQMBAT8QJ0BN/8QAFxEAAwEAAAAAAAAAAAAAAAAAAAEREP/aAAgBAgEBPxCPVP/EABwQAQACAwEBAQAAAAAAAAAAAAEAESExQWFR8P/aAAgBAQABPxB6zouIXKH2LTwHKN5LOS52lQ0zv1ipNFZ4do1Lq4pXT55E3/O46xUaL1HFl4Yan//Z","aspectRatio":1,"src":"/static/e393bccca9ad04da9f27a0afd06cca7d/31709/Author-Headshot.jpg","srcSet":"/static/e393bccca9ad04da9f27a0afd06cca7d/f340b/Author-Headshot.jpg 28w,\n/static/e393bccca9ad04da9f27a0afd06cca7d/22d64/Author-Headshot.jpg 55w,\n/static/e393bccca9ad04da9f27a0afd06cca7d/31709/Author-Headshot.jpg 110w,\n/static/e393bccca9ad04da9f27a0afd06cca7d/aa249/Author-Headshot.jpg 165w,\n/static/e393bccca9ad04da9f27a0afd06cca7d/0dc33/Author-Headshot.jpg 220w,\n/static/e393bccca9ad04da9f27a0afd06cca7d/84d81/Author-Headshot.jpg 250w","srcWebp":"/static/e393bccca9ad04da9f27a0afd06cca7d/8678c/Author-Headshot.webp","srcSetWebp":"/static/e393bccca9ad04da9f27a0afd06cca7d/59cda/Author-Headshot.webp 28w,\n/static/e393bccca9ad04da9f27a0afd06cca7d/7da75/Author-Headshot.webp 55w,\n/static/e393bccca9ad04da9f27a0afd06cca7d/8678c/Author-Headshot.webp 110w,\n/static/e393bccca9ad04da9f27a0afd06cca7d/f282e/Author-Headshot.webp 165w,\n/static/e393bccca9ad04da9f27a0afd06cca7d/a7b21/Author-Headshot.webp 220w,\n/static/e393bccca9ad04da9f27a0afd06cca7d/1d872/Author-Headshot.webp 250w","sizes":"(max-width: 110px) 100vw, 110px"}}}},"primary_tag":{"slug":"react","url":"https://backend.shahednasser.com/tag/react/","name":"React","visibility":"public","feature_image":"https://images.unsplash.com/photo-1581276879432-15e50529f34b?crop=entropy&cs=tinysrgb&fit=max&fm=jpg&ixid=MnwxMTc3M3wwfDF8c2VhcmNofDN8fHJlYWN0fGVufDB8fHx8MTYyMjYzMzI0MA&ixlib=rb-1.2.1&q=80&w=2000","description":"Learn more about React through tutorials, articles, and tips.","meta_title":null,"meta_description":null,"featureImageSharp":{"base":"photo-1581276879432-15e50529f34b.jpg","publicURL":"/static/7140ea32e1157ba61402d5d67ab846d4/photo-1581276879432-15e50529f34b.jpg","imageMeta":{"width":2000,"height":1333},"childImageSharp":{"fluid":{"base64":"data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAANABQDASIAAhEBAxEB/8QAFwAAAwEAAAAAAAAAAAAAAAAAAAIDBf/EABUBAQEAAAAAAAAAAAAAAAAAAAAB/9oADAMBAAIQAxAAAAHJai1MBP/EABcQAAMBAAAAAAAAAAAAAAAAAAABEBH/2gAIAQEAAQUCuDU//8QAFBEBAAAAAAAAAAAAAAAAAAAAEP/aAAgBAwEBPwE//8QAFREBAQAAAAAAAAAAAAAAAAAAEBH/2gAIAQIBAT8Bh//EABQQAQAAAAAAAAAAAAAAAAAAACD/2gAIAQEABj8CX//EABgQAQEBAQEAAAAAAAAAAAAAAAABESFR/9oACAEBAAE/IYy+rHKDH//aAAwDAQACAAMAAAAQC9//xAAVEQEBAAAAAAAAAAAAAAAAAAAQEf/aAAgBAwEBPxCn/8QAFREBAQAAAAAAAAAAAAAAAAAAARD/2gAIAQIBAT8QUT//xAAaEAADAQADAAAAAAAAAAAAAAAAAREhQWFx/9oACAEBAAE/EMY0neijzwMrrVEm7auUQPNGlP/Z","aspectRatio":1.5028901734104045,"src":"/static/7140ea32e1157ba61402d5d67ab846d4/d5c54/photo-1581276879432-15e50529f34b.jpg","srcSet":"/static/7140ea32e1157ba61402d5d67ab846d4/65d8c/photo-1581276879432-15e50529f34b.jpg 260w,\n/static/7140ea32e1157ba61402d5d67ab846d4/c5f21/photo-1581276879432-15e50529f34b.jpg 520w,\n/static/7140ea32e1157ba61402d5d67ab846d4/d5c54/photo-1581276879432-15e50529f34b.jpg 1040w,\n/static/7140ea32e1157ba61402d5d67ab846d4/81a53/photo-1581276879432-15e50529f34b.jpg 1560w,\n/static/7140ea32e1157ba61402d5d67ab846d4/4e5f3/photo-1581276879432-15e50529f34b.jpg 2000w","srcWebp":"/static/7140ea32e1157ba61402d5d67ab846d4/e4875/photo-1581276879432-15e50529f34b.webp","srcSetWebp":"/static/7140ea32e1157ba61402d5d67ab846d4/dc8f3/photo-1581276879432-15e50529f34b.webp 260w,\n/static/7140ea32e1157ba61402d5d67ab846d4/2db4b/photo-1581276879432-15e50529f34b.webp 520w,\n/static/7140ea32e1157ba61402d5d67ab846d4/e4875/photo-1581276879432-15e50529f34b.webp 1040w,\n/static/7140ea32e1157ba61402d5d67ab846d4/f5845/photo-1581276879432-15e50529f34b.webp 1560w,\n/static/7140ea32e1157ba61402d5d67ab846d4/49d6b/photo-1581276879432-15e50529f34b.webp 2000w","sizes":"(max-width: 1040px) 100vw, 1040px"}}}},"tags":[{"slug":"react","url":"https://backend.shahednasser.com/tag/react/","name":"React","visibility":"public","feature_image":"https://images.unsplash.com/photo-1581276879432-15e50529f34b?crop=entropy&cs=tinysrgb&fit=max&fm=jpg&ixid=MnwxMTc3M3wwfDF8c2VhcmNofDN8fHJlYWN0fGVufDB8fHx8MTYyMjYzMzI0MA&ixlib=rb-1.2.1&q=80&w=2000","description":"Learn more about React through tutorials, articles, and tips.","meta_title":null,"meta_description":null,"featureImageSharp":null},{"slug":"js","url":"https://backend.shahednasser.com/tag/js/","name":"Javascript","visibility":"public","feature_image":"https://backend.shahednasser.com/content/images/2022/01/photo-1592609931095-54a2168ae893-2.jpeg","description":"Learn more about Javascript through tutorials, articles, and tips.","meta_title":null,"meta_description":"Learn more about Javascript through tutorials, articles and tips.","featureImageSharp":null}],"plaintext":"Creating interactive end-user Interfaces is difficult regardless of the\nprogramming platform. Web Page design takes time, effort, and attention to\ndetail, but React [https://reactjs.org] simplifies the user interface design and\ndevelopers’ lives.\n\nYou may be curious about using React to build User Interfaces (UI). With its\nexcellent JavaScript structure, the growth process is considerably more\nmanageable.\n\nBig corporations like Facebook, Airbnb, Netflix, and PayPal use it. You surely\ndon't want to ruin your chance of creating an excellent UI for your project.\n\nYou should hire the best React development company\n[https://www.esparkinfo.com/web-app-development/react-js.html] to get your hands\non highly efficient and competitive React designers.\n\nWhat is React\nFacebook created and maintained React, a JavaScript library. React is a\npractical, straightforward, and adaptable open-source JavaScript library for\ndeveloping simple, quick, and scalable online apps.\n\nReact is a widely sought-after option for developing user-friendly and engaging\nweb pages and apps that allow developers to be more creative.\n\nWhat makes React so popular among businesses and brands?\n\nReact Interactive UI Features\nSimple to use\nReact is easy to use and offers dynamism to any UI layout. It also enables rapid\napplication progress with good quality, saving time for both clients and\ndesigners.\n\nDeclarative\nReact allows significant data changes to affect specified UI elements\nautomatically. Because of this advanced feature, you do not need to do anything\nfurther to upgrade your UI.\n\nReusable Elements\nReusability is a cure for developers. React offers tools and frameworks that\ndevelopers may reuse to build new apps. This platform allows contrivers to reuse\nsegments made for other apps with similar functionality. It reduces growth time\nand ensures faultless functioning.\n\nLibraries for JavaScript programming\nA potent combination of JavaScript and HTML code is constantly employed,\nstreamlining the whole coding process. The JS library has numerous methods that\ntranslate HTML elements into needed functions and simplify the overall project.\n\nAssistance with Components\nReact combines JavaScript and HTML tags. Then React acts as a mediator,\nrepresenting the DOM and helping decide which element needs to be changed. The\nHTML elements and JS codes make dealing with large amounts of data easier.\n\nSEO-friendly\nFacebook put a lot of work on React. Likely the best way to construct great,\nSEO-friendly user experiences spanning browsers and engines.\n\nLightweight DOM integration\nBecause React is so simple, it perfectly implements the DOM (document object\nmodel). It does not utilize the design but allows developers to change the DOM\nversion in memory. This feature automatically improves application performance.\n\nExpert Data Binding\nReact lags one-way data binding. It means anybody can trace all modifications\nmade to any data section. Its simplicity is reflected here.\n\nOther Benefits of React\n * Facilitates coding of JavaScript\n * Outstanding cross-platform support\n * Manages dependencies\n * Easy template design\n * UI-driven designs\n * Simple to adopt\n\nTen Reasons to Use React in Your Project\nIf you've heard of React but aren't sure whether to utilize it in your project,\nhere are the top ten reasons why React could be the best choice.\n\n1. It is simple to learn.\nUnlike Angular and Vue, React is considerably easier to understand. It's one of\nthe critical reasons React proliferated. It helps firms construct projects\nrapidly. The more challenging technology or structure is to comprehend, the\nlengthier it takes to develop. And we humans prefer to shun complex tasks.\nHowever, because React is a straightforward platform, enterprises and major\nbrands are more tempted to use it.\n\n2. Creating rich user interfaces\nThe UI of a program is critical nowadays. A poorly designed UI reduces an\napplication's chances of success. However, a high-quality UI increases the\nlikelihood of people using your program. So, creating rich end-UIs is crucial\nfor a program to exist. The helpful information is that React's declarative\nelements allow us to construct high-quality, rich UXs.\n\n3. It supports custom components\nJSX, a voluntary semantic extension, allows you to design your elements for\nReact. These elements allow HTML quoting and make subcomponent rendering easy\nfor contrivers. JSX is already used to design bespoke elements, construct\nhigh-volume apps, and transform HTML mock-ups into ReactElement trees.\n\n4. It boosts developers' output\nFrequent updates can pose problems with complex logic since a single move might\nsubstantially affect other aspects. Facebook fixed the issue by making React's\nelements reusable. Reusable elements allow formulators to reuse digital\nmaterials. It's also simple to add essential components like keys, input fields,\nand a checkbox to wrapper elements, which will then be moved to the root\ncomponent. Because each element in React has its internal logic quickly changed,\nthis strategy increases application growth productivity.\n\n5. It provides quick rendering\nDefining the app's structure early in the design process is crucial. The DOM\nmodel is, in a nutshell, a tree-structured model. Slight UI changes at higher\nlevels might have significant consequences. A virtual DOM created by Facebook\ncan address such differences. As its name indicates, the virtual DOM is a\nvirtual representation that allows testing and evaluating risks before making\nany changes. Faster app performance and a better UX.\n\n6. It's search engine optimized.\nSEO is the key to any internet business's success. Google favors apps that load\nquickly and render quickly. React's quick rendering minimizes webpage load time\ncompared to other platforms, which helps businesses rank higher on Google Search\nEngine Result Pages.\n\n7. It includes a growth toolbox\nIncorporating new technology into projects may be enjoyable and helpful, but\nonly when done correctly. Facebook recognizes this and has introduced\nmuch-needed React and Chrome dev tools to its React structure. These React tools\nlet designers find the child and parent elements, investigate components'\nhierarchical structures, and examine elements' states and properties.\n\n8. Supportive community\nOne of the advantages of utilizing React, like Angular, is the strong community.\nEvery day, thousands of React designers improve the web platform. The project\nhas 136,079 stars and 1,331 regular patrons, at the time of writing this.\nExperts often provide unlimited React tutorial videos and in-depth React papers\nand blogs. Also, React experts frequently answer questions on QA sites, so you\ncan always receive professional help.\n\n9. It improves code stability\nReact ensures that changes in the child structure do not impact the parent\nstructure. So, if a designer modifies an object, they need to edit its states.\nThey will upgrade only one element. This flow of data and architecture improves\nthe code's stability and the application's pace.\n\n10. Numerous Fortune 500 companies use it.\nStill undecided about React?\n\nCheck out some instances of React solutions that are inspiring. Many Fortune 500\norganizations use React for their web pages and mobile apps. Several\nhigh-profile firms use the React framework, including Netflix, PayPal, the BBC,\nLyft, and the Times New York, to cite just a few. Tencent QQ, Walmart, Tesla,\nand Airbnb are just a few well-known companies that have used the React\nstructure in their mobile applications. To put it simply, React is a convenient\nweb and mobile app growth platform.\n\nConclusion\nNow that you know the benefits and best practices of React, you can see why it\nis an excellent framework. Large and mid-sized businesses alike are using React\nto create their web pages' UIs, which should come as no surprise.","html":"<p>Creating interactive end-user Interfaces is difficult regardless of the programming platform. Web Page design takes time, effort, and attention to detail, but <a href=\"https://reactjs.org\">React</a> simplifies the user interface design and developers’ lives.</p><p>You may be curious about using React to build User Interfaces (UI). With its excellent JavaScript structure, the growth process is considerably more manageable.</p><p>Big corporations like Facebook, Airbnb, Netflix, and PayPal use it. You surely don't want to ruin your chance of creating an excellent UI for your project.</p><p>You should <a href=\"https://www.esparkinfo.com/web-app-development/react-js.html\">hire the best React development company</a> to get your hands on highly efficient and competitive React designers.</p><h2 id=\"what-is-react\">What is React</h2><figure class=\"kg-card kg-image-card\"><img src=\"https://lh4.googleusercontent.com/hX9hG8wUHdHJ2rsqOkiQb9fPUC9R3ZQ03g2I6ljzu7iq9D03WLpedXFf6UNwg7yuYVeVf2tO6DRomV3v_Eu4ToY78tNjdE5jpoEVHm6FPmv8kKPEZy7TES0mwQabMnbMG-ghUbV2\" class=\"kg-image\" alt loading=\"lazy\"></figure><p>Facebook created and maintained React, a JavaScript library. React is a practical, straightforward, and adaptable open-source JavaScript library for developing simple, quick, and scalable online apps.</p><p>React is a widely sought-after option for developing user-friendly and engaging web pages and apps that allow developers to be more creative.</p><p>What makes React so popular among businesses and brands?</p><h2 id=\"react-interactive-ui-features\">React Interactive UI Features</h2><h3 id=\"simple-to-use\">Simple to use</h3><p>React is easy to use and offers dynamism to any UI layout. It also enables rapid application progress with good quality, saving time for both clients and designers.</p><h3 id=\"declarative\">Declarative</h3><p>React allows significant data changes to affect specified UI elements automatically. Because of this advanced feature, you do not need to do anything further to upgrade your UI.</p><h3 id=\"reusable-elements\">Reusable Elements</h3><p>Reusability is a cure for developers. React offers tools and frameworks that developers may reuse to build new apps. This platform allows contrivers to reuse segments made for other apps with similar functionality. It reduces growth time and ensures faultless functioning.</p><h3 id=\"libraries-for-javascript-programming\">Libraries for JavaScript programming</h3><p>A potent combination of JavaScript and HTML code is constantly employed, streamlining the whole coding process. The JS library has numerous methods that translate HTML elements into needed functions and simplify the overall project.</p><h3 id=\"assistance-with-components\">Assistance with Components</h3><p>React combines JavaScript and HTML tags. Then React acts as a mediator, representing the DOM and helping decide which element needs to be changed. The HTML elements and JS codes make dealing with large amounts of data easier.</p><h3 id=\"seo-friendly\">SEO-friendly</h3><p>Facebook put a lot of work on React. Likely the best way to construct great, SEO-friendly user experiences spanning browsers and engines.</p><h3 id=\"lightweight-dom-integration\">Lightweight DOM integration</h3><p>Because React is so simple, it perfectly implements the DOM (document object model). It does not utilize the design but allows developers to change the DOM version in memory. This feature automatically improves application performance.</p><h3 id=\"expert-data-binding\">Expert Data Binding</h3><p>React lags one-way data binding. It means anybody can trace all modifications made to any data section. Its simplicity is reflected here.</p><h3 id=\"other-benefits-of-react\">Other Benefits of React</h3><ul><li>Facilitates coding of JavaScript</li><li>Outstanding cross-platform support</li><li>Manages dependencies</li><li>Easy template design</li><li>UI-driven designs</li><li>Simple to adopt</li></ul><h2 id=\"ten-reasons-to-use-react-in-your-project\">Ten Reasons to Use React in Your Project</h2><p>If you've heard of React but aren't sure whether to utilize it in your project, here are the top ten reasons why React could be the best choice.</p><h3 id=\"1-it-is-simple-to-learn\">1. It is simple to learn.</h3><p>Unlike Angular and Vue, React is considerably easier to understand. It's one of the critical reasons React proliferated. It helps firms construct projects rapidly. The more challenging technology or structure is to comprehend, the lengthier it takes to develop. And we humans prefer to shun complex tasks. However, because React is a straightforward platform, enterprises and major brands are more tempted to use it.</p><h3 id=\"2-creating-rich-user-interfaces\">2. Creating rich user interfaces</h3><p>The UI of a program is critical nowadays. A poorly designed UI reduces an application's chances of success. However, a high-quality UI increases the likelihood of people using your program. So, creating rich end-UIs is crucial for a program to exist. The helpful information is that React's declarative elements allow us to construct high-quality, rich UXs.</p><h3 id=\"3-it-supports-custom-components\">3. It supports custom components</h3><p>JSX, a voluntary semantic extension, allows you to design your elements for React. These elements allow HTML quoting and make subcomponent rendering easy for contrivers. JSX is already used to design bespoke elements, construct high-volume apps, and transform HTML mock-ups into ReactElement trees.</p><h3 id=\"4-it-boosts-developers-output\">4. It boosts developers' output</h3><p>Frequent updates can pose problems with complex logic since a single move might substantially affect other aspects. Facebook fixed the issue by making React's elements reusable. Reusable elements allow formulators to reuse digital materials. It's also simple to add essential components like keys, input fields, and a checkbox to wrapper elements, which will then be moved to the root component. Because each element in React has its internal logic quickly changed, this strategy increases application growth productivity.</p><h3 id=\"5-it-provides-quick-rendering\">5. It provides quick rendering</h3><p>Defining the app's structure early in the design process is crucial. The DOM model is, in a nutshell, a tree-structured model. Slight UI changes at higher levels might have significant consequences. A virtual DOM created by Facebook can address such differences. As its name indicates, the virtual DOM is a virtual representation that allows testing and evaluating risks before making any changes. Faster app performance and a better UX.</p><h3 id=\"6-its-search-engine-optimized\">6. It's search engine optimized.</h3><p>SEO is the key to any internet business's success. Google favors apps that load quickly and render quickly. React's quick rendering minimizes webpage load time compared to other platforms, which helps businesses rank higher on Google Search Engine Result Pages.</p><h3 id=\"7-it-includes-a-growth-toolbox\">7. It includes a growth toolbox</h3><p>Incorporating new technology into projects may be enjoyable and helpful, but only when done correctly. Facebook recognizes this and has introduced much-needed React and Chrome dev tools to its React structure. These React tools let designers find the child and parent elements, investigate components' hierarchical structures, and examine elements' states and properties.</p><h3 id=\"8-supportive-community\">8. Supportive community</h3><p>One of the advantages of utilizing React, like Angular, is the strong community. Every day, thousands of React designers improve the web platform. The project has 136,079 stars and 1,331 regular patrons, at the time of writing this. Experts often provide unlimited React tutorial videos and in-depth React papers and blogs. Also, React experts frequently answer questions on QA sites, so you can always receive professional help.</p><h3 id=\"9-it-improves-code-stability\">9. It improves code stability</h3><p>React ensures that changes in the child structure do not impact the parent structure. So, if a designer modifies an object, they need to edit its states. They will upgrade only one element. This flow of data and architecture improves the code's stability and the application's pace.</p><h3 id=\"10-numerous-fortune-500-companies-use-it\">10. Numerous Fortune 500 companies use it.</h3><p>Still undecided about React?</p><p>Check out some instances of React solutions that are inspiring. Many Fortune 500 organizations use React for their web pages and mobile apps. Several high-profile firms use the React framework, including Netflix, PayPal, the BBC, Lyft, and the Times New York, to cite just a few. Tencent QQ, Walmart, Tesla, and Airbnb are just a few well-known companies that have used the React structure in their mobile applications. To put it simply, React is a convenient web and mobile app growth platform.</p><h2 id=\"conclusion\">Conclusion</h2><p>Now that you know the benefits and best practices of React, you can see why it is an excellent framework. Large and mid-sized businesses alike are using React to create their web pages' UIs, which should come as no surprise.</p>","url":"https://backend.shahednasser.com/10-reasons-why-you-should-use-react-for-building-interactive-user-interfaces/","canonical_url":null,"uuid":"bbb1997e-8a87-450e-a3ee-623061efa645","codeinjection_foot":null,"codeinjection_head":null,"codeinjection_styles":null,"comment_id":"61bc7669c393bb64592d8688","reading_time":5,"send_email_when_published":null,"email_subject":null,"childHtmlRehype":{"html":"<p>Creating interactive end-user Interfaces is difficult regardless of the programming platform. Web Page design takes time, effort, and attention to detail, but <a href=\"https://reactjs.org\">React</a> simplifies the user interface design and developers’ lives.</p><p>You may be curious about using React to build User Interfaces (UI). With its excellent JavaScript structure, the growth process is considerably more manageable.</p><p>Big corporations like Facebook, Airbnb, Netflix, and PayPal use it. You surely don't want to ruin your chance of creating an excellent UI for your project.</p><p>You should <a href=\"https://www.esparkinfo.com/web-app-development/react-js.html\">hire the best React development company</a> to get your hands on highly efficient and competitive React designers.</p><h2 id=\"what-is-react\">What is React</h2><figure class=\"kg-card kg-image-card\"><img src=\"https://lh4.googleusercontent.com/hX9hG8wUHdHJ2rsqOkiQb9fPUC9R3ZQ03g2I6ljzu7iq9D03WLpedXFf6UNwg7yuYVeVf2tO6DRomV3v_Eu4ToY78tNjdE5jpoEVHm6FPmv8kKPEZy7TES0mwQabMnbMG-ghUbV2\" class=\"kg-image\" alt=\"\" loading=\"lazy\"></figure><p>Facebook created and maintained React, a JavaScript library. React is a practical, straightforward, and adaptable open-source JavaScript library for developing simple, quick, and scalable online apps.</p><p>React is a widely sought-after option for developing user-friendly and engaging web pages and apps that allow developers to be more creative.</p><p>What makes React so popular among businesses and brands?</p><h2 id=\"react-interactive-ui-features\">React Interactive UI Features</h2><h3 id=\"simple-to-use\">Simple to use</h3><p>React is easy to use and offers dynamism to any UI layout. It also enables rapid application progress with good quality, saving time for both clients and designers.</p><h3 id=\"declarative\">Declarative</h3><p>React allows significant data changes to affect specified UI elements automatically. Because of this advanced feature, you do not need to do anything further to upgrade your UI.</p><h3 id=\"reusable-elements\">Reusable Elements</h3><p>Reusability is a cure for developers. React offers tools and frameworks that developers may reuse to build new apps. This platform allows contrivers to reuse segments made for other apps with similar functionality. It reduces growth time and ensures faultless functioning.</p><h3 id=\"libraries-for-javascript-programming\">Libraries for JavaScript programming</h3><p>A potent combination of JavaScript and HTML code is constantly employed, streamlining the whole coding process. The JS library has numerous methods that translate HTML elements into needed functions and simplify the overall project.</p><h3 id=\"assistance-with-components\">Assistance with Components</h3><p>React combines JavaScript and HTML tags. Then React acts as a mediator, representing the DOM and helping decide which element needs to be changed. The HTML elements and JS codes make dealing with large amounts of data easier.</p><h3 id=\"seo-friendly\">SEO-friendly</h3><p>Facebook put a lot of work on React. Likely the best way to construct great, SEO-friendly user experiences spanning browsers and engines.</p><h3 id=\"lightweight-dom-integration\">Lightweight DOM integration</h3><p>Because React is so simple, it perfectly implements the DOM (document object model). It does not utilize the design but allows developers to change the DOM version in memory. This feature automatically improves application performance.</p><h3 id=\"expert-data-binding\">Expert Data Binding</h3><p>React lags one-way data binding. It means anybody can trace all modifications made to any data section. Its simplicity is reflected here.</p><h3 id=\"other-benefits-of-react\">Other Benefits of React</h3><ul><li>Facilitates coding of JavaScript</li><li>Outstanding cross-platform support</li><li>Manages dependencies</li><li>Easy template design</li><li>UI-driven designs</li><li>Simple to adopt</li></ul><h2 id=\"ten-reasons-to-use-react-in-your-project\">Ten Reasons to Use React in Your Project</h2><p>If you've heard of React but aren't sure whether to utilize it in your project, here are the top ten reasons why React could be the best choice.</p><h3 id=\"1-it-is-simple-to-learn\">1. It is simple to learn.</h3><p>Unlike Angular and Vue, React is considerably easier to understand. It's one of the critical reasons React proliferated. It helps firms construct projects rapidly. The more challenging technology or structure is to comprehend, the lengthier it takes to develop. And we humans prefer to shun complex tasks. However, because React is a straightforward platform, enterprises and major brands are more tempted to use it.</p><h3 id=\"2-creating-rich-user-interfaces\">2. Creating rich user interfaces</h3><p>The UI of a program is critical nowadays. A poorly designed UI reduces an application's chances of success. However, a high-quality UI increases the likelihood of people using your program. So, creating rich end-UIs is crucial for a program to exist. The helpful information is that React's declarative elements allow us to construct high-quality, rich UXs.</p><h3 id=\"3-it-supports-custom-components\">3. It supports custom components</h3><p>JSX, a voluntary semantic extension, allows you to design your elements for React. These elements allow HTML quoting and make subcomponent rendering easy for contrivers. JSX is already used to design bespoke elements, construct high-volume apps, and transform HTML mock-ups into ReactElement trees.</p><h3 id=\"4-it-boosts-developers-output\">4. It boosts developers' output</h3><p>Frequent updates can pose problems with complex logic since a single move might substantially affect other aspects. Facebook fixed the issue by making React's elements reusable. Reusable elements allow formulators to reuse digital materials. It's also simple to add essential components like keys, input fields, and a checkbox to wrapper elements, which will then be moved to the root component. Because each element in React has its internal logic quickly changed, this strategy increases application growth productivity.</p><h3 id=\"5-it-provides-quick-rendering\">5. It provides quick rendering</h3><p>Defining the app's structure early in the design process is crucial. The DOM model is, in a nutshell, a tree-structured model. Slight UI changes at higher levels might have significant consequences. A virtual DOM created by Facebook can address such differences. As its name indicates, the virtual DOM is a virtual representation that allows testing and evaluating risks before making any changes. Faster app performance and a better UX.</p><h3 id=\"6-its-search-engine-optimized\">6. It's search engine optimized.</h3><p>SEO is the key to any internet business's success. Google favors apps that load quickly and render quickly. React's quick rendering minimizes webpage load time compared to other platforms, which helps businesses rank higher on Google Search Engine Result Pages.</p><h3 id=\"7-it-includes-a-growth-toolbox\">7. It includes a growth toolbox</h3><p>Incorporating new technology into projects may be enjoyable and helpful, but only when done correctly. Facebook recognizes this and has introduced much-needed React and Chrome dev tools to its React structure. These React tools let designers find the child and parent elements, investigate components' hierarchical structures, and examine elements' states and properties.</p><h3 id=\"8-supportive-community\">8. Supportive community</h3><p>One of the advantages of utilizing React, like Angular, is the strong community. Every day, thousands of React designers improve the web platform. The project has 136,079 stars and 1,331 regular patrons, at the time of writing this. Experts often provide unlimited React tutorial videos and in-depth React papers and blogs. Also, React experts frequently answer questions on QA sites, so you can always receive professional help.</p><h3 id=\"9-it-improves-code-stability\">9. It improves code stability</h3><p>React ensures that changes in the child structure do not impact the parent structure. So, if a designer modifies an object, they need to edit its states. They will upgrade only one element. This flow of data and architecture improves the code's stability and the application's pace.</p><h3 id=\"10-numerous-fortune-500-companies-use-it\">10. Numerous Fortune 500 companies use it.</h3><p>Still undecided about React?</p><p>Check out some instances of React solutions that are inspiring. Many Fortune 500 organizations use React for their web pages and mobile apps. Several high-profile firms use the React framework, including Netflix, PayPal, the BBC, Lyft, and the Times New York, to cite just a few. Tencent QQ, Walmart, Tesla, and Airbnb are just a few well-known companies that have used the React structure in their mobile applications. To put it simply, React is a convenient web and mobile app growth platform.</p><h2 id=\"conclusion\">Conclusion</h2><p>Now that you know the benefits and best practices of React, you can see why it is an excellent framework. Large and mid-sized businesses alike are using React to create their web pages' UIs, which should come as no surprise.</p>","htmlAst":{"type":"root","children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Creating interactive end-user Interfaces is difficult regardless of the programming platform. Web Page design takes time, effort, and attention to detail, but "},{"type":"element","tagName":"a","properties":{"href":"https://reactjs.org"},"children":[{"type":"text","value":"React"}]},{"type":"text","value":" simplifies the user interface design and developers’ lives."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You may be curious about using React to build User Interfaces (UI). With its excellent JavaScript structure, the growth process is considerably more manageable."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Big corporations like Facebook, Airbnb, Netflix, and PayPal use it. You surely don't want to ruin your chance of creating an excellent UI for your project."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You should "},{"type":"element","tagName":"a","properties":{"href":"https://www.esparkinfo.com/web-app-development/react-js.html"},"children":[{"type":"text","value":"hire the best React development company"}]},{"type":"text","value":" to get your hands on highly efficient and competitive React designers."}]},{"type":"element","tagName":"h2","properties":{"id":"what-is-react"},"children":[{"type":"text","value":"What is React"}]},{"type":"element","tagName":"figure","properties":{"className":["kg-card","kg-image-card"]},"children":[{"type":"element","tagName":"img","properties":{"src":"https://lh4.googleusercontent.com/hX9hG8wUHdHJ2rsqOkiQb9fPUC9R3ZQ03g2I6ljzu7iq9D03WLpedXFf6UNwg7yuYVeVf2tO6DRomV3v_Eu4ToY78tNjdE5jpoEVHm6FPmv8kKPEZy7TES0mwQabMnbMG-ghUbV2","className":["kg-image"],"alt":"","loading":"lazy"},"children":[]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Facebook created and maintained React, a JavaScript library. React is a practical, straightforward, and adaptable open-source JavaScript library for developing simple, quick, and scalable online apps."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"React is a widely sought-after option for developing user-friendly and engaging web pages and apps that allow developers to be more creative."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"What makes React so popular among businesses and brands?"}]},{"type":"element","tagName":"h2","properties":{"id":"react-interactive-ui-features"},"children":[{"type":"text","value":"React Interactive UI Features"}]},{"type":"element","tagName":"h3","properties":{"id":"simple-to-use"},"children":[{"type":"text","value":"Simple to use"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"React is easy to use and offers dynamism to any UI layout. It also enables rapid application progress with good quality, saving time for both clients and designers."}]},{"type":"element","tagName":"h3","properties":{"id":"declarative"},"children":[{"type":"text","value":"Declarative"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"React allows significant data changes to affect specified UI elements automatically. Because of this advanced feature, you do not need to do anything further to upgrade your UI."}]},{"type":"element","tagName":"h3","properties":{"id":"reusable-elements"},"children":[{"type":"text","value":"Reusable Elements"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Reusability is a cure for developers. React offers tools and frameworks that developers may reuse to build new apps. This platform allows contrivers to reuse segments made for other apps with similar functionality. It reduces growth time and ensures faultless functioning."}]},{"type":"element","tagName":"h3","properties":{"id":"libraries-for-javascript-programming"},"children":[{"type":"text","value":"Libraries for JavaScript programming"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A potent combination of JavaScript and HTML code is constantly employed, streamlining the whole coding process. The JS library has numerous methods that translate HTML elements into needed functions and simplify the overall project."}]},{"type":"element","tagName":"h3","properties":{"id":"assistance-with-components"},"children":[{"type":"text","value":"Assistance with Components"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"React combines JavaScript and HTML tags. Then React acts as a mediator, representing the DOM and helping decide which element needs to be changed. The HTML elements and JS codes make dealing with large amounts of data easier."}]},{"type":"element","tagName":"h3","properties":{"id":"seo-friendly"},"children":[{"type":"text","value":"SEO-friendly"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Facebook put a lot of work on React. Likely the best way to construct great, SEO-friendly user experiences spanning browsers and engines."}]},{"type":"element","tagName":"h3","properties":{"id":"lightweight-dom-integration"},"children":[{"type":"text","value":"Lightweight DOM integration"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Because React is so simple, it perfectly implements the DOM (document object model). It does not utilize the design but allows developers to change the DOM version in memory. This feature automatically improves application performance."}]},{"type":"element","tagName":"h3","properties":{"id":"expert-data-binding"},"children":[{"type":"text","value":"Expert Data Binding"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"React lags one-way data binding. It means anybody can trace all modifications made to any data section. Its simplicity is reflected here."}]},{"type":"element","tagName":"h3","properties":{"id":"other-benefits-of-react"},"children":[{"type":"text","value":"Other Benefits of React"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Facilitates coding of JavaScript"}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Outstanding cross-platform support"}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Manages dependencies"}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Easy template design"}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"UI-driven designs"}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Simple to adopt"}]}]},{"type":"element","tagName":"h2","properties":{"id":"ten-reasons-to-use-react-in-your-project"},"children":[{"type":"text","value":"Ten Reasons to Use React in Your Project"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If you've heard of React but aren't sure whether to utilize it in your project, here are the top ten reasons why React could be the best choice."}]},{"type":"element","tagName":"h3","properties":{"id":"1-it-is-simple-to-learn"},"children":[{"type":"text","value":"1. It is simple to learn."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Unlike Angular and Vue, React is considerably easier to understand. It's one of the critical reasons React proliferated. It helps firms construct projects rapidly. The more challenging technology or structure is to comprehend, the lengthier it takes to develop. And we humans prefer to shun complex tasks. However, because React is a straightforward platform, enterprises and major brands are more tempted to use it."}]},{"type":"element","tagName":"h3","properties":{"id":"2-creating-rich-user-interfaces"},"children":[{"type":"text","value":"2. Creating rich user interfaces"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The UI of a program is critical nowadays. A poorly designed UI reduces an application's chances of success. However, a high-quality UI increases the likelihood of people using your program. So, creating rich end-UIs is crucial for a program to exist. The helpful information is that React's declarative elements allow us to construct high-quality, rich UXs."}]},{"type":"element","tagName":"h3","properties":{"id":"3-it-supports-custom-components"},"children":[{"type":"text","value":"3. It supports custom components"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"JSX, a voluntary semantic extension, allows you to design your elements for React. These elements allow HTML quoting and make subcomponent rendering easy for contrivers. JSX is already used to design bespoke elements, construct high-volume apps, and transform HTML mock-ups into ReactElement trees."}]},{"type":"element","tagName":"h3","properties":{"id":"4-it-boosts-developers-output"},"children":[{"type":"text","value":"4. It boosts developers' output"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Frequent updates can pose problems with complex logic since a single move might substantially affect other aspects. Facebook fixed the issue by making React's elements reusable. Reusable elements allow formulators to reuse digital materials. It's also simple to add essential components like keys, input fields, and a checkbox to wrapper elements, which will then be moved to the root component. Because each element in React has its internal logic quickly changed, this strategy increases application growth productivity."}]},{"type":"element","tagName":"h3","properties":{"id":"5-it-provides-quick-rendering"},"children":[{"type":"text","value":"5. It provides quick rendering"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Defining the app's structure early in the design process is crucial. The DOM model is, in a nutshell, a tree-structured model. Slight UI changes at higher levels might have significant consequences. A virtual DOM created by Facebook can address such differences. As its name indicates, the virtual DOM is a virtual representation that allows testing and evaluating risks before making any changes. Faster app performance and a better UX."}]},{"type":"element","tagName":"h3","properties":{"id":"6-its-search-engine-optimized"},"children":[{"type":"text","value":"6. It's search engine optimized."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"SEO is the key to any internet business's success. Google favors apps that load quickly and render quickly. React's quick rendering minimizes webpage load time compared to other platforms, which helps businesses rank higher on Google Search Engine Result Pages."}]},{"type":"element","tagName":"h3","properties":{"id":"7-it-includes-a-growth-toolbox"},"children":[{"type":"text","value":"7. It includes a growth toolbox"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Incorporating new technology into projects may be enjoyable and helpful, but only when done correctly. Facebook recognizes this and has introduced much-needed React and Chrome dev tools to its React structure. These React tools let designers find the child and parent elements, investigate components' hierarchical structures, and examine elements' states and properties."}]},{"type":"element","tagName":"h3","properties":{"id":"8-supportive-community"},"children":[{"type":"text","value":"8. Supportive community"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"One of the advantages of utilizing React, like Angular, is the strong community. Every day, thousands of React designers improve the web platform. The project has 136,079 stars and 1,331 regular patrons, at the time of writing this. Experts often provide unlimited React tutorial videos and in-depth React papers and blogs. Also, React experts frequently answer questions on QA sites, so you can always receive professional help."}]},{"type":"element","tagName":"h3","properties":{"id":"9-it-improves-code-stability"},"children":[{"type":"text","value":"9. It improves code stability"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"React ensures that changes in the child structure do not impact the parent structure. So, if a designer modifies an object, they need to edit its states. They will upgrade only one element. This flow of data and architecture improves the code's stability and the application's pace."}]},{"type":"element","tagName":"h3","properties":{"id":"10-numerous-fortune-500-companies-use-it"},"children":[{"type":"text","value":"10. Numerous Fortune 500 companies use it."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Still undecided about React?"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Check out some instances of React solutions that are inspiring. Many Fortune 500 organizations use React for their web pages and mobile apps. Several high-profile firms use the React framework, including Netflix, PayPal, the BBC, Lyft, and the Times New York, to cite just a few. Tencent QQ, Walmart, Tesla, and Airbnb are just a few well-known companies that have used the React structure in their mobile applications. To put it simply, React is a convenient web and mobile app growth platform."}]},{"type":"element","tagName":"h2","properties":{"id":"conclusion"},"children":[{"type":"text","value":"Conclusion"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Now that you know the benefits and best practices of React, you can see why it is an excellent framework. Large and mid-sized businesses alike are using React to create their web pages' UIs, which should come as no surprise."}]}],"data":{"quirksMode":false}},"tableOfContents":[{"id":"what-is-react","heading":"What is React"},{"id":"react-interactive-ui-features","heading":"React Interactive UI Features","items":[{"id":"simple-to-use","heading":"Simple to use"},{"id":"declarative","heading":"Declarative"},{"id":"reusable-elements","heading":"Reusable Elements"},{"id":"libraries-for-javascript-programming","heading":"Libraries for JavaScript programming"},{"id":"assistance-with-components","heading":"Assistance with Components"},{"id":"seo-friendly","heading":"SEO-friendly"},{"id":"lightweight-dom-integration","heading":"Lightweight DOM integration"},{"id":"expert-data-binding","heading":"Expert Data Binding"},{"id":"other-benefits-of-react","heading":"Other Benefits of React"}]},{"id":"ten-reasons-to-use-react-in-your-project","heading":"Ten Reasons to Use React in Your Project","items":[{"id":"1-it-is-simple-to-learn","heading":"1. It is simple to learn."},{"id":"2-creating-rich-user-interfaces","heading":"2. Creating rich user interfaces"},{"id":"3-it-supports-custom-components","heading":"3. It supports custom components"},{"id":"4-it-boosts-developers-output","heading":"4. It boosts developers' output"},{"id":"5-it-provides-quick-rendering","heading":"5. It provides quick rendering"},{"id":"6-its-search-engine-optimized","heading":"6. It's search engine optimized."},{"id":"7-it-includes-a-growth-toolbox","heading":"7. It includes a growth toolbox"},{"id":"8-supportive-community","heading":"8. Supportive community"},{"id":"9-it-improves-code-stability","heading":"9. It improves code stability"},{"id":"10-numerous-fortune-500-companies-use-it","heading":"10. Numerous Fortune 500 companies use it."}]},{"id":"conclusion","heading":"Conclusion"}]},"featureImageSharp":{"base":"lautaro-andreani-xkBaqlcqeb4-unsplash-2.jpg","publicURL":"/static/be66cede7624ebbc675202ad24146138/lautaro-andreani-xkBaqlcqeb4-unsplash-2.jpg","imageMeta":{"width":1920,"height":1280},"childImageSharp":{"fluid":{"base64":"data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAANABQDASIAAhEBAxEB/8QAGAAAAgMAAAAAAAAAAAAAAAAAAAIBAwX/xAAVAQEBAAAAAAAAAAAAAAAAAAABAP/aAAwDAQACEAMQAAAByq3hGFI//8QAGRAAAgMBAAAAAAAAAAAAAAAAAAECERJB/9oACAEBAAEFAqVDUThJ5f8A/8QAFREBAQAAAAAAAAAAAAAAAAAAABH/2gAIAQMBAT8BR//EABQRAQAAAAAAAAAAAAAAAAAAABD/2gAIAQIBAT8BP//EABYQAAMAAAAAAAAAAAAAAAAAAAARIP/aAAgBAQAGPwKUf//EABsQAQACAwEBAAAAAAAAAAAAAAEAESExURCh/9oACAEBAAE/IdwPnl7EdIFodhYy1xqf/9oADAMBAAIAAwAAABDD3//EABcRAAMBAAAAAAAAAAAAAAAAAAABETH/2gAIAQMBAT8QFC0//8QAFREBAQAAAAAAAAAAAAAAAAAAABH/2gAIAQIBAT8Qiv/EABoQAQEAAwEBAAAAAAAAAAAAAAERACExQZH/2gAIAQEAAT8QWKKl1xiFnN7fcASUmVSsfLnLCpsuJWr1F8mf/9k=","aspectRatio":1.5028901734104045,"src":"/static/be66cede7624ebbc675202ad24146138/d5c54/lautaro-andreani-xkBaqlcqeb4-unsplash-2.jpg","srcSet":"/static/be66cede7624ebbc675202ad24146138/65d8c/lautaro-andreani-xkBaqlcqeb4-unsplash-2.jpg 260w,\n/static/be66cede7624ebbc675202ad24146138/c5f21/lautaro-andreani-xkBaqlcqeb4-unsplash-2.jpg 520w,\n/static/be66cede7624ebbc675202ad24146138/d5c54/lautaro-andreani-xkBaqlcqeb4-unsplash-2.jpg 1040w,\n/static/be66cede7624ebbc675202ad24146138/81a53/lautaro-andreani-xkBaqlcqeb4-unsplash-2.jpg 1560w,\n/static/be66cede7624ebbc675202ad24146138/74fd5/lautaro-andreani-xkBaqlcqeb4-unsplash-2.jpg 1920w","srcWebp":"/static/be66cede7624ebbc675202ad24146138/e4875/lautaro-andreani-xkBaqlcqeb4-unsplash-2.webp","srcSetWebp":"/static/be66cede7624ebbc675202ad24146138/dc8f3/lautaro-andreani-xkBaqlcqeb4-unsplash-2.webp 260w,\n/static/be66cede7624ebbc675202ad24146138/2db4b/lautaro-andreani-xkBaqlcqeb4-unsplash-2.webp 520w,\n/static/be66cede7624ebbc675202ad24146138/e4875/lautaro-andreani-xkBaqlcqeb4-unsplash-2.webp 1040w,\n/static/be66cede7624ebbc675202ad24146138/f5845/lautaro-andreani-xkBaqlcqeb4-unsplash-2.webp 1560w,\n/static/be66cede7624ebbc675202ad24146138/6833b/lautaro-andreani-xkBaqlcqeb4-unsplash-2.webp 1920w","sizes":"(max-width: 1040px) 100vw, 1040px"}}}},"prev":{"id":"Ghost__Post__61ba1908c393bb64592d848e","title":"Create Laravel Blog with Strapi v4","slug":"create-laravel-blog-with-strapi-v4","featured":false,"feature_image":"https://backend.shahednasser.com/content/images/2021/12/lum3n--RBuQ2PK_L8-unsplash-2.jpg","excerpt":"In this tutorial, you'll learn how to create a blog in Laravel using Strapi. The blog will have posts, tags, and allow users to post comments.","custom_excerpt":"In this tutorial, you'll learn how to create a blog in Laravel using Strapi. The blog will have posts, tags, and allow users to post comments.","visibility":"public","created_at_pretty":"15 Dec 2021","published_at_pretty":"20 Dec 2021","updated_at_pretty":"27 Dec 2021","created_at":"2021-12-15T16:34:16.000+00:00","published_at":"2021-12-20T08:50:50.000+00:00","updated_at":"2021-12-27T11:09:20.000+00:00","meta_title":null,"meta_description":null,"og_description":null,"og_image":null,"og_title":null,"twitter_description":null,"twitter_image":null,"twitter_title":null,"authors":[{"slug":"shahed","url":"https://backend.shahednasser.com/author/shahed/","name":"Shahed Nasser","bio":null,"cover_image":null,"profile_image":"https://backend.shahednasser.com/content/images/2022/03/IMG_0591.jpg","location":null,"website":null,"twitter":null,"facebook":null,"meta_title":null,"meta_description":null,"coverImageSharp":null,"profileImageSharp":null}],"primary_author":{"slug":"shahed","url":"https://backend.shahednasser.com/author/shahed/","name":"Shahed Nasser","bio":null,"cover_image":null,"profile_image":"https://backend.shahednasser.com/content/images/2022/03/IMG_0591.jpg","location":null,"website":null,"twitter":null,"facebook":null,"meta_title":null,"meta_description":null,"coverImageSharp":null,"profileImageSharp":{"base":"IMG_0591.jpg","publicURL":"/static/ceb49c3c631485453e71e00d7f84b069/IMG_0591.jpg","imageMeta":{"width":1182,"height":1179},"childImageSharp":{"fluid":{"base64":"data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAUABQDASIAAhEBAxEB/8QAGAABAQEBAQAAAAAAAAAAAAAAAAMEAQL/xAAWAQEBAQAAAAAAAAAAAAAAAAADBAL/2gAMAwEAAhADEAAAAdXiFM6i0CohUWXoKn//xAAcEAACAgIDAAAAAAAAAAAAAAACAwESBBEhM0H/2gAIAQEAAQUCWySE3WEr7SzbXjAj4iKty+sOQ//EABYRAQEBAAAAAAAAAAAAAAAAAAERIP/aAAgBAwEBPwEhj//EABQRAQAAAAAAAAAAAAAAAAAAACD/2gAIAQIBAT8BH//EAB4QAAIBBAMBAAAAAAAAAAAAAAABIRESMUECECJx/9oACAEBAAY/ApVGWvOjzgtUwLlTZA0sdL4f/8QAHBAAAwACAwEAAAAAAAAAAAAAAAERITFBkbHB/9oACAEBAAE/IahkCy+N2GwZpjQiJHJCspUFY0QrSi+HqiW2rgf/2gAMAwEAAgADAAAAEPw3/wD/xAAYEQEBAAMAAAAAAAAAAAAAAAAAARExQf/aAAgBAwEBPxCtjDqP/8QAFxEBAQEBAAAAAAAAAAAAAAAAAQAxEf/aAAgBAgEBPxBFus6Tt//EAB8QAQEAAgIBBQAAAAAAAAAAAAERACExQWFRcYGR0f/aAAgBAQABPxAuaBPPzkO1wyX7F4wkwXanfZrFQgeqE9JgS14vVOvrERIJomVBKwt2jebAeP0yVa8h1n//2Q==","aspectRatio":1,"src":"/static/ceb49c3c631485453e71e00d7f84b069/31709/IMG_0591.jpg","srcSet":"/static/ceb49c3c631485453e71e00d7f84b069/f340b/IMG_0591.jpg 28w,\n/static/ceb49c3c631485453e71e00d7f84b069/22d64/IMG_0591.jpg 55w,\n/static/ceb49c3c631485453e71e00d7f84b069/31709/IMG_0591.jpg 110w,\n/static/ceb49c3c631485453e71e00d7f84b069/aa249/IMG_0591.jpg 165w,\n/static/ceb49c3c631485453e71e00d7f84b069/0dc33/IMG_0591.jpg 220w,\n/static/ceb49c3c631485453e71e00d7f84b069/d8257/IMG_0591.jpg 1182w","srcWebp":"/static/ceb49c3c631485453e71e00d7f84b069/8678c/IMG_0591.webp","srcSetWebp":"/static/ceb49c3c631485453e71e00d7f84b069/59cda/IMG_0591.webp 28w,\n/static/ceb49c3c631485453e71e00d7f84b069/7da75/IMG_0591.webp 55w,\n/static/ceb49c3c631485453e71e00d7f84b069/8678c/IMG_0591.webp 110w,\n/static/ceb49c3c631485453e71e00d7f84b069/f282e/IMG_0591.webp 165w,\n/static/ceb49c3c631485453e71e00d7f84b069/a7b21/IMG_0591.webp 220w,\n/static/ceb49c3c631485453e71e00d7f84b069/63099/IMG_0591.webp 1182w","sizes":"(max-width: 110px) 100vw, 110px"}}}},"primary_tag":{"slug":"strapi","url":"https://backend.shahednasser.com/tag/strapi/","name":"Strapi","visibility":"public","feature_image":null,"description":null,"meta_title":null,"meta_description":null,"featureImageSharp":null},"tags":[{"slug":"strapi","url":"https://backend.shahednasser.com/tag/strapi/","name":"Strapi","visibility":"public","feature_image":null,"description":null,"meta_title":null,"meta_description":null,"featureImageSharp":null},{"slug":"laravel","url":"https://backend.shahednasser.com/tag/laravel/","name":"Laravel","visibility":"public","feature_image":null,"description":null,"meta_title":null,"meta_description":null,"featureImageSharp":null},{"slug":"js","url":"https://backend.shahednasser.com/tag/js/","name":"Javascript","visibility":"public","feature_image":"https://backend.shahednasser.com/content/images/2022/01/photo-1592609931095-54a2168ae893-2.jpeg","description":"Learn more about Javascript through tutorials, articles, and tips.","meta_title":null,"meta_description":"Learn more about Javascript through tutorials, articles and tips.","featureImageSharp":null},{"slug":"php","url":"https://backend.shahednasser.com/tag/php/","name":"PHP","visibility":"public","feature_image":null,"description":null,"meta_title":null,"meta_description":null,"featureImageSharp":null}],"plaintext":"Laravel [https://laravel.com] is one of the most popular web frameworks that\nallow you to create secure and scalable websites. With Laravel, you can create\nalmost any website, ranging from simple portfolio websites to full-fledged\ne-commerce solutions.\n\nStrapi [https://strapi.io] is a headless CMS framework that has been gaining a\nlot of attention. With Strapi, you can easily add CMS functionalities to your\nwebsite, regardless of the technology being used. Strapi also removes the\nhassles of creating a database and models tailored to your CMS needs as it is\ncustomizable to the point that you can create your own models and entities, also\ncalled content types.\n\nIn this tutorial, you'll learn how to create a blog in Laravel using Strapi. The\nblog will have posts, tags, and allow users to post comments. You can find the\ncode for this tutorial in this GitHub repository\n[https://github.com/shahednasser/strapi-laravel-blog].\n\nPrerequisites \nBefore you start, you need the following tools on your machine:\n\n 1. Node.js [https://nodejs.org/en/]. Only versions 12 and 14 are supported by\n    Strapi, and 14 is recommended.\n 2. PHP [https://www.php.net] >= v7.3\n 3. Composer [https://getcomposer.org/download/]\n\nPlease note that this tutorial will be using Strapi v4 and Laravel v8.9\n\nSetup Strapi\nStart by setting up Strapi. In your terminal, run the following command:\n\nnpx create-strapi-app@latest strapi --quickstart\n\nOnce the command is done, the server will start at localhost:1337\n[http://localhost:1337] and a new page will open in a browser. It will be a\nregistration form, where you need to create an admin account on Strapi.\n\nOnce you're done, you'll be redirected to the main dashboard.\n\nCreate Content-Types\nNext, you'll create the content types necessary for the blog. You'll create\ncontent types for the posts and tags that the posts will use. You'll also create\na comment content type which will be filled from our blog on Laravel.\n\nStart by clicking on Create your first Content type in the dashboard. This will\nopen a new page to create content types. Under the Content Types Builder\nsidebar, click on Create new collection type under  Collection type. This will\nopen a pop-up where you can enter basic and advanced settings.\n\nYou'll first create the tags content type. Enter in the Display Name field in\nthe popup Tag. This will automatically generate the singular and plural forms of\nthe content type.\n\nOnce you're done, click Continue. You can now choose the fields in this content\ntype. tags will only have a name field other than their id. So, click on the \nText field type. Then, enter in the Name field name.\n\nIn the Advanced Settings tab, check the Required checkbox to ensure that all\ntags have a name. \n\nSince that's the only field you'll add for the Tag content type, click on Finish\n. Then, when the pop-up closes, click on the Save button at the top right. This\nwill restart the server. Every time you create a content type, the server is\nrestarted.\n\nNext, you'll create the Post content type. Again, click on Create new collection\ntype. In the pop up that opens, enter for Display Name Post, then click on \nContinue.\n\nFor posts, there will be fields for title, content, image, date posted, and tags\nthat the post falls under.\n\nFor the title field, choose the Text field and make it required as we did\nearlier. Once done, click on Add another field.\n\nFor the content field, choose the Rich text field, and make it required.\n\nFor the image field, choose the Media field, and choose for Type \"Single media\".\nIn the Advanced Settings tab, change the allowed file types under \"Select\nallowed types of media\" to only Images. Make the field required as well.\n\nFor the date_posted field, choose the Date field, and choose for Type\n\"datetime\". Mark this field required as well.\n\nFinally, for the tags field, choose the Relation field, then for the relation\ntype choose \"Post belongs to many Tags\".\n\nOnce you're done, click on Finish, then click on Save at the top right. This\nwill save the new content type and restart the server.\n\nFinally, you need to create the Comment content type. Just like you did with the\nprevious content types, create a new content type with the name Comment.\n\nThe Comment content type will have 3 fields. The first is an Email field with\nthe name field. Make sure to set it as required.\n\nThe second field is a Rich text field with the name content. This is where the\nuser's comment will go.\n\nThe third field is a Relation field between Comment and Post. The relation\nshould be \"Post has many Comments\".\n\nNote that when you create this field a new field will be added automatically in\nPost called comments.\n\nOnce you're done, click on Finish, then click on Save at the top right. This\nwill save the new content type and restart the server.\n\nOur content types are ready!\n\nAdd Content\nThe next step would be to add content. Click on Content Manager in the sidebar.\n Start by adding a few tags by clicking on Tag in the Content Manager sidebar,\nthen click on Add new entry at the top right. \n\nWhen you create content, make sure you click Publish after saving the content.\n\nNext, add posts the same way. You can use Lorem Ipsum Generator\n[https://loremipsum.io/generator/?n=5&t=p] if you want to create mock content.\n\nChange Permissions\nThe last step left is to make posts and tags public so that you can consume them\nin Laravel.\n\nFirst, you'll create an API token to use for your requests. In the sidebar,\nclick Settings, then API Token. Click on Add Entry at the top right.\n\nIn this form, enter the name of the token. This is just useful to remember what\nyour API tokens are for. You can also enter a description.\n\nIn the Token type field, choose Full Access.\n\nOnce you're done, click on Save at the top right. This will create a new API\ntoken and the API token will be shown to you only once when you create it. So,\ncopy the API token and store it somewhere as you'll use it later.\n\nNext, you'll modify the permissions for authenticated users to be able to query\ncontent types and add new entries.\n\nOn the sidebar, click Settings, then Roles in the Settings sidebar.\n\nYou'll see two roles: Authenticated and Public. Click on the pencil icon on the\nAuthenticated row.\n\nScroll down and you'll see that for each content type you can select what this\nrole can access. Check Select All for Post, Tag, and Comment, then click Save.\n\nSetup Laravel\nNow that Strapi is ready, you'll get started with Laravel.\n\nRun the following command to create a new Laravel project:\n\ncomposer create-project laravel/laravel blog\n\nOnce this command is done, change to the directory created:\n\ncd blog\n\nYou can then start the server with the following command:\n\nphp artisan serve\n\nThis will start the server at localhost:8000.\n\nAdd Environment Variables\nBefore you can make requests to Strapi, you need to add 2 environment variables.\nAdd the following environment variables to .env:\n\nSTRAPI_URL=http://localhost:1337\nSTRAPI_API_TOKEN=\n\nThe first is the URL to Strapi. You can change it if it's not the same local\nURL. The second is the API token you created earlier. Paste it here after the = \nsign.\n\nAdd Home Page\nOn the home page, you'll query all posts in Strapi and display them.\n\nRun the following command in your terminal to create a new controller:\n\nphp artisan make:controller BlogController\n\nThen, open app/Http/Controllers/BlogController.php and the following method in\nthe class:\n\npublic function home () {\n    //retrieve the posts from Strapi\n    $response = Http::withToken(env('STRAPI_API_TOKEN'))->get(env('STRAPI_URL') . '/api/posts?populate=image,tags');\n    $posts = [];\n\n    if ($response->failed()) {\n        if (isset($data['error'])) {\n        \tLog::error('Server error: ' . $data['error']['message']);\n        } else {\n        \tLog::error('Request Failed');\n        }\n    } else {\n        //get posts from response\n        $posts = $response->json('data');\n    }\n\n    return view('home', ['posts' => $posts]);\n}\n\nFirst, you query Strapi using Laravel's HTTP Client\n[https://laravel.com/docs/8.x/http-client]. You use withToken to pass it the API\ntoken from .env using the env helper function. Then, you send a get request to\nthe endpointlocalhost:1337/api/posts?populate=image,tags.\n\nNotice that localhost:1337 also is retrieved from .env. As for the endpoint\npath, Strapi has a conventional path for all its content types. When querying a\ncollection, the pattern for the endpoint is /api/{collection_name}.\n\nWhen you use Strapi's API, you can pass it a lot of useful parameters\n[https://docs.strapi.io/developer-docs/latest/developer-resources/database-apis-reference/rest-api.html#api-parameters] \nthat allow you to filter, sort, paginate the data, and more. Here, you use the \npopulate parameter which allows you to retrieve a content type with its\nrelations. You use it to retrieve the post with its image and tags.\n\nAfter sending the request, you can check if the request failed using \n$response->failed(). If the request failed, you log the error. If not, you set \n$posts to the data parameter in the response body. Note that you can use the \njson method to retrieve the parameters from a JSON response, optionally passing\nit a parameter name as the first element.\n\nNext, you need to add the home view. Create the file \nresources/views/home.blade.php with the following content:\n\n<!DOCTYPE html>\n<html lang=\"{{ str_replace('_', '-', app()->getLocale()) }}\">\n    <head>\n        <meta charset=\"utf-8\">\n        <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n\n        <title>Blog</title>\n\n        <link href=\"https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css\" rel=\"stylesheet\" integrity=\"sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3\" crossorigin=\"anonymous\">\n    </head>\n    <body class=\"antialiased bg-light\">\n        <div class=\"container mt-4 py-3 mx-auto bg-white rounded shadow-sm\">\n            <div class=\"row\">\n                @forelse ($posts as $post)\n                    <div class=\"col-2 col-md-4\">\n                        <div class=\"card\">\n                            <img src=\"{{ env('STRAPI_URL') . $post['attributes']['image']['data']['attributes']['formats']['medium']['url'] }}\" \n                                class=\"card-img-top\" alt=\"{{ $post['attributes']['image']['data']['attributes']['alternativeText'] }}\">\n                            <div class=\"card-body\">\n                                <h5 class=\"card-title\">{{ $post['attributes']['title'] }}</h5>\n                                <p class=\"card-text\">{{ substr($post['attributes']['content'], 0, 50) }}...</p>\n                                <a href=\"/post/{{ $post['id'] }}\" class=\"btn btn-primary\">Read More</a>\n                            </div>\n                            <div class=\"card-footer\">\n                                @if(count($post['attributes']['tags']['data']))\n                                    @foreach ($post['attributes']['tags']['data'] as $tag)\n                                        <span class=\"badge bg-success\">{{ $tag['attributes']['name'] }}</span>\n                                    @endforeach\n                                @endif\n                            </div>\n                        </div>\n                    </div>\n                @empty\n                    <div class=\"col\">\n                        <div class=\"card\">\n                            <div class=\"card-body\">\n                                This is some text within a card body.\n                            </div>\n                        </div>\n                    </div>\n                @endforelse\n            </div>\n        </div>\n    </body>\n</html>\n\n\nThis just displays the posts as cards\n[https://getbootstrap.com/docs/5.1/components/card/] using Bootstrap\n[https://getbootstrap.com]. Notice that the content type entries that Strapi\nreturn has the following format:\n\n{\n\t\"data\": {\n    \t\"id\",\n        \"attributes\": {\n        \t\"title\",\n            ...\n       \t}\n    }\n}\n\nSo, you'll find the content type's fields inside the attributes key of data.\n\nFinally, change the current route in routes/web.php to the following:\n\nRoute::get('/', [\\App\\Http\\Controllers\\BlogController::class, 'home']);\n\nLet's test it out. Make sure that both Laravel and Strapi's servers are running.\nThen, open localhost:8000. You'll see the posts you added as cards.\n\nAdd View Post Page\nNext, you'll add the page to view a post. This page receives the post ID as a\nparameter, then queries the post's data from Strapi.\n\nIn app/Http/Controllers/BlogController.php add a new method:\n\npublic function viewPost ($id) {\n        //retrieve the post from Strapi\n        $response = Http::withToken(env('STRAPI_API_TOKEN'))->get(env('STRAPI_URL') . '/api/posts/' . $id . '?populate=image,tags,comments');\n\n        if ($response->failed()) {\n            if (isset($data['error'])) {\n                Log::error('Server error: ' . $data['error']['message']);\n            } else {\n                Log::error('Request Failed');\n            }\n\n            return response()->redirectTo('/');\n        }\n\n        //get post from response\n        $post = $response->json('data');\n\n        return view('post', ['post' => $post]);\n }\n\nIn this method, you use the $id parameter, which is the post ID, to send a\nrequest to Strapi's single entry endpoint. The endpoint's pattern is \n/api/{collection_name}/{id}. Similar to the previous endpoint, you can also pass\nit parameters like populate.\n\nIf the request fails, you redirect the user to the home page and log the error.\nIf the request is done successfully, you retrieve the post from the response's\nbody and render the view post.\n\nNow, create resources/views/post.blade.php with the following content:\n\n<!DOCTYPE html>\n<html lang=\"{{ str_replace('_', '-', app()->getLocale()) }}\">\n    <head>\n        <meta charset=\"utf-8\">\n        <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n\n        <title>{{ $post['attributes']['title'] }} - Blog</title>\n\n        <link href=\"https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css\" rel=\"stylesheet\" integrity=\"sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3\" crossorigin=\"anonymous\">\n    </head>\n    <body class=\"antialiased bg-light\">\n        <div class=\"container mt-4 py-3 px-5 mx-auto bg-white rounded shadow-sm\">\n            <h1>{{ $post['attributes']['title'] }}</h1>\n            <small class=\"text-muted d-block\">{{ $post['attributes']['date_posted'] }}</small>\n            <img src=\"{{ env('STRAPI_URL') . $post['attributes']['image']['data']['attributes']['formats']['medium']['url'] }}\" \n                                class=\"img-fluid mx-auto d-block my-3\" alt=\"{{ $post['attributes']['image']['data']['attributes']['alternativeText'] }}\">\n            @if(count($post['attributes']['tags']['data']))\n              <div class=\"mb-3\">\n                @foreach ($post['attributes']['tags']['data'] as $tag)\n                  <span class=\"badge bg-success\">{{ $tag['attributes']['name'] }}</span>\n                @endforeach\n              </div>\n            @endif\n            <p class=\"content\">\n              {{ $post['attributes']['content'] }}\n            </p>\n\n            <hr />\n            <form action=\"/post/{{ $post['id'] }}\" method=\"POST\">\n              @csrf\n              <h2>Add Your Comment</h2>\n              <div class=\"mb-3\">\n                <label for=\"email\" class=\"form-label\">Email address</label>\n                <input type=\"email\" class=\"form-control\" id=\"email\" name=\"email\" required>\n              </div>\n              <div class=\"mb-3\">\n                <label for=\"content\" class=\"form-label\">Your Comment</label>\n                <textarea rows=\"5\" class=\"form-control\" id=\"content\" name=\"content\" required></textarea>\n              </div>\n              <button type=\"submit\" class=\"btn btn-primary\">Submit</button>\n            </form>\n        </div>\n    </body>\n</html>\n\n\nThis page just shows the post's details. Similar to how you extracted the post's\ndata, the post's field is nested inside the attributes field.\n\nThis page also has a comments form at the end of it. You'll implement its\nfunctionality after this.\n\nFinally, add the new route in routes/web.php:\n\nRoute::get('/post/{id}', [\\App\\Http\\Controllers\\BlogController::class, 'viewPost']);\n\n\nNow, open the home page again and click on Read More for one of the posts. A new\npage will open with the post's content.\n\nIf you scroll down, you'll see a form to add your comment.\n\nAdd Comment Functionality\nThe last thing you'll do in this tutorial is add the commenting functionality.\nThe form is already added, so you just need to add the POST route to add the\ncomment.\n\nAdd the following method in app/Http/Controllers/BlogController.php:\n\npublic function addComment (Request $request, $id) {\n        $data = [\n            \"data\" => [\n                'email' => $request->get('email'),\n                'content' => $request->get('content'),\n                'post' => $id\n            ]\n        ];\n\n        $response = Http::withToken(env('STRAPI_API_TOKEN'))->post(env('STRAPI_URL') . '/api/comments', $data);\n\n        if ($response->failed()) {\n            if (isset($data['error'])) {\n                Log::error('Server error: ' . $data['error']['message']);\n            } else {\n                Log::error('Request Failed');\n            }\n\n            return response()->redirectTo('/');\n        }\n\n        //successfully added\n        return response()->redirectTo('/post/' . $id);\n    }\n\nYou first format the data as Strapi likes it. When adding a content type entry,\nyou should nest the data inside a data parameter. Here, you add the email, \ncontent, and post fields. Notice that we are skipping validation here for\ntutorial simplicity.\n\nThen, you send a POST request to the endpoint /api/comments. Strapi's endpoint\npattern for adding a content type entry is /api/{collection_name}. You pass the\ndata as a second parameter to the post method.\n\nIf the request fails, the user is redirected to the home page. If it's\nsuccessful, the user is redirected back to the post's page.\n\nNext, add before the comment form in resources/views/post.blade.php the\nfollowing:\n\n<hr/>\n@if (count($post['attributes']['comments']['data']))\n    <div class=\"comments\">\n        <h2>Comments</h2>\n        @foreach ($post['attributes']['comments']['data'] as $comment)\n            <div class=\"card mb-3\">\n                <div class=\"card-body\">\n                \t{{ $comment['attributes']['content'] }}\n                </div>\n                <div class=\"card-footer\">\n                \tBy {{ $comment['attributes']['email'] }}\n                </div>\n            </div>\n        @endforeach\n    </div>\n@endif\n\nThis will show the comments if a post has any.\n\nFinally, add the new route in routes/web.php:\n\nRoute::post('/post/{id}', [\\App\\Http\\Controllers\\BlogController::class, 'addComment']);\n\nLet's test it out. Go to a post's page, then go to the comment form. Add your\ncomment and click Submit. You'll be redirected back to the post's page, but you\ncan see the comment below the post.\n\nConclusion\nIn this tutorial, you learned how to build a blog with Laravel and Strapi.\nStrapi is completely customizable, and that simplifies the process of adding\ncontent types, adding entries, and using its endpoints to query the content\ntypes or add new entries in them.","html":"<p><a href=\"https://laravel.com\">Laravel</a> is one of the most popular web frameworks that allow you to create secure and scalable websites. With Laravel, you can create almost any website, ranging from simple portfolio websites to full-fledged e-commerce solutions.</p><p><a href=\"https://strapi.io\">Strapi</a> is a headless CMS framework that has been gaining a lot of attention. With Strapi, you can easily add CMS functionalities to your website, regardless of the technology being used. Strapi also removes the hassles of creating a database and models tailored to your CMS needs as it is customizable to the point that you can create your own models and entities, also called content types.</p><p>In this tutorial, you'll learn how to create a blog in Laravel using Strapi. The blog will have posts, tags, and allow users to post comments. You can find the code for this tutorial in <a href=\"https://github.com/shahednasser/strapi-laravel-blog\">this GitHub repository</a>.</p><h2 id=\"prerequisites\">Prerequisites </h2><p>Before you start, you need the following tools on your machine:</p><ol><li><a href=\"https://nodejs.org/en/\">Node.js</a>. Only versions 12 and 14 are supported by Strapi, and 14 is recommended.</li><li><a href=\"https://www.php.net\">PHP</a> &gt;= v7.3</li><li><a href=\"https://getcomposer.org/download/\">Composer</a></li></ol><p>Please note that this tutorial will be using Strapi v4 and Laravel v8.9</p><h2 id=\"setup-strapi\">Setup Strapi</h2><p>Start by setting up Strapi. In your terminal, run the following command:</p><pre><code class=\"language-bash\">npx create-strapi-app@latest strapi --quickstart</code></pre><p>Once the command is done, the server will start at <a href=\"http://localhost:1337\">localhost:1337</a> and a new page will open in a browser. It will be a registration form, where you need to create an admin account on Strapi.</p><p>Once you're done, you'll be redirected to the main dashboard.</p><figure class=\"kg-card kg-image-card\"><img src=\"https://backend.shahednasser.com/content/images/2021/12/Screen-Shot-2021-12-15-at-7.40.33-PM.png\" class=\"kg-image\" alt loading=\"lazy\" width=\"2000\" height=\"1136\" srcset=\"https://backend.shahednasser.com/content/images/size/w600/2021/12/Screen-Shot-2021-12-15-at-7.40.33-PM.png 600w, https://backend.shahednasser.com/content/images/size/w1000/2021/12/Screen-Shot-2021-12-15-at-7.40.33-PM.png 1000w, https://backend.shahednasser.com/content/images/size/w1600/2021/12/Screen-Shot-2021-12-15-at-7.40.33-PM.png 1600w, https://backend.shahednasser.com/content/images/size/w2400/2021/12/Screen-Shot-2021-12-15-at-7.40.33-PM.png 2400w\" sizes=\"(min-width: 720px) 720px\"></figure><h3 id=\"create-content-types\">Create Content-Types</h3><p>Next, you'll create the content types necessary for the blog. You'll create content types for the posts and tags that the posts will use. You'll also create a comment content type which will be filled from our blog on Laravel.</p><p>Start by clicking on <em>Create your first Content type</em> in the dashboard. This will open a new page to create content types. Under the Content Types Builder sidebar, click on <em>Create new collection type </em>under  Collection type. This will open a pop-up where you can enter basic and advanced settings.</p><p>You'll first create the tags content type. Enter in the Display Name field in the popup <code>Tag</code>. This will automatically generate the singular and plural forms of the content type.</p><figure class=\"kg-card kg-image-card\"><img src=\"https://backend.shahednasser.com/content/images/2021/12/Screen-Shot-2021-12-15-at-7.45.23-PM.png\" class=\"kg-image\" alt loading=\"lazy\" width=\"1814\" height=\"1026\" srcset=\"https://backend.shahednasser.com/content/images/size/w600/2021/12/Screen-Shot-2021-12-15-at-7.45.23-PM.png 600w, https://backend.shahednasser.com/content/images/size/w1000/2021/12/Screen-Shot-2021-12-15-at-7.45.23-PM.png 1000w, https://backend.shahednasser.com/content/images/size/w1600/2021/12/Screen-Shot-2021-12-15-at-7.45.23-PM.png 1600w, https://backend.shahednasser.com/content/images/2021/12/Screen-Shot-2021-12-15-at-7.45.23-PM.png 1814w\" sizes=\"(min-width: 720px) 720px\"></figure><p>Once you're done, click <em>Continue</em>. You can now choose the fields in this content type. tags will only have a <code>name</code> field other than their id. So, click on the <em>Text </em>field type. Then, enter in the Name field <code>name</code>.</p><figure class=\"kg-card kg-image-card\"><img src=\"https://backend.shahednasser.com/content/images/2021/12/Screen-Shot-2021-12-15-at-7.47.28-PM.png\" class=\"kg-image\" alt loading=\"lazy\" width=\"1768\" height=\"1080\" srcset=\"https://backend.shahednasser.com/content/images/size/w600/2021/12/Screen-Shot-2021-12-15-at-7.47.28-PM.png 600w, https://backend.shahednasser.com/content/images/size/w1000/2021/12/Screen-Shot-2021-12-15-at-7.47.28-PM.png 1000w, https://backend.shahednasser.com/content/images/size/w1600/2021/12/Screen-Shot-2021-12-15-at-7.47.28-PM.png 1600w, https://backend.shahednasser.com/content/images/2021/12/Screen-Shot-2021-12-15-at-7.47.28-PM.png 1768w\" sizes=\"(min-width: 720px) 720px\"></figure><p>In the Advanced Settings tab, check the Required checkbox to ensure that all tags have a name. </p><p>Since that's the only field you'll add for the Tag content type, click on <em>Finish</em>. Then, when the pop-up closes, click on the<em> <em>Save</em></em> button at the top right. This will restart the server. Every time you create a content type, the server is restarted.</p><p>Next, you'll create the Post content type. Again, click on <em>Create new collection type. </em>In the pop up that opens, enter for Display Name <code>Post</code>, then click on <em>Continue.</em></p><p>For posts, there will be fields for title, content, image, date posted, and tags that the post falls under.</p><p>For the <code>title</code> field, choose the Text field and make it required as we did earlier. Once done, click on <em>Add another field.</em></p><p>For the <code>content</code> field, choose the Rich text field, and make it required.</p><p>For the <code>image</code> field, choose the Media field, and choose for Type \"Single media\". In the Advanced Settings tab, change the allowed file types under \"Select allowed types of media\" to only Images. Make the field required as well.</p><p>For the <code>date_posted</code> field, choose the Date field, and choose for Type \"datetime\". Mark this field required as well.</p><p>Finally, for the <code>tags</code> field, choose the Relation field, then for the relation type choose \"Post belongs to many Tags\".</p><figure class=\"kg-card kg-image-card\"><img src=\"https://backend.shahednasser.com/content/images/2021/12/Screen-Shot-2021-12-15-at-7.59.01-PM.png\" class=\"kg-image\" alt loading=\"lazy\" width=\"1830\" height=\"902\" srcset=\"https://backend.shahednasser.com/content/images/size/w600/2021/12/Screen-Shot-2021-12-15-at-7.59.01-PM.png 600w, https://backend.shahednasser.com/content/images/size/w1000/2021/12/Screen-Shot-2021-12-15-at-7.59.01-PM.png 1000w, https://backend.shahednasser.com/content/images/size/w1600/2021/12/Screen-Shot-2021-12-15-at-7.59.01-PM.png 1600w, https://backend.shahednasser.com/content/images/2021/12/Screen-Shot-2021-12-15-at-7.59.01-PM.png 1830w\" sizes=\"(min-width: 720px) 720px\"></figure><p>Once you're done, click on <em>Finish</em>, then click on<em> Save </em>at the top right. This will save the new content type and restart the server.</p><p>Finally, you need to create the Comment content type. Just like you did with the previous content types, create a new content type with the name <code>Comment</code>.</p><p>The <code>Comment</code> content type will have 3 fields. The first is an Email field with the name <code>field</code>. Make sure to set it as required.</p><p>The second field is a Rich text field with the name <code>content</code>. This is where the user's comment will go.</p><p>The third field is a Relation field between Comment and Post. The relation should be \"Post has many Comments\".</p><figure class=\"kg-card kg-image-card\"><img src=\"https://backend.shahednasser.com/content/images/2021/12/Screen-Shot-2021-12-16-at-8.13.33-PM.png\" class=\"kg-image\" alt loading=\"lazy\" width=\"1862\" height=\"972\" srcset=\"https://backend.shahednasser.com/content/images/size/w600/2021/12/Screen-Shot-2021-12-16-at-8.13.33-PM.png 600w, https://backend.shahednasser.com/content/images/size/w1000/2021/12/Screen-Shot-2021-12-16-at-8.13.33-PM.png 1000w, https://backend.shahednasser.com/content/images/size/w1600/2021/12/Screen-Shot-2021-12-16-at-8.13.33-PM.png 1600w, https://backend.shahednasser.com/content/images/2021/12/Screen-Shot-2021-12-16-at-8.13.33-PM.png 1862w\" sizes=\"(min-width: 720px) 720px\"></figure><p>Note that when you create this field a new field will be added automatically in Post called <code>comments</code>.</p><p>Once you're done, click on <em>Finish</em>, then click on<em> Save </em>at the top right. This will save the new content type and restart the server.</p><p>Our content types are ready!</p><h3 id=\"add-content\">Add Content</h3><p>The next step would be to add content. Click on <em>Content Manager</em> in the sidebar.  Start by adding a few tags by clicking on <em>Tag</em> in the Content Manager sidebar, then click on <em>Add new entry</em> at the top right. </p><p>When you create content, make sure you click <em>Publish</em> after saving the content.</p><p>Next, add posts the same way. You can use <a href=\"https://loremipsum.io/generator/?n=5&amp;t=p\">Lorem Ipsum Generator</a> if you want to create mock content.</p><h3 id=\"change-permissions\">Change Permissions</h3><p>The last step left is to make posts and tags public so that you can consume them in Laravel.</p><p>First, you'll create an API token to use for your requests. In the sidebar, click <em>Settings</em>, then <em>API Token</em>. Click on <em>Add Entry</em> at the top right.</p><p>In this form, enter the name of the token. This is just useful to remember what your API tokens are for. You can also enter a description.</p><p>In the Token type field, choose Full Access.</p><figure class=\"kg-card kg-image-card\"><img src=\"https://backend.shahednasser.com/content/images/2021/12/Screen-Shot-2021-12-16-at-7.27.11-PM.png\" class=\"kg-image\" alt loading=\"lazy\" width=\"1830\" height=\"678\" srcset=\"https://backend.shahednasser.com/content/images/size/w600/2021/12/Screen-Shot-2021-12-16-at-7.27.11-PM.png 600w, https://backend.shahednasser.com/content/images/size/w1000/2021/12/Screen-Shot-2021-12-16-at-7.27.11-PM.png 1000w, https://backend.shahednasser.com/content/images/size/w1600/2021/12/Screen-Shot-2021-12-16-at-7.27.11-PM.png 1600w, https://backend.shahednasser.com/content/images/2021/12/Screen-Shot-2021-12-16-at-7.27.11-PM.png 1830w\" sizes=\"(min-width: 720px) 720px\"></figure><p>Once you're done, click on <em>Save</em> at the top right. This will create a new API token and the API token will be shown to you only once when you create it. So, copy the API token and store it somewhere as you'll use it later.</p><p>Next, you'll modify the permissions for authenticated users to be able to query content types and add new entries.</p><p>On the sidebar, click <em>Settings</em>, then <em>Roles</em> in the <em>Settings</em> sidebar.</p><p>You'll see two roles: Authenticated and Public. Click on the pencil icon on the Authenticated row.</p><p>Scroll down and you'll see that for each content type you can select what this role can access. Check Select All for Post, Tag, and Comment, then click <em>Save</em>.</p><figure class=\"kg-card kg-image-card\"><img src=\"https://backend.shahednasser.com/content/images/2021/12/Screen-Shot-2021-12-16-at-7.37.10-PM.png\" class=\"kg-image\" alt loading=\"lazy\" width=\"964\" height=\"1070\" srcset=\"https://backend.shahednasser.com/content/images/size/w600/2021/12/Screen-Shot-2021-12-16-at-7.37.10-PM.png 600w, https://backend.shahednasser.com/content/images/2021/12/Screen-Shot-2021-12-16-at-7.37.10-PM.png 964w\" sizes=\"(min-width: 720px) 720px\"></figure><h2 id=\"setup-laravel\">Setup Laravel</h2><p>Now that Strapi is ready, you'll get started with Laravel.</p><p>Run the following command to create a new Laravel project:</p><pre><code class=\"language-bash\">composer create-project laravel/laravel blog</code></pre><p>Once this command is done, change to the directory created:</p><pre><code class=\"language-bash\">cd blog</code></pre><p>You can then start the server with the following command:</p><pre><code class=\"language-bash\">php artisan serve</code></pre><p>This will start the server at <code>localhost:8000</code>.</p><h3 id=\"add-environment-variables\">Add Environment Variables</h3><p>Before you can make requests to Strapi, you need to add 2 environment variables. Add the following environment variables to <code>.env</code>:</p><pre><code>STRAPI_URL=http://localhost:1337\nSTRAPI_API_TOKEN=</code></pre><p>The first is the URL to Strapi. You can change it if it's not the same local URL. The second is the API token you created earlier. Paste it here after the <code>=</code> sign.</p><h3 id=\"add-home-page\">Add Home Page</h3><p>On the home page, you'll query all posts in Strapi and display them.</p><p>Run the following command in your terminal to create a new controller:</p><pre><code class=\"language-bash\">php artisan make:controller BlogController</code></pre><p>Then, open <code>app/Http/Controllers/BlogController.php</code> and the following method in the class:</p><pre><code class=\"language-php\">public function home () {\n    //retrieve the posts from Strapi\n    $response = Http::withToken(env('STRAPI_API_TOKEN'))-&gt;get(env('STRAPI_URL') . '/api/posts?populate=image,tags');\n    $posts = [];\n\n    if ($response-&gt;failed()) {\n        if (isset($data['error'])) {\n        \tLog::error('Server error: ' . $data['error']['message']);\n        } else {\n        \tLog::error('Request Failed');\n        }\n    } else {\n        //get posts from response\n        $posts = $response-&gt;json('data');\n    }\n\n    return view('home', ['posts' =&gt; $posts]);\n}</code></pre><p>First, you query Strapi using Laravel's <a href=\"https://laravel.com/docs/8.x/http-client\">HTTP Client</a>. You use <code>withToken</code> to pass it the API token from <code>.env</code> using the <code>env</code> helper function. Then, you send a <code>get</code> request to the endpoint  <code>localhost:1337/api/posts?populate=image,tags</code>.</p><p>Notice that <code>localhost:1337</code> also is retrieved from <code>.env</code>. As for the endpoint path, Strapi has a conventional path for all its content types. When querying a collection, the pattern for the endpoint is <code>/api/{collection_name}</code>.</p><p>When you use Strapi's API, you can pass it a lot of <a href=\"https://docs.strapi.io/developer-docs/latest/developer-resources/database-apis-reference/rest-api.html#api-parameters\">useful parameters</a> that allow you to filter, sort, paginate the data, and more. Here, you use the <code>populate</code> parameter which allows you to retrieve a content type with its relations. You use it to retrieve the post with its image and tags.</p><p>After sending the request, you can check if the request failed using <code>$response-&gt;failed()</code>. If the request failed, you log the error. If not, you set <code>$posts</code> to the <code>data</code> parameter in the response body. Note that you can use the <code>json</code> method to retrieve the parameters from a JSON response, optionally passing it a parameter name as the first element.</p><p>Next, you need to add the <code>home</code> view. Create the file <code>resources/views/home.blade.php</code> with the following content:</p><pre><code class=\"language-php\">&lt;!DOCTYPE html&gt;\n&lt;html lang=\"{{ str_replace('_', '-', app()-&gt;getLocale()) }}\"&gt;\n    &lt;head&gt;\n        &lt;meta charset=\"utf-8\"&gt;\n        &lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"&gt;\n\n        &lt;title&gt;Blog&lt;/title&gt;\n\n        &lt;link href=\"https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css\" rel=\"stylesheet\" integrity=\"sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3\" crossorigin=\"anonymous\"&gt;\n    &lt;/head&gt;\n    &lt;body class=\"antialiased bg-light\"&gt;\n        &lt;div class=\"container mt-4 py-3 mx-auto bg-white rounded shadow-sm\"&gt;\n            &lt;div class=\"row\"&gt;\n                @forelse ($posts as $post)\n                    &lt;div class=\"col-2 col-md-4\"&gt;\n                        &lt;div class=\"card\"&gt;\n                            &lt;img src=\"{{ env('STRAPI_URL') . $post['attributes']['image']['data']['attributes']['formats']['medium']['url'] }}\" \n                                class=\"card-img-top\" alt=\"{{ $post['attributes']['image']['data']['attributes']['alternativeText'] }}\"&gt;\n                            &lt;div class=\"card-body\"&gt;\n                                &lt;h5 class=\"card-title\"&gt;{{ $post['attributes']['title'] }}&lt;/h5&gt;\n                                &lt;p class=\"card-text\"&gt;{{ substr($post['attributes']['content'], 0, 50) }}...&lt;/p&gt;\n                                &lt;a href=\"/post/{{ $post['id'] }}\" class=\"btn btn-primary\"&gt;Read More&lt;/a&gt;\n                            &lt;/div&gt;\n                            &lt;div class=\"card-footer\"&gt;\n                                @if(count($post['attributes']['tags']['data']))\n                                    @foreach ($post['attributes']['tags']['data'] as $tag)\n                                        &lt;span class=\"badge bg-success\"&gt;{{ $tag['attributes']['name'] }}&lt;/span&gt;\n                                    @endforeach\n                                @endif\n                            &lt;/div&gt;\n                        &lt;/div&gt;\n                    &lt;/div&gt;\n                @empty\n                    &lt;div class=\"col\"&gt;\n                        &lt;div class=\"card\"&gt;\n                            &lt;div class=\"card-body\"&gt;\n                                This is some text within a card body.\n                            &lt;/div&gt;\n                        &lt;/div&gt;\n                    &lt;/div&gt;\n                @endforelse\n            &lt;/div&gt;\n        &lt;/div&gt;\n    &lt;/body&gt;\n&lt;/html&gt;\n</code></pre><p>This just displays the posts as <a href=\"https://getbootstrap.com/docs/5.1/components/card/\">cards</a> using <a href=\"https://getbootstrap.com\">Bootstrap</a>. Notice that the content type entries that Strapi return has the following format:</p><pre><code class=\"language-json\">{\n\t\"data\": {\n    \t\"id\",\n        \"attributes\": {\n        \t\"title\",\n            ...\n       \t}\n    }\n}</code></pre><p>So, you'll find the content type's fields inside the <code>attributes</code> key of <code>data</code>.</p><p>Finally, change the current route in <code>routes/web.php</code> to the following:</p><pre><code class=\"language-php\">Route::get('/', [\\App\\Http\\Controllers\\BlogController::class, 'home']);</code></pre><p>Let's test it out. Make sure that both Laravel and Strapi's servers are running. Then, open <code>localhost:8000</code>. You'll see the posts you added as cards.</p><figure class=\"kg-card kg-image-card\"><img src=\"https://backend.shahednasser.com/content/images/2021/12/Screen-Shot-2021-12-16-at-7.48.15-PM.png\" class=\"kg-image\" alt loading=\"lazy\" width=\"2000\" height=\"1021\" srcset=\"https://backend.shahednasser.com/content/images/size/w600/2021/12/Screen-Shot-2021-12-16-at-7.48.15-PM.png 600w, https://backend.shahednasser.com/content/images/size/w1000/2021/12/Screen-Shot-2021-12-16-at-7.48.15-PM.png 1000w, https://backend.shahednasser.com/content/images/size/w1600/2021/12/Screen-Shot-2021-12-16-at-7.48.15-PM.png 1600w, https://backend.shahednasser.com/content/images/size/w2400/2021/12/Screen-Shot-2021-12-16-at-7.48.15-PM.png 2400w\" sizes=\"(min-width: 720px) 720px\"></figure><h3 id=\"add-view-post-page\">Add View Post Page</h3><p>Next, you'll add the page to view a post. This page receives the post ID as a parameter, then queries the post's data from Strapi.</p><p>In <code>app/Http/Controllers/BlogController.php</code> add a new method:</p><pre><code class=\"language-php\">public function viewPost ($id) {\n        //retrieve the post from Strapi\n        $response = Http::withToken(env('STRAPI_API_TOKEN'))-&gt;get(env('STRAPI_URL') . '/api/posts/' . $id . '?populate=image,tags,comments');\n\n        if ($response-&gt;failed()) {\n            if (isset($data['error'])) {\n                Log::error('Server error: ' . $data['error']['message']);\n            } else {\n                Log::error('Request Failed');\n            }\n\n            return response()-&gt;redirectTo('/');\n        }\n\n        //get post from response\n        $post = $response-&gt;json('data');\n\n        return view('post', ['post' =&gt; $post]);\n }</code></pre><p>In this method, you use the <code>$id</code> parameter, which is the post ID, to send a request to Strapi's single entry endpoint. The endpoint's pattern is <code>/api/{collection_name}/{id}</code>. Similar to the previous endpoint, you can also pass it parameters like <code>populate</code>.</p><p>If the request fails, you redirect the user to the home page and log the error. If the request is done successfully, you retrieve the post from the response's body and render the view <code>post</code>.</p><p>Now, create <code>resources/views/post.blade.php</code> with the following content:</p><pre><code class=\"language-php\">&lt;!DOCTYPE html&gt;\n&lt;html lang=\"{{ str_replace('_', '-', app()-&gt;getLocale()) }}\"&gt;\n    &lt;head&gt;\n        &lt;meta charset=\"utf-8\"&gt;\n        &lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"&gt;\n\n        &lt;title&gt;{{ $post['attributes']['title'] }} - Blog&lt;/title&gt;\n\n        &lt;link href=\"https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css\" rel=\"stylesheet\" integrity=\"sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3\" crossorigin=\"anonymous\"&gt;\n    &lt;/head&gt;\n    &lt;body class=\"antialiased bg-light\"&gt;\n        &lt;div class=\"container mt-4 py-3 px-5 mx-auto bg-white rounded shadow-sm\"&gt;\n            &lt;h1&gt;{{ $post['attributes']['title'] }}&lt;/h1&gt;\n            &lt;small class=\"text-muted d-block\"&gt;{{ $post['attributes']['date_posted'] }}&lt;/small&gt;\n            &lt;img src=\"{{ env('STRAPI_URL') . $post['attributes']['image']['data']['attributes']['formats']['medium']['url'] }}\" \n                                class=\"img-fluid mx-auto d-block my-3\" alt=\"{{ $post['attributes']['image']['data']['attributes']['alternativeText'] }}\"&gt;\n            @if(count($post['attributes']['tags']['data']))\n              &lt;div class=\"mb-3\"&gt;\n                @foreach ($post['attributes']['tags']['data'] as $tag)\n                  &lt;span class=\"badge bg-success\"&gt;{{ $tag['attributes']['name'] }}&lt;/span&gt;\n                @endforeach\n              &lt;/div&gt;\n            @endif\n            &lt;p class=\"content\"&gt;\n              {{ $post['attributes']['content'] }}\n            &lt;/p&gt;\n\n            &lt;hr /&gt;\n            &lt;form action=\"/post/{{ $post['id'] }}\" method=\"POST\"&gt;\n              @csrf\n              &lt;h2&gt;Add Your Comment&lt;/h2&gt;\n              &lt;div class=\"mb-3\"&gt;\n                &lt;label for=\"email\" class=\"form-label\"&gt;Email address&lt;/label&gt;\n                &lt;input type=\"email\" class=\"form-control\" id=\"email\" name=\"email\" required&gt;\n              &lt;/div&gt;\n              &lt;div class=\"mb-3\"&gt;\n                &lt;label for=\"content\" class=\"form-label\"&gt;Your Comment&lt;/label&gt;\n                &lt;textarea rows=\"5\" class=\"form-control\" id=\"content\" name=\"content\" required&gt;&lt;/textarea&gt;\n              &lt;/div&gt;\n              &lt;button type=\"submit\" class=\"btn btn-primary\"&gt;Submit&lt;/button&gt;\n            &lt;/form&gt;\n        &lt;/div&gt;\n    &lt;/body&gt;\n&lt;/html&gt;\n</code></pre><p>This page just shows the post's details. Similar to how you extracted the post's data, the post's field is nested inside the <code>attributes</code> field.</p><p>This page also has a comments form at the end of it. You'll implement its functionality after this.</p><p>Finally, add the new route in <code>routes/web.php</code>:</p><pre><code class=\"language-php\">Route::get('/post/{id}', [\\App\\Http\\Controllers\\BlogController::class, 'viewPost']);\n</code></pre><p>Now, open the home page again and click on <em>Read More</em> for one of the posts. A new page will open with the post's content.</p><figure class=\"kg-card kg-image-card\"><img src=\"https://backend.shahednasser.com/content/images/2021/12/Screen-Shot-2021-12-16-at-7.55.39-PM.png\" class=\"kg-image\" alt loading=\"lazy\" width=\"2000\" height=\"1161\" srcset=\"https://backend.shahednasser.com/content/images/size/w600/2021/12/Screen-Shot-2021-12-16-at-7.55.39-PM.png 600w, https://backend.shahednasser.com/content/images/size/w1000/2021/12/Screen-Shot-2021-12-16-at-7.55.39-PM.png 1000w, https://backend.shahednasser.com/content/images/size/w1600/2021/12/Screen-Shot-2021-12-16-at-7.55.39-PM.png 1600w, https://backend.shahednasser.com/content/images/size/w2400/2021/12/Screen-Shot-2021-12-16-at-7.55.39-PM.png 2400w\" sizes=\"(min-width: 720px) 720px\"></figure><p>If you scroll down, you'll see a form to add your comment.</p><figure class=\"kg-card kg-image-card\"><img src=\"https://backend.shahednasser.com/content/images/2021/12/Screen-Shot-2021-12-16-at-8.10.42-PM.png\" class=\"kg-image\" alt loading=\"lazy\" width=\"2000\" height=\"589\" srcset=\"https://backend.shahednasser.com/content/images/size/w600/2021/12/Screen-Shot-2021-12-16-at-8.10.42-PM.png 600w, https://backend.shahednasser.com/content/images/size/w1000/2021/12/Screen-Shot-2021-12-16-at-8.10.42-PM.png 1000w, https://backend.shahednasser.com/content/images/size/w1600/2021/12/Screen-Shot-2021-12-16-at-8.10.42-PM.png 1600w, https://backend.shahednasser.com/content/images/size/w2400/2021/12/Screen-Shot-2021-12-16-at-8.10.42-PM.png 2400w\" sizes=\"(min-width: 720px) 720px\"></figure><h3 id=\"add-comment-functionality\">Add Comment Functionality</h3><p>The last thing you'll do in this tutorial is add the commenting functionality. The form is already added, so you just need to add the <code>POST</code> route to add the comment.</p><p>Add the following method in <code>app/Http/Controllers/BlogController.php</code>:</p><pre><code class=\"language-php\">public function addComment (Request $request, $id) {\n        $data = [\n            \"data\" =&gt; [\n                'email' =&gt; $request-&gt;get('email'),\n                'content' =&gt; $request-&gt;get('content'),\n                'post' =&gt; $id\n            ]\n        ];\n\n        $response = Http::withToken(env('STRAPI_API_TOKEN'))-&gt;post(env('STRAPI_URL') . '/api/comments', $data);\n\n        if ($response-&gt;failed()) {\n            if (isset($data['error'])) {\n                Log::error('Server error: ' . $data['error']['message']);\n            } else {\n                Log::error('Request Failed');\n            }\n\n            return response()-&gt;redirectTo('/');\n        }\n\n        //successfully added\n        return response()-&gt;redirectTo('/post/' . $id);\n    }</code></pre><p>You first format the data as Strapi likes it. When adding a content type entry, you should nest the data inside a <code>data</code> parameter. Here, you add the <code>email</code>, <code>content</code>, and <code>post</code> fields. Notice that we are skipping validation here for tutorial simplicity.</p><p>Then, you send a <code>POST</code> request to the endpoint <code>/api/comments</code>. Strapi's endpoint pattern for adding a content type entry is <code>/api/{collection_name}</code>. You pass the data as a second parameter to the <code>post</code> method.</p><p>If the request fails, the user is redirected to the home page. If it's successful, the user is redirected back to the post's page.</p><p>Next, add before the comment form in <code>resources/views/post.blade.php</code> the following:</p><pre><code class=\"language-php\">&lt;hr/&gt;\n@if (count($post['attributes']['comments']['data']))\n    &lt;div class=\"comments\"&gt;\n        &lt;h2&gt;Comments&lt;/h2&gt;\n        @foreach ($post['attributes']['comments']['data'] as $comment)\n            &lt;div class=\"card mb-3\"&gt;\n                &lt;div class=\"card-body\"&gt;\n                \t{{ $comment['attributes']['content'] }}\n                &lt;/div&gt;\n                &lt;div class=\"card-footer\"&gt;\n                \tBy {{ $comment['attributes']['email'] }}\n                &lt;/div&gt;\n            &lt;/div&gt;\n        @endforeach\n    &lt;/div&gt;\n@endif</code></pre><p>This will show the comments if a post has any.</p><p>Finally, add the new route in <code>routes/web.php</code>:</p><pre><code class=\"language-php\">Route::post('/post/{id}', [\\App\\Http\\Controllers\\BlogController::class, 'addComment']);</code></pre><p>Let's test it out. Go to a post's page, then go to the comment form. Add your comment and click Submit. You'll be redirected back to the post's page, but you can see the comment below the post.</p><figure class=\"kg-card kg-image-card\"><img src=\"https://backend.shahednasser.com/content/images/2021/12/Screen-Shot-2021-12-16-at-8.10.34-PM.png\" class=\"kg-image\" alt loading=\"lazy\" width=\"2000\" height=\"443\" srcset=\"https://backend.shahednasser.com/content/images/size/w600/2021/12/Screen-Shot-2021-12-16-at-8.10.34-PM.png 600w, https://backend.shahednasser.com/content/images/size/w1000/2021/12/Screen-Shot-2021-12-16-at-8.10.34-PM.png 1000w, https://backend.shahednasser.com/content/images/size/w1600/2021/12/Screen-Shot-2021-12-16-at-8.10.34-PM.png 1600w, https://backend.shahednasser.com/content/images/size/w2400/2021/12/Screen-Shot-2021-12-16-at-8.10.34-PM.png 2400w\" sizes=\"(min-width: 720px) 720px\"></figure><h2 id=\"conclusion\">Conclusion</h2><p>In this tutorial, you learned how to build a blog with Laravel and Strapi. Strapi is completely customizable, and that simplifies the process of adding content types, adding entries, and using its endpoints to query the content types or add new entries in them.</p>","url":"https://backend.shahednasser.com/create-laravel-blog-with-strapi-v4/","canonical_url":null,"uuid":"179d8174-00da-470e-a7f8-3f64b74e5a4b","codeinjection_foot":null,"codeinjection_head":null,"codeinjection_styles":null,"comment_id":"61ba1908c393bb64592d848e","reading_time":12,"send_email_when_published":null,"email_subject":null,"childHtmlRehype":{"html":"<p><a href=\"https://laravel.com\">Laravel</a> is one of the most popular web frameworks that allow you to create secure and scalable websites. With Laravel, you can create almost any website, ranging from simple portfolio websites to full-fledged e-commerce solutions.</p><p><a href=\"https://strapi.io\">Strapi</a> is a headless CMS framework that has been gaining a lot of attention. With Strapi, you can easily add CMS functionalities to your website, regardless of the technology being used. Strapi also removes the hassles of creating a database and models tailored to your CMS needs as it is customizable to the point that you can create your own models and entities, also called content types.</p><p>In this tutorial, you'll learn how to create a blog in Laravel using Strapi. The blog will have posts, tags, and allow users to post comments. You can find the code for this tutorial in <a href=\"https://github.com/shahednasser/strapi-laravel-blog\">this GitHub repository</a>.</p><h2 id=\"prerequisites\">Prerequisites </h2><p>Before you start, you need the following tools on your machine:</p><ol><li><a href=\"https://nodejs.org/en/\">Node.js</a>. Only versions 12 and 14 are supported by Strapi, and 14 is recommended.</li><li><a href=\"https://www.php.net\">PHP</a> >= v7.3</li><li><a href=\"https://getcomposer.org/download/\">Composer</a></li></ol><p>Please note that this tutorial will be using Strapi v4 and Laravel v8.9</p><h2 id=\"setup-strapi\">Setup Strapi</h2><p>Start by setting up Strapi. In your terminal, run the following command:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\">npx create-strapi-app@latest strapi --quickstart</code></pre></div><p>Once the command is done, the server will start at <a href=\"http://localhost:1337\">localhost:1337</a> and a new page will open in a browser. It will be a registration form, where you need to create an admin account on Strapi.</p><p>Once you're done, you'll be redirected to the main dashboard.</p><figure class=\"kg-card kg-image-card\"><img src=\"https://backend.shahednasser.com/content/images/2021/12/Screen-Shot-2021-12-15-at-7.40.33-PM.png\" class=\"kg-image\" alt=\"\" loading=\"lazy\" width=\"2000\" height=\"1136\" srcset=\"https://backend.shahednasser.com/content/images/size/w600/2021/12/Screen-Shot-2021-12-15-at-7.40.33-PM.png 600w, https://backend.shahednasser.com/content/images/size/w1000/2021/12/Screen-Shot-2021-12-15-at-7.40.33-PM.png 1000w, https://backend.shahednasser.com/content/images/size/w1600/2021/12/Screen-Shot-2021-12-15-at-7.40.33-PM.png 1600w, https://backend.shahednasser.com/content/images/size/w2400/2021/12/Screen-Shot-2021-12-15-at-7.40.33-PM.png 2400w\" sizes=\"(min-width: 720px) 720px\"></figure><h3 id=\"create-content-types\">Create Content-Types</h3><p>Next, you'll create the content types necessary for the blog. You'll create content types for the posts and tags that the posts will use. You'll also create a comment content type which will be filled from our blog on Laravel.</p><p>Start by clicking on <em>Create your first Content type</em> in the dashboard. This will open a new page to create content types. Under the Content Types Builder sidebar, click on <em>Create new collection type </em>under  Collection type. This will open a pop-up where you can enter basic and advanced settings.</p><p>You'll first create the tags content type. Enter in the Display Name field in the popup <code class=\"language-text\">Tag</code>. This will automatically generate the singular and plural forms of the content type.</p><figure class=\"kg-card kg-image-card\"><img src=\"https://backend.shahednasser.com/content/images/2021/12/Screen-Shot-2021-12-15-at-7.45.23-PM.png\" class=\"kg-image\" alt=\"\" loading=\"lazy\" width=\"1814\" height=\"1026\" srcset=\"https://backend.shahednasser.com/content/images/size/w600/2021/12/Screen-Shot-2021-12-15-at-7.45.23-PM.png 600w, https://backend.shahednasser.com/content/images/size/w1000/2021/12/Screen-Shot-2021-12-15-at-7.45.23-PM.png 1000w, https://backend.shahednasser.com/content/images/size/w1600/2021/12/Screen-Shot-2021-12-15-at-7.45.23-PM.png 1600w, https://backend.shahednasser.com/content/images/2021/12/Screen-Shot-2021-12-15-at-7.45.23-PM.png 1814w\" sizes=\"(min-width: 720px) 720px\"></figure><p>Once you're done, click <em>Continue</em>. You can now choose the fields in this content type. tags will only have a <code class=\"language-text\">name</code> field other than their id. So, click on the <em>Text </em>field type. Then, enter in the Name field <code class=\"language-text\">name</code>.</p><figure class=\"kg-card kg-image-card\"><img src=\"https://backend.shahednasser.com/content/images/2021/12/Screen-Shot-2021-12-15-at-7.47.28-PM.png\" class=\"kg-image\" alt=\"\" loading=\"lazy\" width=\"1768\" height=\"1080\" srcset=\"https://backend.shahednasser.com/content/images/size/w600/2021/12/Screen-Shot-2021-12-15-at-7.47.28-PM.png 600w, https://backend.shahednasser.com/content/images/size/w1000/2021/12/Screen-Shot-2021-12-15-at-7.47.28-PM.png 1000w, https://backend.shahednasser.com/content/images/size/w1600/2021/12/Screen-Shot-2021-12-15-at-7.47.28-PM.png 1600w, https://backend.shahednasser.com/content/images/2021/12/Screen-Shot-2021-12-15-at-7.47.28-PM.png 1768w\" sizes=\"(min-width: 720px) 720px\"></figure><p>In the Advanced Settings tab, check the Required checkbox to ensure that all tags have a name. </p><p>Since that's the only field you'll add for the Tag content type, click on <em>Finish</em>. Then, when the pop-up closes, click on the<em> <em>Save</em></em> button at the top right. This will restart the server. Every time you create a content type, the server is restarted.</p><p>Next, you'll create the Post content type. Again, click on <em>Create new collection type. </em>In the pop up that opens, enter for Display Name <code class=\"language-text\">Post</code>, then click on <em>Continue.</em></p><p>For posts, there will be fields for title, content, image, date posted, and tags that the post falls under.</p><p>For the <code class=\"language-text\">title</code> field, choose the Text field and make it required as we did earlier. Once done, click on <em>Add another field.</em></p><p>For the <code class=\"language-text\">content</code> field, choose the Rich text field, and make it required.</p><p>For the <code class=\"language-text\">image</code> field, choose the Media field, and choose for Type \"Single media\". In the Advanced Settings tab, change the allowed file types under \"Select allowed types of media\" to only Images. Make the field required as well.</p><p>For the <code class=\"language-text\">date_posted</code> field, choose the Date field, and choose for Type \"datetime\". Mark this field required as well.</p><p>Finally, for the <code class=\"language-text\">tags</code> field, choose the Relation field, then for the relation type choose \"Post belongs to many Tags\".</p><figure class=\"kg-card kg-image-card\"><img src=\"https://backend.shahednasser.com/content/images/2021/12/Screen-Shot-2021-12-15-at-7.59.01-PM.png\" class=\"kg-image\" alt=\"\" loading=\"lazy\" width=\"1830\" height=\"902\" srcset=\"https://backend.shahednasser.com/content/images/size/w600/2021/12/Screen-Shot-2021-12-15-at-7.59.01-PM.png 600w, https://backend.shahednasser.com/content/images/size/w1000/2021/12/Screen-Shot-2021-12-15-at-7.59.01-PM.png 1000w, https://backend.shahednasser.com/content/images/size/w1600/2021/12/Screen-Shot-2021-12-15-at-7.59.01-PM.png 1600w, https://backend.shahednasser.com/content/images/2021/12/Screen-Shot-2021-12-15-at-7.59.01-PM.png 1830w\" sizes=\"(min-width: 720px) 720px\"></figure><p>Once you're done, click on <em>Finish</em>, then click on<em> Save </em>at the top right. This will save the new content type and restart the server.</p><p>Finally, you need to create the Comment content type. Just like you did with the previous content types, create a new content type with the name <code class=\"language-text\">Comment</code>.</p><p>The <code class=\"language-text\">Comment</code> content type will have 3 fields. The first is an Email field with the name <code class=\"language-text\">field</code>. Make sure to set it as required.</p><p>The second field is a Rich text field with the name <code class=\"language-text\">content</code>. This is where the user's comment will go.</p><p>The third field is a Relation field between Comment and Post. The relation should be \"Post has many Comments\".</p><figure class=\"kg-card kg-image-card\"><img src=\"https://backend.shahednasser.com/content/images/2021/12/Screen-Shot-2021-12-16-at-8.13.33-PM.png\" class=\"kg-image\" alt=\"\" loading=\"lazy\" width=\"1862\" height=\"972\" srcset=\"https://backend.shahednasser.com/content/images/size/w600/2021/12/Screen-Shot-2021-12-16-at-8.13.33-PM.png 600w, https://backend.shahednasser.com/content/images/size/w1000/2021/12/Screen-Shot-2021-12-16-at-8.13.33-PM.png 1000w, https://backend.shahednasser.com/content/images/size/w1600/2021/12/Screen-Shot-2021-12-16-at-8.13.33-PM.png 1600w, https://backend.shahednasser.com/content/images/2021/12/Screen-Shot-2021-12-16-at-8.13.33-PM.png 1862w\" sizes=\"(min-width: 720px) 720px\"></figure><p>Note that when you create this field a new field will be added automatically in Post called <code class=\"language-text\">comments</code>.</p><p>Once you're done, click on <em>Finish</em>, then click on<em> Save </em>at the top right. This will save the new content type and restart the server.</p><p>Our content types are ready!</p><h3 id=\"add-content\">Add Content</h3><p>The next step would be to add content. Click on <em>Content Manager</em> in the sidebar.  Start by adding a few tags by clicking on <em>Tag</em> in the Content Manager sidebar, then click on <em>Add new entry</em> at the top right. </p><p>When you create content, make sure you click <em>Publish</em> after saving the content.</p><p>Next, add posts the same way. You can use <a href=\"https://loremipsum.io/generator/?n=5&#x26;t=p\">Lorem Ipsum Generator</a> if you want to create mock content.</p><h3 id=\"change-permissions\">Change Permissions</h3><p>The last step left is to make posts and tags public so that you can consume them in Laravel.</p><p>First, you'll create an API token to use for your requests. In the sidebar, click <em>Settings</em>, then <em>API Token</em>. Click on <em>Add Entry</em> at the top right.</p><p>In this form, enter the name of the token. This is just useful to remember what your API tokens are for. You can also enter a description.</p><p>In the Token type field, choose Full Access.</p><figure class=\"kg-card kg-image-card\"><img src=\"https://backend.shahednasser.com/content/images/2021/12/Screen-Shot-2021-12-16-at-7.27.11-PM.png\" class=\"kg-image\" alt=\"\" loading=\"lazy\" width=\"1830\" height=\"678\" srcset=\"https://backend.shahednasser.com/content/images/size/w600/2021/12/Screen-Shot-2021-12-16-at-7.27.11-PM.png 600w, https://backend.shahednasser.com/content/images/size/w1000/2021/12/Screen-Shot-2021-12-16-at-7.27.11-PM.png 1000w, https://backend.shahednasser.com/content/images/size/w1600/2021/12/Screen-Shot-2021-12-16-at-7.27.11-PM.png 1600w, https://backend.shahednasser.com/content/images/2021/12/Screen-Shot-2021-12-16-at-7.27.11-PM.png 1830w\" sizes=\"(min-width: 720px) 720px\"></figure><p>Once you're done, click on <em>Save</em> at the top right. This will create a new API token and the API token will be shown to you only once when you create it. So, copy the API token and store it somewhere as you'll use it later.</p><p>Next, you'll modify the permissions for authenticated users to be able to query content types and add new entries.</p><p>On the sidebar, click <em>Settings</em>, then <em>Roles</em> in the <em>Settings</em> sidebar.</p><p>You'll see two roles: Authenticated and Public. Click on the pencil icon on the Authenticated row.</p><p>Scroll down and you'll see that for each content type you can select what this role can access. Check Select All for Post, Tag, and Comment, then click <em>Save</em>.</p><figure class=\"kg-card kg-image-card\"><img src=\"https://backend.shahednasser.com/content/images/2021/12/Screen-Shot-2021-12-16-at-7.37.10-PM.png\" class=\"kg-image\" alt=\"\" loading=\"lazy\" width=\"964\" height=\"1070\" srcset=\"https://backend.shahednasser.com/content/images/size/w600/2021/12/Screen-Shot-2021-12-16-at-7.37.10-PM.png 600w, https://backend.shahednasser.com/content/images/2021/12/Screen-Shot-2021-12-16-at-7.37.10-PM.png 964w\" sizes=\"(min-width: 720px) 720px\"></figure><h2 id=\"setup-laravel\">Setup Laravel</h2><p>Now that Strapi is ready, you'll get started with Laravel.</p><p>Run the following command to create a new Laravel project:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\"><span class=\"token function\">composer</span> create-project laravel/laravel blog</code></pre></div><p>Once this command is done, change to the directory created:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\"><span class=\"token builtin class-name\">cd</span> blog</code></pre></div><p>You can then start the server with the following command:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\">php artisan serve</code></pre></div><p>This will start the server at <code class=\"language-text\">localhost:8000</code>.</p><h3 id=\"add-environment-variables\">Add Environment Variables</h3><p>Before you can make requests to Strapi, you need to add 2 environment variables. Add the following environment variables to <code class=\"language-text\">.env</code>:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">STRAPI_URL=http://localhost:1337\nSTRAPI_API_TOKEN=</code></pre></div><p>The first is the URL to Strapi. You can change it if it's not the same local URL. The second is the API token you created earlier. Paste it here after the <code class=\"language-text\">=</code> sign.</p><h3 id=\"add-home-page\">Add Home Page</h3><p>On the home page, you'll query all posts in Strapi and display them.</p><p>Run the following command in your terminal to create a new controller:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\">php artisan make:controller BlogController</code></pre></div><p>Then, open <code class=\"language-text\">app/Http/Controllers/BlogController.php</code> and the following method in the class:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"php\"><pre class=\"language-php\"><code class=\"language-php\"><span class=\"token keyword\">public</span> <span class=\"token keyword\">function</span> <span class=\"token function-definition function\">home</span> <span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token comment\">//retrieve the posts from Strapi</span>\n    <span class=\"token variable\">$response</span> <span class=\"token operator\">=</span> <span class=\"token class-name static-context\">Http</span><span class=\"token operator\">::</span><span class=\"token function\">withToken</span><span class=\"token punctuation\">(</span><span class=\"token function\">env</span><span class=\"token punctuation\">(</span><span class=\"token string single-quoted-string\">'STRAPI_API_TOKEN'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token operator\">-></span><span class=\"token function\">get</span><span class=\"token punctuation\">(</span><span class=\"token function\">env</span><span class=\"token punctuation\">(</span><span class=\"token string single-quoted-string\">'STRAPI_URL'</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">.</span> <span class=\"token string single-quoted-string\">'/api/posts?populate=image,tags'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n    <span class=\"token variable\">$posts</span> <span class=\"token operator\">=</span> <span class=\"token punctuation\">[</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span>\n\n    <span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span><span class=\"token variable\">$response</span><span class=\"token operator\">-></span><span class=\"token function\">failed</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n        <span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span><span class=\"token keyword\">isset</span><span class=\"token punctuation\">(</span><span class=\"token variable\">$data</span><span class=\"token punctuation\">[</span><span class=\"token string single-quoted-string\">'error'</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n        \t<span class=\"token class-name static-context\">Log</span><span class=\"token operator\">::</span><span class=\"token function\">error</span><span class=\"token punctuation\">(</span><span class=\"token string single-quoted-string\">'Server error: '</span> <span class=\"token operator\">.</span> <span class=\"token variable\">$data</span><span class=\"token punctuation\">[</span><span class=\"token string single-quoted-string\">'error'</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">[</span><span class=\"token string single-quoted-string\">'message'</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n        <span class=\"token punctuation\">}</span> <span class=\"token keyword\">else</span> <span class=\"token punctuation\">{</span>\n        \t<span class=\"token class-name static-context\">Log</span><span class=\"token operator\">::</span><span class=\"token function\">error</span><span class=\"token punctuation\">(</span><span class=\"token string single-quoted-string\">'Request Failed'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n        <span class=\"token punctuation\">}</span>\n    <span class=\"token punctuation\">}</span> <span class=\"token keyword\">else</span> <span class=\"token punctuation\">{</span>\n        <span class=\"token comment\">//get posts from response</span>\n        <span class=\"token variable\">$posts</span> <span class=\"token operator\">=</span> <span class=\"token variable\">$response</span><span class=\"token operator\">-></span><span class=\"token function\">json</span><span class=\"token punctuation\">(</span><span class=\"token string single-quoted-string\">'data'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n    <span class=\"token punctuation\">}</span>\n\n    <span class=\"token keyword\">return</span> <span class=\"token function\">view</span><span class=\"token punctuation\">(</span><span class=\"token string single-quoted-string\">'home'</span><span class=\"token punctuation\">,</span> <span class=\"token punctuation\">[</span><span class=\"token string single-quoted-string\">'posts'</span> <span class=\"token operator\">=></span> <span class=\"token variable\">$posts</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span></code></pre></div><p>First, you query Strapi using Laravel's <a href=\"https://laravel.com/docs/8.x/http-client\">HTTP Client</a>. You use <code class=\"language-text\">withToken</code> to pass it the API token from <code class=\"language-text\">.env</code> using the <code class=\"language-text\">env</code> helper function. Then, you send a <code class=\"language-text\">get</code> request to the endpoint  <code class=\"language-text\">localhost:1337/api/posts?populate=image,tags</code>.</p><p>Notice that <code class=\"language-text\">localhost:1337</code> also is retrieved from <code class=\"language-text\">.env</code>. As for the endpoint path, Strapi has a conventional path for all its content types. When querying a collection, the pattern for the endpoint is <code class=\"language-text\">/api/{collection_name}</code>.</p><p>When you use Strapi's API, you can pass it a lot of <a href=\"https://docs.strapi.io/developer-docs/latest/developer-resources/database-apis-reference/rest-api.html#api-parameters\">useful parameters</a> that allow you to filter, sort, paginate the data, and more. Here, you use the <code class=\"language-text\">populate</code> parameter which allows you to retrieve a content type with its relations. You use it to retrieve the post with its image and tags.</p><p>After sending the request, you can check if the request failed using <code class=\"language-text\">$response->failed()</code>. If the request failed, you log the error. If not, you set <code class=\"language-text\">$posts</code> to the <code class=\"language-text\">data</code> parameter in the response body. Note that you can use the <code class=\"language-text\">json</code> method to retrieve the parameters from a JSON response, optionally passing it a parameter name as the first element.</p><p>Next, you need to add the <code class=\"language-text\">home</code> view. Create the file <code class=\"language-text\">resources/views/home.blade.php</code> with the following content:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"php\"><pre class=\"language-php\"><code class=\"language-php\"><span class=\"token operator\">&#x3C;</span><span class=\"token operator\">!</span><span class=\"token constant\">DOCTYPE</span> html<span class=\"token operator\">></span>\n<span class=\"token operator\">&#x3C;</span>html lang<span class=\"token operator\">=</span><span class=\"token string double-quoted-string\">\"{{ str_replace('_', '-', app()->getLocale()) }}\"</span><span class=\"token operator\">></span>\n    <span class=\"token operator\">&#x3C;</span>head<span class=\"token operator\">></span>\n        <span class=\"token operator\">&#x3C;</span>meta charset<span class=\"token operator\">=</span><span class=\"token string double-quoted-string\">\"utf-8\"</span><span class=\"token operator\">></span>\n        <span class=\"token operator\">&#x3C;</span>meta name<span class=\"token operator\">=</span><span class=\"token string double-quoted-string\">\"viewport\"</span> content<span class=\"token operator\">=</span><span class=\"token string double-quoted-string\">\"width=device-width, initial-scale=1\"</span><span class=\"token operator\">></span>\n\n        <span class=\"token operator\">&#x3C;</span>title<span class=\"token operator\">></span>Blog<span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>title<span class=\"token operator\">></span>\n\n        <span class=\"token operator\">&#x3C;</span>link href<span class=\"token operator\">=</span><span class=\"token string double-quoted-string\">\"https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css\"</span> rel<span class=\"token operator\">=</span><span class=\"token string double-quoted-string\">\"stylesheet\"</span> integrity<span class=\"token operator\">=</span><span class=\"token string double-quoted-string\">\"sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3\"</span> crossorigin<span class=\"token operator\">=</span><span class=\"token string double-quoted-string\">\"anonymous\"</span><span class=\"token operator\">></span>\n    <span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>head<span class=\"token operator\">></span>\n    <span class=\"token operator\">&#x3C;</span>body <span class=\"token keyword\">class</span><span class=\"token operator\">=</span><span class=\"token string double-quoted-string\">\"antialiased bg-light\"</span><span class=\"token operator\">></span>\n        <span class=\"token operator\">&#x3C;</span>div <span class=\"token keyword\">class</span><span class=\"token operator\">=</span><span class=\"token string double-quoted-string\">\"container mt-4 py-3 mx-auto bg-white rounded shadow-sm\"</span><span class=\"token operator\">></span>\n            <span class=\"token operator\">&#x3C;</span>div <span class=\"token keyword\">class</span><span class=\"token operator\">=</span><span class=\"token string double-quoted-string\">\"row\"</span><span class=\"token operator\">></span>\n                @<span class=\"token function\">forelse</span> <span class=\"token punctuation\">(</span><span class=\"token variable\">$posts</span> <span class=\"token keyword\">as</span> <span class=\"token variable\">$post</span><span class=\"token punctuation\">)</span>\n                    <span class=\"token operator\">&#x3C;</span>div <span class=\"token keyword\">class</span><span class=\"token operator\">=</span><span class=\"token string double-quoted-string\">\"col-2 col-md-4\"</span><span class=\"token operator\">></span>\n                        <span class=\"token operator\">&#x3C;</span>div <span class=\"token keyword\">class</span><span class=\"token operator\">=</span><span class=\"token string double-quoted-string\">\"card\"</span><span class=\"token operator\">></span>\n                            <span class=\"token operator\">&#x3C;</span>img src<span class=\"token operator\">=</span><span class=\"token string double-quoted-string\">\"{{ env('STRAPI_URL') . <span class=\"token interpolation\"><span class=\"token variable\">$post</span><span class=\"token punctuation\">[</span><span class=\"token string single-quoted-string\">'attributes'</span><span class=\"token punctuation\">]</span></span>['image']['data']['attributes']['formats']['medium']['url'] }}\"</span> \n                                <span class=\"token keyword\">class</span><span class=\"token operator\">=</span><span class=\"token string double-quoted-string\">\"card-img-top\"</span> alt<span class=\"token operator\">=</span><span class=\"token string double-quoted-string\">\"{{ <span class=\"token interpolation\"><span class=\"token variable\">$post</span><span class=\"token punctuation\">[</span><span class=\"token string single-quoted-string\">'attributes'</span><span class=\"token punctuation\">]</span></span>['image']['data']['attributes']['alternativeText'] }}\"</span><span class=\"token operator\">></span>\n                            <span class=\"token operator\">&#x3C;</span>div <span class=\"token keyword\">class</span><span class=\"token operator\">=</span><span class=\"token string double-quoted-string\">\"card-body\"</span><span class=\"token operator\">></span>\n                                <span class=\"token operator\">&#x3C;</span>h5 <span class=\"token keyword\">class</span><span class=\"token operator\">=</span><span class=\"token string double-quoted-string\">\"card-title\"</span><span class=\"token operator\">></span><span class=\"token punctuation\">{</span><span class=\"token punctuation\">{</span> <span class=\"token variable\">$post</span><span class=\"token punctuation\">[</span><span class=\"token string single-quoted-string\">'attributes'</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">[</span><span class=\"token string single-quoted-string\">'title'</span><span class=\"token punctuation\">]</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">}</span><span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>h5<span class=\"token operator\">></span>\n                                <span class=\"token operator\">&#x3C;</span>p <span class=\"token keyword\">class</span><span class=\"token operator\">=</span><span class=\"token string double-quoted-string\">\"card-text\"</span><span class=\"token operator\">></span><span class=\"token punctuation\">{</span><span class=\"token punctuation\">{</span> <span class=\"token function\">substr</span><span class=\"token punctuation\">(</span><span class=\"token variable\">$post</span><span class=\"token punctuation\">[</span><span class=\"token string single-quoted-string\">'attributes'</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">[</span><span class=\"token string single-quoted-string\">'content'</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0</span><span class=\"token punctuation\">,</span> <span class=\"token number\">50</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">}</span><span class=\"token operator\">...</span><span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>p<span class=\"token operator\">></span>\n                                <span class=\"token operator\">&#x3C;</span>a href<span class=\"token operator\">=</span><span class=\"token string double-quoted-string\">\"/post/{{ <span class=\"token interpolation\"><span class=\"token variable\">$post</span><span class=\"token punctuation\">[</span><span class=\"token string single-quoted-string\">'id'</span><span class=\"token punctuation\">]</span></span> }}\"</span> <span class=\"token keyword\">class</span><span class=\"token operator\">=</span><span class=\"token string double-quoted-string\">\"btn btn-primary\"</span><span class=\"token operator\">></span>Read More<span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>a<span class=\"token operator\">></span>\n                            <span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>div<span class=\"token operator\">></span>\n                            <span class=\"token operator\">&#x3C;</span>div <span class=\"token keyword\">class</span><span class=\"token operator\">=</span><span class=\"token string double-quoted-string\">\"card-footer\"</span><span class=\"token operator\">></span>\n                                @<span class=\"token keyword\">if</span><span class=\"token punctuation\">(</span><span class=\"token function\">count</span><span class=\"token punctuation\">(</span><span class=\"token variable\">$post</span><span class=\"token punctuation\">[</span><span class=\"token string single-quoted-string\">'attributes'</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">[</span><span class=\"token string single-quoted-string\">'tags'</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">[</span><span class=\"token string single-quoted-string\">'data'</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span>\n                                    @<span class=\"token keyword\">foreach</span> <span class=\"token punctuation\">(</span><span class=\"token variable\">$post</span><span class=\"token punctuation\">[</span><span class=\"token string single-quoted-string\">'attributes'</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">[</span><span class=\"token string single-quoted-string\">'tags'</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">[</span><span class=\"token string single-quoted-string\">'data'</span><span class=\"token punctuation\">]</span> <span class=\"token keyword\">as</span> <span class=\"token variable\">$tag</span><span class=\"token punctuation\">)</span>\n                                        <span class=\"token operator\">&#x3C;</span>span <span class=\"token keyword\">class</span><span class=\"token operator\">=</span><span class=\"token string double-quoted-string\">\"badge bg-success\"</span><span class=\"token operator\">></span><span class=\"token punctuation\">{</span><span class=\"token punctuation\">{</span> <span class=\"token variable\">$tag</span><span class=\"token punctuation\">[</span><span class=\"token string single-quoted-string\">'attributes'</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">[</span><span class=\"token string single-quoted-string\">'name'</span><span class=\"token punctuation\">]</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">}</span><span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>span<span class=\"token operator\">></span>\n                                    @<span class=\"token keyword\">endforeach</span>\n                                @<span class=\"token keyword\">endif</span>\n                            <span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>div<span class=\"token operator\">></span>\n                        <span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>div<span class=\"token operator\">></span>\n                    <span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>div<span class=\"token operator\">></span>\n                @<span class=\"token keyword\">empty</span>\n                    <span class=\"token operator\">&#x3C;</span>div <span class=\"token keyword\">class</span><span class=\"token operator\">=</span><span class=\"token string double-quoted-string\">\"col\"</span><span class=\"token operator\">></span>\n                        <span class=\"token operator\">&#x3C;</span>div <span class=\"token keyword\">class</span><span class=\"token operator\">=</span><span class=\"token string double-quoted-string\">\"card\"</span><span class=\"token operator\">></span>\n                            <span class=\"token operator\">&#x3C;</span>div <span class=\"token keyword\">class</span><span class=\"token operator\">=</span><span class=\"token string double-quoted-string\">\"card-body\"</span><span class=\"token operator\">></span>\n                                This is some text within a card body<span class=\"token operator\">.</span>\n                            <span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>div<span class=\"token operator\">></span>\n                        <span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>div<span class=\"token operator\">></span>\n                    <span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>div<span class=\"token operator\">></span>\n                @endforelse\n            <span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>div<span class=\"token operator\">></span>\n        <span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>div<span class=\"token operator\">></span>\n    <span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>body<span class=\"token operator\">></span>\n<span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>html<span class=\"token operator\">></span>\n</code></pre></div><p>This just displays the posts as <a href=\"https://getbootstrap.com/docs/5.1/components/card/\">cards</a> using <a href=\"https://getbootstrap.com\">Bootstrap</a>. Notice that the content type entries that Strapi return has the following format:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"json\"><pre class=\"language-json\"><code class=\"language-json\"><span class=\"token punctuation\">{</span>\n\t<span class=\"token property\">\"data\"</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span>\n    \t<span class=\"token string\">\"id\"</span><span class=\"token punctuation\">,</span>\n        <span class=\"token property\">\"attributes\"</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span>\n        \t<span class=\"token string\">\"title\"</span><span class=\"token punctuation\">,</span>\n            ...\n       \t<span class=\"token punctuation\">}</span>\n    <span class=\"token punctuation\">}</span>\n<span class=\"token punctuation\">}</span></code></pre></div><p>So, you'll find the content type's fields inside the <code class=\"language-text\">attributes</code> key of <code class=\"language-text\">data</code>.</p><p>Finally, change the current route in <code class=\"language-text\">routes/web.php</code> to the following:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"php\"><pre class=\"language-php\"><code class=\"language-php\"><span class=\"token class-name static-context\">Route</span><span class=\"token operator\">::</span><span class=\"token function\">get</span><span class=\"token punctuation\">(</span><span class=\"token string single-quoted-string\">'/'</span><span class=\"token punctuation\">,</span> <span class=\"token punctuation\">[</span><span class=\"token class-name class-name-fully-qualified static-context\"><span class=\"token punctuation\">\\</span>App<span class=\"token punctuation\">\\</span>Http<span class=\"token punctuation\">\\</span>Controllers<span class=\"token punctuation\">\\</span>BlogController</span><span class=\"token operator\">::</span><span class=\"token keyword\">class</span><span class=\"token punctuation\">,</span> <span class=\"token string single-quoted-string\">'home'</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code></pre></div><p>Let's test it out. Make sure that both Laravel and Strapi's servers are running. Then, open <code class=\"language-text\">localhost:8000</code>. You'll see the posts you added as cards.</p><figure class=\"kg-card kg-image-card\"><img src=\"https://backend.shahednasser.com/content/images/2021/12/Screen-Shot-2021-12-16-at-7.48.15-PM.png\" class=\"kg-image\" alt=\"\" loading=\"lazy\" width=\"2000\" height=\"1021\" srcset=\"https://backend.shahednasser.com/content/images/size/w600/2021/12/Screen-Shot-2021-12-16-at-7.48.15-PM.png 600w, https://backend.shahednasser.com/content/images/size/w1000/2021/12/Screen-Shot-2021-12-16-at-7.48.15-PM.png 1000w, https://backend.shahednasser.com/content/images/size/w1600/2021/12/Screen-Shot-2021-12-16-at-7.48.15-PM.png 1600w, https://backend.shahednasser.com/content/images/size/w2400/2021/12/Screen-Shot-2021-12-16-at-7.48.15-PM.png 2400w\" sizes=\"(min-width: 720px) 720px\"></figure><h3 id=\"add-view-post-page\">Add View Post Page</h3><p>Next, you'll add the page to view a post. This page receives the post ID as a parameter, then queries the post's data from Strapi.</p><p>In <code class=\"language-text\">app/Http/Controllers/BlogController.php</code> add a new method:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"php\"><pre class=\"language-php\"><code class=\"language-php\"><span class=\"token keyword\">public</span> <span class=\"token keyword\">function</span> <span class=\"token function-definition function\">viewPost</span> <span class=\"token punctuation\">(</span><span class=\"token variable\">$id</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n        <span class=\"token comment\">//retrieve the post from Strapi</span>\n        <span class=\"token variable\">$response</span> <span class=\"token operator\">=</span> <span class=\"token class-name static-context\">Http</span><span class=\"token operator\">::</span><span class=\"token function\">withToken</span><span class=\"token punctuation\">(</span><span class=\"token function\">env</span><span class=\"token punctuation\">(</span><span class=\"token string single-quoted-string\">'STRAPI_API_TOKEN'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token operator\">-></span><span class=\"token function\">get</span><span class=\"token punctuation\">(</span><span class=\"token function\">env</span><span class=\"token punctuation\">(</span><span class=\"token string single-quoted-string\">'STRAPI_URL'</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">.</span> <span class=\"token string single-quoted-string\">'/api/posts/'</span> <span class=\"token operator\">.</span> <span class=\"token variable\">$id</span> <span class=\"token operator\">.</span> <span class=\"token string single-quoted-string\">'?populate=image,tags,comments'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n\n        <span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span><span class=\"token variable\">$response</span><span class=\"token operator\">-></span><span class=\"token function\">failed</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n            <span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span><span class=\"token keyword\">isset</span><span class=\"token punctuation\">(</span><span class=\"token variable\">$data</span><span class=\"token punctuation\">[</span><span class=\"token string single-quoted-string\">'error'</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n                <span class=\"token class-name static-context\">Log</span><span class=\"token operator\">::</span><span class=\"token function\">error</span><span class=\"token punctuation\">(</span><span class=\"token string single-quoted-string\">'Server error: '</span> <span class=\"token operator\">.</span> <span class=\"token variable\">$data</span><span class=\"token punctuation\">[</span><span class=\"token string single-quoted-string\">'error'</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">[</span><span class=\"token string single-quoted-string\">'message'</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n            <span class=\"token punctuation\">}</span> <span class=\"token keyword\">else</span> <span class=\"token punctuation\">{</span>\n                <span class=\"token class-name static-context\">Log</span><span class=\"token operator\">::</span><span class=\"token function\">error</span><span class=\"token punctuation\">(</span><span class=\"token string single-quoted-string\">'Request Failed'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n            <span class=\"token punctuation\">}</span>\n\n            <span class=\"token keyword\">return</span> <span class=\"token function\">response</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token operator\">-></span><span class=\"token function\">redirectTo</span><span class=\"token punctuation\">(</span><span class=\"token string single-quoted-string\">'/'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n        <span class=\"token punctuation\">}</span>\n\n        <span class=\"token comment\">//get post from response</span>\n        <span class=\"token variable\">$post</span> <span class=\"token operator\">=</span> <span class=\"token variable\">$response</span><span class=\"token operator\">-></span><span class=\"token function\">json</span><span class=\"token punctuation\">(</span><span class=\"token string single-quoted-string\">'data'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n\n        <span class=\"token keyword\">return</span> <span class=\"token function\">view</span><span class=\"token punctuation\">(</span><span class=\"token string single-quoted-string\">'post'</span><span class=\"token punctuation\">,</span> <span class=\"token punctuation\">[</span><span class=\"token string single-quoted-string\">'post'</span> <span class=\"token operator\">=></span> <span class=\"token variable\">$post</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n <span class=\"token punctuation\">}</span></code></pre></div><p>In this method, you use the <code class=\"language-text\">$id</code> parameter, which is the post ID, to send a request to Strapi's single entry endpoint. The endpoint's pattern is <code class=\"language-text\">/api/{collection_name}/{id}</code>. Similar to the previous endpoint, you can also pass it parameters like <code class=\"language-text\">populate</code>.</p><p>If the request fails, you redirect the user to the home page and log the error. If the request is done successfully, you retrieve the post from the response's body and render the view <code class=\"language-text\">post</code>.</p><p>Now, create <code class=\"language-text\">resources/views/post.blade.php</code> with the following content:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"php\"><pre class=\"language-php\"><code class=\"language-php\"><span class=\"token operator\">&#x3C;</span><span class=\"token operator\">!</span><span class=\"token constant\">DOCTYPE</span> html<span class=\"token operator\">></span>\n<span class=\"token operator\">&#x3C;</span>html lang<span class=\"token operator\">=</span><span class=\"token string double-quoted-string\">\"{{ str_replace('_', '-', app()->getLocale()) }}\"</span><span class=\"token operator\">></span>\n    <span class=\"token operator\">&#x3C;</span>head<span class=\"token operator\">></span>\n        <span class=\"token operator\">&#x3C;</span>meta charset<span class=\"token operator\">=</span><span class=\"token string double-quoted-string\">\"utf-8\"</span><span class=\"token operator\">></span>\n        <span class=\"token operator\">&#x3C;</span>meta name<span class=\"token operator\">=</span><span class=\"token string double-quoted-string\">\"viewport\"</span> content<span class=\"token operator\">=</span><span class=\"token string double-quoted-string\">\"width=device-width, initial-scale=1\"</span><span class=\"token operator\">></span>\n\n        <span class=\"token operator\">&#x3C;</span>title<span class=\"token operator\">></span><span class=\"token punctuation\">{</span><span class=\"token punctuation\">{</span> <span class=\"token variable\">$post</span><span class=\"token punctuation\">[</span><span class=\"token string single-quoted-string\">'attributes'</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">[</span><span class=\"token string single-quoted-string\">'title'</span><span class=\"token punctuation\">]</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">}</span> <span class=\"token operator\">-</span> Blog<span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>title<span class=\"token operator\">></span>\n\n        <span class=\"token operator\">&#x3C;</span>link href<span class=\"token operator\">=</span><span class=\"token string double-quoted-string\">\"https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css\"</span> rel<span class=\"token operator\">=</span><span class=\"token string double-quoted-string\">\"stylesheet\"</span> integrity<span class=\"token operator\">=</span><span class=\"token string double-quoted-string\">\"sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3\"</span> crossorigin<span class=\"token operator\">=</span><span class=\"token string double-quoted-string\">\"anonymous\"</span><span class=\"token operator\">></span>\n    <span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>head<span class=\"token operator\">></span>\n    <span class=\"token operator\">&#x3C;</span>body <span class=\"token keyword\">class</span><span class=\"token operator\">=</span><span class=\"token string double-quoted-string\">\"antialiased bg-light\"</span><span class=\"token operator\">></span>\n        <span class=\"token operator\">&#x3C;</span>div <span class=\"token keyword\">class</span><span class=\"token operator\">=</span><span class=\"token string double-quoted-string\">\"container mt-4 py-3 px-5 mx-auto bg-white rounded shadow-sm\"</span><span class=\"token operator\">></span>\n            <span class=\"token operator\">&#x3C;</span>h1<span class=\"token operator\">></span><span class=\"token punctuation\">{</span><span class=\"token punctuation\">{</span> <span class=\"token variable\">$post</span><span class=\"token punctuation\">[</span><span class=\"token string single-quoted-string\">'attributes'</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">[</span><span class=\"token string single-quoted-string\">'title'</span><span class=\"token punctuation\">]</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">}</span><span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>h1<span class=\"token operator\">></span>\n            <span class=\"token operator\">&#x3C;</span>small <span class=\"token keyword\">class</span><span class=\"token operator\">=</span><span class=\"token string double-quoted-string\">\"text-muted d-block\"</span><span class=\"token operator\">></span><span class=\"token punctuation\">{</span><span class=\"token punctuation\">{</span> <span class=\"token variable\">$post</span><span class=\"token punctuation\">[</span><span class=\"token string single-quoted-string\">'attributes'</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">[</span><span class=\"token string single-quoted-string\">'date_posted'</span><span class=\"token punctuation\">]</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">}</span><span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>small<span class=\"token operator\">></span>\n            <span class=\"token operator\">&#x3C;</span>img src<span class=\"token operator\">=</span><span class=\"token string double-quoted-string\">\"{{ env('STRAPI_URL') . <span class=\"token interpolation\"><span class=\"token variable\">$post</span><span class=\"token punctuation\">[</span><span class=\"token string single-quoted-string\">'attributes'</span><span class=\"token punctuation\">]</span></span>['image']['data']['attributes']['formats']['medium']['url'] }}\"</span> \n                                <span class=\"token keyword\">class</span><span class=\"token operator\">=</span><span class=\"token string double-quoted-string\">\"img-fluid mx-auto d-block my-3\"</span> alt<span class=\"token operator\">=</span><span class=\"token string double-quoted-string\">\"{{ <span class=\"token interpolation\"><span class=\"token variable\">$post</span><span class=\"token punctuation\">[</span><span class=\"token string single-quoted-string\">'attributes'</span><span class=\"token punctuation\">]</span></span>['image']['data']['attributes']['alternativeText'] }}\"</span><span class=\"token operator\">></span>\n            @<span class=\"token keyword\">if</span><span class=\"token punctuation\">(</span><span class=\"token function\">count</span><span class=\"token punctuation\">(</span><span class=\"token variable\">$post</span><span class=\"token punctuation\">[</span><span class=\"token string single-quoted-string\">'attributes'</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">[</span><span class=\"token string single-quoted-string\">'tags'</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">[</span><span class=\"token string single-quoted-string\">'data'</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span>\n              <span class=\"token operator\">&#x3C;</span>div <span class=\"token keyword\">class</span><span class=\"token operator\">=</span><span class=\"token string double-quoted-string\">\"mb-3\"</span><span class=\"token operator\">></span>\n                @<span class=\"token keyword\">foreach</span> <span class=\"token punctuation\">(</span><span class=\"token variable\">$post</span><span class=\"token punctuation\">[</span><span class=\"token string single-quoted-string\">'attributes'</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">[</span><span class=\"token string single-quoted-string\">'tags'</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">[</span><span class=\"token string single-quoted-string\">'data'</span><span class=\"token punctuation\">]</span> <span class=\"token keyword\">as</span> <span class=\"token variable\">$tag</span><span class=\"token punctuation\">)</span>\n                  <span class=\"token operator\">&#x3C;</span>span <span class=\"token keyword\">class</span><span class=\"token operator\">=</span><span class=\"token string double-quoted-string\">\"badge bg-success\"</span><span class=\"token operator\">></span><span class=\"token punctuation\">{</span><span class=\"token punctuation\">{</span> <span class=\"token variable\">$tag</span><span class=\"token punctuation\">[</span><span class=\"token string single-quoted-string\">'attributes'</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">[</span><span class=\"token string single-quoted-string\">'name'</span><span class=\"token punctuation\">]</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">}</span><span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>span<span class=\"token operator\">></span>\n                @<span class=\"token keyword\">endforeach</span>\n              <span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>div<span class=\"token operator\">></span>\n            @<span class=\"token keyword\">endif</span>\n            <span class=\"token operator\">&#x3C;</span>p <span class=\"token keyword\">class</span><span class=\"token operator\">=</span><span class=\"token string double-quoted-string\">\"content\"</span><span class=\"token operator\">></span>\n              <span class=\"token punctuation\">{</span><span class=\"token punctuation\">{</span> <span class=\"token variable\">$post</span><span class=\"token punctuation\">[</span><span class=\"token string single-quoted-string\">'attributes'</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">[</span><span class=\"token string single-quoted-string\">'content'</span><span class=\"token punctuation\">]</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">}</span>\n            <span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>p<span class=\"token operator\">></span>\n\n            <span class=\"token operator\">&#x3C;</span>hr <span class=\"token operator\">/</span><span class=\"token operator\">></span>\n            <span class=\"token operator\">&#x3C;</span>form action<span class=\"token operator\">=</span><span class=\"token string double-quoted-string\">\"/post/{{ <span class=\"token interpolation\"><span class=\"token variable\">$post</span><span class=\"token punctuation\">[</span><span class=\"token string single-quoted-string\">'id'</span><span class=\"token punctuation\">]</span></span> }}\"</span> method<span class=\"token operator\">=</span><span class=\"token string double-quoted-string\">\"POST\"</span><span class=\"token operator\">></span>\n              @csrf\n              <span class=\"token operator\">&#x3C;</span>h2<span class=\"token operator\">></span>Add Your Comment<span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>h2<span class=\"token operator\">></span>\n              <span class=\"token operator\">&#x3C;</span>div <span class=\"token keyword\">class</span><span class=\"token operator\">=</span><span class=\"token string double-quoted-string\">\"mb-3\"</span><span class=\"token operator\">></span>\n                <span class=\"token operator\">&#x3C;</span>label <span class=\"token keyword\">for</span><span class=\"token operator\">=</span><span class=\"token string double-quoted-string\">\"email\"</span> <span class=\"token keyword\">class</span><span class=\"token operator\">=</span><span class=\"token string double-quoted-string\">\"form-label\"</span><span class=\"token operator\">></span>Email address<span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>label<span class=\"token operator\">></span>\n                <span class=\"token operator\">&#x3C;</span>input type<span class=\"token operator\">=</span><span class=\"token string double-quoted-string\">\"email\"</span> <span class=\"token keyword\">class</span><span class=\"token operator\">=</span><span class=\"token string double-quoted-string\">\"form-control\"</span> id<span class=\"token operator\">=</span><span class=\"token string double-quoted-string\">\"email\"</span> name<span class=\"token operator\">=</span><span class=\"token string double-quoted-string\">\"email\"</span> required<span class=\"token operator\">></span>\n              <span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>div<span class=\"token operator\">></span>\n              <span class=\"token operator\">&#x3C;</span>div <span class=\"token keyword\">class</span><span class=\"token operator\">=</span><span class=\"token string double-quoted-string\">\"mb-3\"</span><span class=\"token operator\">></span>\n                <span class=\"token operator\">&#x3C;</span>label <span class=\"token keyword\">for</span><span class=\"token operator\">=</span><span class=\"token string double-quoted-string\">\"content\"</span> <span class=\"token keyword\">class</span><span class=\"token operator\">=</span><span class=\"token string double-quoted-string\">\"form-label\"</span><span class=\"token operator\">></span>Your Comment<span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>label<span class=\"token operator\">></span>\n                <span class=\"token operator\">&#x3C;</span>textarea rows<span class=\"token operator\">=</span><span class=\"token string double-quoted-string\">\"5\"</span> <span class=\"token keyword\">class</span><span class=\"token operator\">=</span><span class=\"token string double-quoted-string\">\"form-control\"</span> id<span class=\"token operator\">=</span><span class=\"token string double-quoted-string\">\"content\"</span> name<span class=\"token operator\">=</span><span class=\"token string double-quoted-string\">\"content\"</span> required<span class=\"token operator\">></span><span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>textarea<span class=\"token operator\">></span>\n              <span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>div<span class=\"token operator\">></span>\n              <span class=\"token operator\">&#x3C;</span>button type<span class=\"token operator\">=</span><span class=\"token string double-quoted-string\">\"submit\"</span> <span class=\"token keyword\">class</span><span class=\"token operator\">=</span><span class=\"token string double-quoted-string\">\"btn btn-primary\"</span><span class=\"token operator\">></span>Submit<span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>button<span class=\"token operator\">></span>\n            <span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>form<span class=\"token operator\">></span>\n        <span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>div<span class=\"token operator\">></span>\n    <span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>body<span class=\"token operator\">></span>\n<span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>html<span class=\"token operator\">></span>\n</code></pre></div><p>This page just shows the post's details. Similar to how you extracted the post's data, the post's field is nested inside the <code class=\"language-text\">attributes</code> field.</p><p>This page also has a comments form at the end of it. You'll implement its functionality after this.</p><p>Finally, add the new route in <code class=\"language-text\">routes/web.php</code>:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"php\"><pre class=\"language-php\"><code class=\"language-php\"><span class=\"token class-name static-context\">Route</span><span class=\"token operator\">::</span><span class=\"token function\">get</span><span class=\"token punctuation\">(</span><span class=\"token string single-quoted-string\">'/post/{id}'</span><span class=\"token punctuation\">,</span> <span class=\"token punctuation\">[</span><span class=\"token class-name class-name-fully-qualified static-context\"><span class=\"token punctuation\">\\</span>App<span class=\"token punctuation\">\\</span>Http<span class=\"token punctuation\">\\</span>Controllers<span class=\"token punctuation\">\\</span>BlogController</span><span class=\"token operator\">::</span><span class=\"token keyword\">class</span><span class=\"token punctuation\">,</span> <span class=\"token string single-quoted-string\">'viewPost'</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n</code></pre></div><p>Now, open the home page again and click on <em>Read More</em> for one of the posts. A new page will open with the post's content.</p><figure class=\"kg-card kg-image-card\"><img src=\"https://backend.shahednasser.com/content/images/2021/12/Screen-Shot-2021-12-16-at-7.55.39-PM.png\" class=\"kg-image\" alt=\"\" loading=\"lazy\" width=\"2000\" height=\"1161\" srcset=\"https://backend.shahednasser.com/content/images/size/w600/2021/12/Screen-Shot-2021-12-16-at-7.55.39-PM.png 600w, https://backend.shahednasser.com/content/images/size/w1000/2021/12/Screen-Shot-2021-12-16-at-7.55.39-PM.png 1000w, https://backend.shahednasser.com/content/images/size/w1600/2021/12/Screen-Shot-2021-12-16-at-7.55.39-PM.png 1600w, https://backend.shahednasser.com/content/images/size/w2400/2021/12/Screen-Shot-2021-12-16-at-7.55.39-PM.png 2400w\" sizes=\"(min-width: 720px) 720px\"></figure><p>If you scroll down, you'll see a form to add your comment.</p><figure class=\"kg-card kg-image-card\"><img src=\"https://backend.shahednasser.com/content/images/2021/12/Screen-Shot-2021-12-16-at-8.10.42-PM.png\" class=\"kg-image\" alt=\"\" loading=\"lazy\" width=\"2000\" height=\"589\" srcset=\"https://backend.shahednasser.com/content/images/size/w600/2021/12/Screen-Shot-2021-12-16-at-8.10.42-PM.png 600w, https://backend.shahednasser.com/content/images/size/w1000/2021/12/Screen-Shot-2021-12-16-at-8.10.42-PM.png 1000w, https://backend.shahednasser.com/content/images/size/w1600/2021/12/Screen-Shot-2021-12-16-at-8.10.42-PM.png 1600w, https://backend.shahednasser.com/content/images/size/w2400/2021/12/Screen-Shot-2021-12-16-at-8.10.42-PM.png 2400w\" sizes=\"(min-width: 720px) 720px\"></figure><h3 id=\"add-comment-functionality\">Add Comment Functionality</h3><p>The last thing you'll do in this tutorial is add the commenting functionality. The form is already added, so you just need to add the <code class=\"language-text\">POST</code> route to add the comment.</p><p>Add the following method in <code class=\"language-text\">app/Http/Controllers/BlogController.php</code>:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"php\"><pre class=\"language-php\"><code class=\"language-php\"><span class=\"token keyword\">public</span> <span class=\"token keyword\">function</span> <span class=\"token function-definition function\">addComment</span> <span class=\"token punctuation\">(</span><span class=\"token class-name type-declaration\">Request</span> <span class=\"token variable\">$request</span><span class=\"token punctuation\">,</span> <span class=\"token variable\">$id</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n        <span class=\"token variable\">$data</span> <span class=\"token operator\">=</span> <span class=\"token punctuation\">[</span>\n            <span class=\"token string double-quoted-string\">\"data\"</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">[</span>\n                <span class=\"token string single-quoted-string\">'email'</span> <span class=\"token operator\">=></span> <span class=\"token variable\">$request</span><span class=\"token operator\">-></span><span class=\"token function\">get</span><span class=\"token punctuation\">(</span><span class=\"token string single-quoted-string\">'email'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">,</span>\n                <span class=\"token string single-quoted-string\">'content'</span> <span class=\"token operator\">=></span> <span class=\"token variable\">$request</span><span class=\"token operator\">-></span><span class=\"token function\">get</span><span class=\"token punctuation\">(</span><span class=\"token string single-quoted-string\">'content'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">,</span>\n                <span class=\"token string single-quoted-string\">'post'</span> <span class=\"token operator\">=></span> <span class=\"token variable\">$id</span>\n            <span class=\"token punctuation\">]</span>\n        <span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span>\n\n        <span class=\"token variable\">$response</span> <span class=\"token operator\">=</span> <span class=\"token class-name static-context\">Http</span><span class=\"token operator\">::</span><span class=\"token function\">withToken</span><span class=\"token punctuation\">(</span><span class=\"token function\">env</span><span class=\"token punctuation\">(</span><span class=\"token string single-quoted-string\">'STRAPI_API_TOKEN'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token operator\">-></span><span class=\"token function\">post</span><span class=\"token punctuation\">(</span><span class=\"token function\">env</span><span class=\"token punctuation\">(</span><span class=\"token string single-quoted-string\">'STRAPI_URL'</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">.</span> <span class=\"token string single-quoted-string\">'/api/comments'</span><span class=\"token punctuation\">,</span> <span class=\"token variable\">$data</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n\n        <span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span><span class=\"token variable\">$response</span><span class=\"token operator\">-></span><span class=\"token function\">failed</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n            <span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span><span class=\"token keyword\">isset</span><span class=\"token punctuation\">(</span><span class=\"token variable\">$data</span><span class=\"token punctuation\">[</span><span class=\"token string single-quoted-string\">'error'</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n                <span class=\"token class-name static-context\">Log</span><span class=\"token operator\">::</span><span class=\"token function\">error</span><span class=\"token punctuation\">(</span><span class=\"token string single-quoted-string\">'Server error: '</span> <span class=\"token operator\">.</span> <span class=\"token variable\">$data</span><span class=\"token punctuation\">[</span><span class=\"token string single-quoted-string\">'error'</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">[</span><span class=\"token string single-quoted-string\">'message'</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n            <span class=\"token punctuation\">}</span> <span class=\"token keyword\">else</span> <span class=\"token punctuation\">{</span>\n                <span class=\"token class-name static-context\">Log</span><span class=\"token operator\">::</span><span class=\"token function\">error</span><span class=\"token punctuation\">(</span><span class=\"token string single-quoted-string\">'Request Failed'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n            <span class=\"token punctuation\">}</span>\n\n            <span class=\"token keyword\">return</span> <span class=\"token function\">response</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token operator\">-></span><span class=\"token function\">redirectTo</span><span class=\"token punctuation\">(</span><span class=\"token string single-quoted-string\">'/'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n        <span class=\"token punctuation\">}</span>\n\n        <span class=\"token comment\">//successfully added</span>\n        <span class=\"token keyword\">return</span> <span class=\"token function\">response</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token operator\">-></span><span class=\"token function\">redirectTo</span><span class=\"token punctuation\">(</span><span class=\"token string single-quoted-string\">'/post/'</span> <span class=\"token operator\">.</span> <span class=\"token variable\">$id</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n    <span class=\"token punctuation\">}</span></code></pre></div><p>You first format the data as Strapi likes it. When adding a content type entry, you should nest the data inside a <code class=\"language-text\">data</code> parameter. Here, you add the <code class=\"language-text\">email</code>, <code class=\"language-text\">content</code>, and <code class=\"language-text\">post</code> fields. Notice that we are skipping validation here for tutorial simplicity.</p><p>Then, you send a <code class=\"language-text\">POST</code> request to the endpoint <code class=\"language-text\">/api/comments</code>. Strapi's endpoint pattern for adding a content type entry is <code class=\"language-text\">/api/{collection_name}</code>. You pass the data as a second parameter to the <code class=\"language-text\">post</code> method.</p><p>If the request fails, the user is redirected to the home page. If it's successful, the user is redirected back to the post's page.</p><p>Next, add before the comment form in <code class=\"language-text\">resources/views/post.blade.php</code> the following:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"php\"><pre class=\"language-php\"><code class=\"language-php\"><span class=\"token operator\">&#x3C;</span>hr<span class=\"token operator\">/</span><span class=\"token operator\">></span>\n@<span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span><span class=\"token function\">count</span><span class=\"token punctuation\">(</span><span class=\"token variable\">$post</span><span class=\"token punctuation\">[</span><span class=\"token string single-quoted-string\">'attributes'</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">[</span><span class=\"token string single-quoted-string\">'comments'</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">[</span><span class=\"token string single-quoted-string\">'data'</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span>\n    <span class=\"token operator\">&#x3C;</span>div <span class=\"token keyword\">class</span><span class=\"token operator\">=</span><span class=\"token string double-quoted-string\">\"comments\"</span><span class=\"token operator\">></span>\n        <span class=\"token operator\">&#x3C;</span>h2<span class=\"token operator\">></span>Comments<span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>h2<span class=\"token operator\">></span>\n        @<span class=\"token keyword\">foreach</span> <span class=\"token punctuation\">(</span><span class=\"token variable\">$post</span><span class=\"token punctuation\">[</span><span class=\"token string single-quoted-string\">'attributes'</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">[</span><span class=\"token string single-quoted-string\">'comments'</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">[</span><span class=\"token string single-quoted-string\">'data'</span><span class=\"token punctuation\">]</span> <span class=\"token keyword\">as</span> <span class=\"token variable\">$comment</span><span class=\"token punctuation\">)</span>\n            <span class=\"token operator\">&#x3C;</span>div <span class=\"token keyword\">class</span><span class=\"token operator\">=</span><span class=\"token string double-quoted-string\">\"card mb-3\"</span><span class=\"token operator\">></span>\n                <span class=\"token operator\">&#x3C;</span>div <span class=\"token keyword\">class</span><span class=\"token operator\">=</span><span class=\"token string double-quoted-string\">\"card-body\"</span><span class=\"token operator\">></span>\n                \t<span class=\"token punctuation\">{</span><span class=\"token punctuation\">{</span> <span class=\"token variable\">$comment</span><span class=\"token punctuation\">[</span><span class=\"token string single-quoted-string\">'attributes'</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">[</span><span class=\"token string single-quoted-string\">'content'</span><span class=\"token punctuation\">]</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">}</span>\n                <span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>div<span class=\"token operator\">></span>\n                <span class=\"token operator\">&#x3C;</span>div <span class=\"token keyword\">class</span><span class=\"token operator\">=</span><span class=\"token string double-quoted-string\">\"card-footer\"</span><span class=\"token operator\">></span>\n                \tBy <span class=\"token punctuation\">{</span><span class=\"token punctuation\">{</span> <span class=\"token variable\">$comment</span><span class=\"token punctuation\">[</span><span class=\"token string single-quoted-string\">'attributes'</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">[</span><span class=\"token string single-quoted-string\">'email'</span><span class=\"token punctuation\">]</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">}</span>\n                <span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>div<span class=\"token operator\">></span>\n            <span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>div<span class=\"token operator\">></span>\n        @<span class=\"token keyword\">endforeach</span>\n    <span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>div<span class=\"token operator\">></span>\n@<span class=\"token keyword\">endif</span></code></pre></div><p>This will show the comments if a post has any.</p><p>Finally, add the new route in <code class=\"language-text\">routes/web.php</code>:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"php\"><pre class=\"language-php\"><code class=\"language-php\"><span class=\"token class-name static-context\">Route</span><span class=\"token operator\">::</span><span class=\"token function\">post</span><span class=\"token punctuation\">(</span><span class=\"token string single-quoted-string\">'/post/{id}'</span><span class=\"token punctuation\">,</span> <span class=\"token punctuation\">[</span><span class=\"token class-name class-name-fully-qualified static-context\"><span class=\"token punctuation\">\\</span>App<span class=\"token punctuation\">\\</span>Http<span class=\"token punctuation\">\\</span>Controllers<span class=\"token punctuation\">\\</span>BlogController</span><span class=\"token operator\">::</span><span class=\"token keyword\">class</span><span class=\"token punctuation\">,</span> <span class=\"token string single-quoted-string\">'addComment'</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code></pre></div><p>Let's test it out. Go to a post's page, then go to the comment form. Add your comment and click Submit. You'll be redirected back to the post's page, but you can see the comment below the post.</p><figure class=\"kg-card kg-image-card\"><img src=\"https://backend.shahednasser.com/content/images/2021/12/Screen-Shot-2021-12-16-at-8.10.34-PM.png\" class=\"kg-image\" alt=\"\" loading=\"lazy\" width=\"2000\" height=\"443\" srcset=\"https://backend.shahednasser.com/content/images/size/w600/2021/12/Screen-Shot-2021-12-16-at-8.10.34-PM.png 600w, https://backend.shahednasser.com/content/images/size/w1000/2021/12/Screen-Shot-2021-12-16-at-8.10.34-PM.png 1000w, https://backend.shahednasser.com/content/images/size/w1600/2021/12/Screen-Shot-2021-12-16-at-8.10.34-PM.png 1600w, https://backend.shahednasser.com/content/images/size/w2400/2021/12/Screen-Shot-2021-12-16-at-8.10.34-PM.png 2400w\" sizes=\"(min-width: 720px) 720px\"></figure><h2 id=\"conclusion\">Conclusion</h2><p>In this tutorial, you learned how to build a blog with Laravel and Strapi. Strapi is completely customizable, and that simplifies the process of adding content types, adding entries, and using its endpoints to query the content types or add new entries in them.</p>","htmlAst":{"type":"root","children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"https://laravel.com"},"children":[{"type":"text","value":"Laravel"}]},{"type":"text","value":" is one of the most popular web frameworks that allow you to create secure and scalable websites. With Laravel, you can create almost any website, ranging from simple portfolio websites to full-fledged e-commerce solutions."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"https://strapi.io"},"children":[{"type":"text","value":"Strapi"}]},{"type":"text","value":" is a headless CMS framework that has been gaining a lot of attention. With Strapi, you can easily add CMS functionalities to your website, regardless of the technology being used. Strapi also removes the hassles of creating a database and models tailored to your CMS needs as it is customizable to the point that you can create your own models and entities, also called content types."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In this tutorial, you'll learn how to create a blog in Laravel using Strapi. The blog will have posts, tags, and allow users to post comments. You can find the code for this tutorial in "},{"type":"element","tagName":"a","properties":{"href":"https://github.com/shahednasser/strapi-laravel-blog"},"children":[{"type":"text","value":"this GitHub repository"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"h2","properties":{"id":"prerequisites"},"children":[{"type":"text","value":"Prerequisites "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Before you start, you need the following tools on your machine:"}]},{"type":"element","tagName":"ol","properties":{},"children":[{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"https://nodejs.org/en/"},"children":[{"type":"text","value":"Node.js"}]},{"type":"text","value":". Only versions 12 and 14 are supported by Strapi, and 14 is recommended."}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"https://www.php.net"},"children":[{"type":"text","value":"PHP"}]},{"type":"text","value":" >= v7.3"}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"https://getcomposer.org/download/"},"children":[{"type":"text","value":"Composer"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please note that this tutorial will be using Strapi v4 and Laravel v8.9"}]},{"type":"element","tagName":"h2","properties":{"id":"setup-strapi"},"children":[{"type":"text","value":"Setup Strapi"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Start by setting up Strapi. In your terminal, run the following command:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"bash"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-bash"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-bash"]},"children":[{"type":"text","value":"npx create-strapi-app@latest strapi --quickstart"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Once the command is done, the server will start at "},{"type":"element","tagName":"a","properties":{"href":"http://localhost:1337"},"children":[{"type":"text","value":"localhost:1337"}]},{"type":"text","value":" and a new page will open in a browser. It will be a registration form, where you need to create an admin account on Strapi."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Once you're done, you'll be redirected to the main dashboard."}]},{"type":"element","tagName":"figure","properties":{"className":["kg-card","kg-image-card"]},"children":[{"type":"element","tagName":"img","properties":{"src":"https://backend.shahednasser.com/content/images/2021/12/Screen-Shot-2021-12-15-at-7.40.33-PM.png","className":["kg-image"],"alt":"","loading":"lazy","width":2000,"height":1136,"srcSet":["https://backend.shahednasser.com/content/images/size/w600/2021/12/Screen-Shot-2021-12-15-at-7.40.33-PM.png 600w","https://backend.shahednasser.com/content/images/size/w1000/2021/12/Screen-Shot-2021-12-15-at-7.40.33-PM.png 1000w","https://backend.shahednasser.com/content/images/size/w1600/2021/12/Screen-Shot-2021-12-15-at-7.40.33-PM.png 1600w","https://backend.shahednasser.com/content/images/size/w2400/2021/12/Screen-Shot-2021-12-15-at-7.40.33-PM.png 2400w"],"sizes":"(min-width: 720px) 720px"},"children":[]}]},{"type":"element","tagName":"h3","properties":{"id":"create-content-types"},"children":[{"type":"text","value":"Create Content-Types"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Next, you'll create the content types necessary for the blog. You'll create content types for the posts and tags that the posts will use. You'll also create a comment content type which will be filled from our blog on Laravel."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Start by clicking on "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Create your first Content type"}]},{"type":"text","value":" in the dashboard. This will open a new page to create content types. Under the Content Types Builder sidebar, click on "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Create new collection type "}]},{"type":"text","value":"under  Collection type. This will open a pop-up where you can enter basic and advanced settings."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You'll first create the tags content type. Enter in the Display Name field in the popup "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Tag"}]},{"type":"text","value":". This will automatically generate the singular and plural forms of the content type."}]},{"type":"element","tagName":"figure","properties":{"className":["kg-card","kg-image-card"]},"children":[{"type":"element","tagName":"img","properties":{"src":"https://backend.shahednasser.com/content/images/2021/12/Screen-Shot-2021-12-15-at-7.45.23-PM.png","className":["kg-image"],"alt":"","loading":"lazy","width":1814,"height":1026,"srcSet":["https://backend.shahednasser.com/content/images/size/w600/2021/12/Screen-Shot-2021-12-15-at-7.45.23-PM.png 600w","https://backend.shahednasser.com/content/images/size/w1000/2021/12/Screen-Shot-2021-12-15-at-7.45.23-PM.png 1000w","https://backend.shahednasser.com/content/images/size/w1600/2021/12/Screen-Shot-2021-12-15-at-7.45.23-PM.png 1600w","https://backend.shahednasser.com/content/images/2021/12/Screen-Shot-2021-12-15-at-7.45.23-PM.png 1814w"],"sizes":"(min-width: 720px) 720px"},"children":[]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Once you're done, click "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Continue"}]},{"type":"text","value":". You can now choose the fields in this content type. tags will only have a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"name"}]},{"type":"text","value":" field other than their id. So, click on the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Text "}]},{"type":"text","value":"field type. Then, enter in the Name field "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"name"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"figure","properties":{"className":["kg-card","kg-image-card"]},"children":[{"type":"element","tagName":"img","properties":{"src":"https://backend.shahednasser.com/content/images/2021/12/Screen-Shot-2021-12-15-at-7.47.28-PM.png","className":["kg-image"],"alt":"","loading":"lazy","width":1768,"height":1080,"srcSet":["https://backend.shahednasser.com/content/images/size/w600/2021/12/Screen-Shot-2021-12-15-at-7.47.28-PM.png 600w","https://backend.shahednasser.com/content/images/size/w1000/2021/12/Screen-Shot-2021-12-15-at-7.47.28-PM.png 1000w","https://backend.shahednasser.com/content/images/size/w1600/2021/12/Screen-Shot-2021-12-15-at-7.47.28-PM.png 1600w","https://backend.shahednasser.com/content/images/2021/12/Screen-Shot-2021-12-15-at-7.47.28-PM.png 1768w"],"sizes":"(min-width: 720px) 720px"},"children":[]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the Advanced Settings tab, check the Required checkbox to ensure that all tags have a name. "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Since that's the only field you'll add for the Tag content type, click on "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Finish"}]},{"type":"text","value":". Then, when the pop-up closes, click on the"},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":" "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Save"}]}]},{"type":"text","value":" button at the top right. This will restart the server. Every time you create a content type, the server is restarted."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Next, you'll create the Post content type. Again, click on "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Create new collection type. "}]},{"type":"text","value":"In the pop up that opens, enter for Display Name "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Post"}]},{"type":"text","value":", then click on "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Continue."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"For posts, there will be fields for title, content, image, date posted, and tags that the post falls under."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"For the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"title"}]},{"type":"text","value":" field, choose the Text field and make it required as we did earlier. Once done, click on "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Add another field."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"For the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"content"}]},{"type":"text","value":" field, choose the Rich text field, and make it required."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"For the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"image"}]},{"type":"text","value":" field, choose the Media field, and choose for Type \"Single media\". In the Advanced Settings tab, change the allowed file types under \"Select allowed types of media\" to only Images. Make the field required as well."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"For the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"date_posted"}]},{"type":"text","value":" field, choose the Date field, and choose for Type \"datetime\". Mark this field required as well."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Finally, for the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"tags"}]},{"type":"text","value":" field, choose the Relation field, then for the relation type choose \"Post belongs to many Tags\"."}]},{"type":"element","tagName":"figure","properties":{"className":["kg-card","kg-image-card"]},"children":[{"type":"element","tagName":"img","properties":{"src":"https://backend.shahednasser.com/content/images/2021/12/Screen-Shot-2021-12-15-at-7.59.01-PM.png","className":["kg-image"],"alt":"","loading":"lazy","width":1830,"height":902,"srcSet":["https://backend.shahednasser.com/content/images/size/w600/2021/12/Screen-Shot-2021-12-15-at-7.59.01-PM.png 600w","https://backend.shahednasser.com/content/images/size/w1000/2021/12/Screen-Shot-2021-12-15-at-7.59.01-PM.png 1000w","https://backend.shahednasser.com/content/images/size/w1600/2021/12/Screen-Shot-2021-12-15-at-7.59.01-PM.png 1600w","https://backend.shahednasser.com/content/images/2021/12/Screen-Shot-2021-12-15-at-7.59.01-PM.png 1830w"],"sizes":"(min-width: 720px) 720px"},"children":[]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Once you're done, click on "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Finish"}]},{"type":"text","value":", then click on"},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":" Save "}]},{"type":"text","value":"at the top right. This will save the new content type and restart the server."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Finally, you need to create the Comment content type. Just like you did with the previous content types, create a new content type with the name "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Comment"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Comment"}]},{"type":"text","value":" content type will have 3 fields. The first is an Email field with the name "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"field"}]},{"type":"text","value":". Make sure to set it as required."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The second field is a Rich text field with the name "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"content"}]},{"type":"text","value":". This is where the user's comment will go."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The third field is a Relation field between Comment and Post. The relation should be \"Post has many Comments\"."}]},{"type":"element","tagName":"figure","properties":{"className":["kg-card","kg-image-card"]},"children":[{"type":"element","tagName":"img","properties":{"src":"https://backend.shahednasser.com/content/images/2021/12/Screen-Shot-2021-12-16-at-8.13.33-PM.png","className":["kg-image"],"alt":"","loading":"lazy","width":1862,"height":972,"srcSet":["https://backend.shahednasser.com/content/images/size/w600/2021/12/Screen-Shot-2021-12-16-at-8.13.33-PM.png 600w","https://backend.shahednasser.com/content/images/size/w1000/2021/12/Screen-Shot-2021-12-16-at-8.13.33-PM.png 1000w","https://backend.shahednasser.com/content/images/size/w1600/2021/12/Screen-Shot-2021-12-16-at-8.13.33-PM.png 1600w","https://backend.shahednasser.com/content/images/2021/12/Screen-Shot-2021-12-16-at-8.13.33-PM.png 1862w"],"sizes":"(min-width: 720px) 720px"},"children":[]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Note that when you create this field a new field will be added automatically in Post called "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"comments"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Once you're done, click on "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Finish"}]},{"type":"text","value":", then click on"},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":" Save "}]},{"type":"text","value":"at the top right. This will save the new content type and restart the server."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Our content types are ready!"}]},{"type":"element","tagName":"h3","properties":{"id":"add-content"},"children":[{"type":"text","value":"Add Content"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The next step would be to add content. Click on "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Content Manager"}]},{"type":"text","value":" in the sidebar.  Start by adding a few tags by clicking on "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Tag"}]},{"type":"text","value":" in the Content Manager sidebar, then click on "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Add new entry"}]},{"type":"text","value":" at the top right. "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"When you create content, make sure you click "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Publish"}]},{"type":"text","value":" after saving the content."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Next, add posts the same way. You can use "},{"type":"element","tagName":"a","properties":{"href":"https://loremipsum.io/generator/?n=5&t=p"},"children":[{"type":"text","value":"Lorem Ipsum Generator"}]},{"type":"text","value":" if you want to create mock content."}]},{"type":"element","tagName":"h3","properties":{"id":"change-permissions"},"children":[{"type":"text","value":"Change Permissions"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The last step left is to make posts and tags public so that you can consume them in Laravel."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"First, you'll create an API token to use for your requests. In the sidebar, click "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Settings"}]},{"type":"text","value":", then "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"API Token"}]},{"type":"text","value":". Click on "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Add Entry"}]},{"type":"text","value":" at the top right."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In this form, enter the name of the token. This is just useful to remember what your API tokens are for. You can also enter a description."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the Token type field, choose Full Access."}]},{"type":"element","tagName":"figure","properties":{"className":["kg-card","kg-image-card"]},"children":[{"type":"element","tagName":"img","properties":{"src":"https://backend.shahednasser.com/content/images/2021/12/Screen-Shot-2021-12-16-at-7.27.11-PM.png","className":["kg-image"],"alt":"","loading":"lazy","width":1830,"height":678,"srcSet":["https://backend.shahednasser.com/content/images/size/w600/2021/12/Screen-Shot-2021-12-16-at-7.27.11-PM.png 600w","https://backend.shahednasser.com/content/images/size/w1000/2021/12/Screen-Shot-2021-12-16-at-7.27.11-PM.png 1000w","https://backend.shahednasser.com/content/images/size/w1600/2021/12/Screen-Shot-2021-12-16-at-7.27.11-PM.png 1600w","https://backend.shahednasser.com/content/images/2021/12/Screen-Shot-2021-12-16-at-7.27.11-PM.png 1830w"],"sizes":"(min-width: 720px) 720px"},"children":[]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Once you're done, click on "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Save"}]},{"type":"text","value":" at the top right. This will create a new API token and the API token will be shown to you only once when you create it. So, copy the API token and store it somewhere as you'll use it later."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Next, you'll modify the permissions for authenticated users to be able to query content types and add new entries."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"On the sidebar, click "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Settings"}]},{"type":"text","value":", then "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Roles"}]},{"type":"text","value":" in the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Settings"}]},{"type":"text","value":" sidebar."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You'll see two roles: Authenticated and Public. Click on the pencil icon on the Authenticated row."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Scroll down and you'll see that for each content type you can select what this role can access. Check Select All for Post, Tag, and Comment, then click "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Save"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"figure","properties":{"className":["kg-card","kg-image-card"]},"children":[{"type":"element","tagName":"img","properties":{"src":"https://backend.shahednasser.com/content/images/2021/12/Screen-Shot-2021-12-16-at-7.37.10-PM.png","className":["kg-image"],"alt":"","loading":"lazy","width":964,"height":1070,"srcSet":["https://backend.shahednasser.com/content/images/size/w600/2021/12/Screen-Shot-2021-12-16-at-7.37.10-PM.png 600w","https://backend.shahednasser.com/content/images/2021/12/Screen-Shot-2021-12-16-at-7.37.10-PM.png 964w"],"sizes":"(min-width: 720px) 720px"},"children":[]}]},{"type":"element","tagName":"h2","properties":{"id":"setup-laravel"},"children":[{"type":"text","value":"Setup Laravel"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Now that Strapi is ready, you'll get started with Laravel."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Run the following command to create a new Laravel project:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"bash"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-bash"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-bash"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"composer"}]},{"type":"text","value":" create-project laravel/laravel blog"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Once this command is done, change to the directory created:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"bash"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-bash"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-bash"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","builtin","class-name"]},"children":[{"type":"text","value":"cd"}]},{"type":"text","value":" blog"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You can then start the server with the following command:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"bash"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-bash"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-bash"]},"children":[{"type":"text","value":"php artisan serve"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This will start the server at "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"localhost:8000"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"h3","properties":{"id":"add-environment-variables"},"children":[{"type":"text","value":"Add Environment Variables"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Before you can make requests to Strapi, you need to add 2 environment variables. Add the following environment variables to "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":".env"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"text"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-text"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"STRAPI_URL=http://localhost:1337\nSTRAPI_API_TOKEN="}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The first is the URL to Strapi. You can change it if it's not the same local URL. The second is the API token you created earlier. Paste it here after the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" sign."}]},{"type":"element","tagName":"h3","properties":{"id":"add-home-page"},"children":[{"type":"text","value":"Add Home Page"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"On the home page, you'll query all posts in Strapi and display them."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Run the following command in your terminal to create a new controller:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"bash"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-bash"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-bash"]},"children":[{"type":"text","value":"php artisan make:controller BlogController"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Then, open "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"app/Http/Controllers/BlogController.php"}]},{"type":"text","value":" and the following method in the class:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"php"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-php"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-php"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"public"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"function"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function-definition","function"]},"children":[{"type":"text","value":"home"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"//retrieve the posts from Strapi"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","variable"]},"children":[{"type":"text","value":"$response"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name","static-context"]},"children":[{"type":"text","value":"Http"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"::"}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"withToken"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"env"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string","single-quoted-string"]},"children":[{"type":"text","value":"'STRAPI_API_TOKEN'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"->"}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"get"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"env"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string","single-quoted-string"]},"children":[{"type":"text","value":"'STRAPI_URL'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string","single-quoted-string"]},"children":[{"type":"text","value":"'/api/posts?populate=image,tags'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","variable"]},"children":[{"type":"text","value":"$posts"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n\n    "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","variable"]},"children":[{"type":"text","value":"$response"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"->"}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"failed"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n        "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"isset"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","variable"]},"children":[{"type":"text","value":"$data"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string","single-quoted-string"]},"children":[{"type":"text","value":"'error'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n        \t"},{"type":"element","tagName":"span","properties":{"className":["token","class-name","static-context"]},"children":[{"type":"text","value":"Log"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"::"}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"error"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string","single-quoted-string"]},"children":[{"type":"text","value":"'Server error: '"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","variable"]},"children":[{"type":"text","value":"$data"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string","single-quoted-string"]},"children":[{"type":"text","value":"'error'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string","single-quoted-string"]},"children":[{"type":"text","value":"'message'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n        "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n        \t"},{"type":"element","tagName":"span","properties":{"className":["token","class-name","static-context"]},"children":[{"type":"text","value":"Log"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"::"}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"error"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string","single-quoted-string"]},"children":[{"type":"text","value":"'Request Failed'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n        "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n        "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"//get posts from response"}]},{"type":"text","value":"\n        "},{"type":"element","tagName":"span","properties":{"className":["token","variable"]},"children":[{"type":"text","value":"$posts"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","variable"]},"children":[{"type":"text","value":"$response"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"->"}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"json"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string","single-quoted-string"]},"children":[{"type":"text","value":"'data'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n\n    "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"view"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string","single-quoted-string"]},"children":[{"type":"text","value":"'home'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string","single-quoted-string"]},"children":[{"type":"text","value":"'posts'"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=>"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","variable"]},"children":[{"type":"text","value":"$posts"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"First, you query Strapi using Laravel's "},{"type":"element","tagName":"a","properties":{"href":"https://laravel.com/docs/8.x/http-client"},"children":[{"type":"text","value":"HTTP Client"}]},{"type":"text","value":". You use "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"withToken"}]},{"type":"text","value":" to pass it the API token from "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":".env"}]},{"type":"text","value":" using the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"env"}]},{"type":"text","value":" helper function. Then, you send a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"get"}]},{"type":"text","value":" request to the endpoint  "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"localhost:1337/api/posts?populate=image,tags"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Notice that "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"localhost:1337"}]},{"type":"text","value":" also is retrieved from "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":".env"}]},{"type":"text","value":". As for the endpoint path, Strapi has a conventional path for all its content types. When querying a collection, the pattern for the endpoint is "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"/api/{collection_name}"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"When you use Strapi's API, you can pass it a lot of "},{"type":"element","tagName":"a","properties":{"href":"https://docs.strapi.io/developer-docs/latest/developer-resources/database-apis-reference/rest-api.html#api-parameters"},"children":[{"type":"text","value":"useful parameters"}]},{"type":"text","value":" that allow you to filter, sort, paginate the data, and more. Here, you use the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"populate"}]},{"type":"text","value":" parameter which allows you to retrieve a content type with its relations. You use it to retrieve the post with its image and tags."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"After sending the request, you can check if the request failed using "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"$response->failed()"}]},{"type":"text","value":". If the request failed, you log the error. If not, you set "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"$posts"}]},{"type":"text","value":" to the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"data"}]},{"type":"text","value":" parameter in the response body. Note that you can use the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"json"}]},{"type":"text","value":" method to retrieve the parameters from a JSON response, optionally passing it a parameter name as the first element."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Next, you need to add the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"home"}]},{"type":"text","value":" view. Create the file "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"resources/views/home.blade.php"}]},{"type":"text","value":" with the following content:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"php"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-php"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-php"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"!"}]},{"type":"element","tagName":"span","properties":{"className":["token","constant"]},"children":[{"type":"text","value":"DOCTYPE"}]},{"type":"text","value":" html"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"html lang"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string","double-quoted-string"]},"children":[{"type":"text","value":"\"{{ str_replace('_', '-', app()->getLocale()) }}\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"head"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n        "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"meta charset"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string","double-quoted-string"]},"children":[{"type":"text","value":"\"utf-8\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n        "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"meta name"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string","double-quoted-string"]},"children":[{"type":"text","value":"\"viewport\""}]},{"type":"text","value":" content"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string","double-quoted-string"]},"children":[{"type":"text","value":"\"width=device-width, initial-scale=1\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n\n        "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"title"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"Blog"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"title"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n\n        "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"link href"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string","double-quoted-string"]},"children":[{"type":"text","value":"\"https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css\""}]},{"type":"text","value":" rel"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string","double-quoted-string"]},"children":[{"type":"text","value":"\"stylesheet\""}]},{"type":"text","value":" integrity"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string","double-quoted-string"]},"children":[{"type":"text","value":"\"sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3\""}]},{"type":"text","value":" crossorigin"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string","double-quoted-string"]},"children":[{"type":"text","value":"\"anonymous\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"head"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"body "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string","double-quoted-string"]},"children":[{"type":"text","value":"\"antialiased bg-light\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n        "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"div "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string","double-quoted-string"]},"children":[{"type":"text","value":"\"container mt-4 py-3 mx-auto bg-white rounded shadow-sm\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n            "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"div "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string","double-quoted-string"]},"children":[{"type":"text","value":"\"row\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n                @"},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"forelse"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","variable"]},"children":[{"type":"text","value":"$posts"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","variable"]},"children":[{"type":"text","value":"$post"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n                    "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"div "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string","double-quoted-string"]},"children":[{"type":"text","value":"\"col-2 col-md-4\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n                        "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"div "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string","double-quoted-string"]},"children":[{"type":"text","value":"\"card\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n                            "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"img src"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string","double-quoted-string"]},"children":[{"type":"text","value":"\"{{ env('STRAPI_URL') . "},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","variable"]},"children":[{"type":"text","value":"$post"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string","single-quoted-string"]},"children":[{"type":"text","value":"'attributes'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]}]},{"type":"text","value":"['image']['data']['attributes']['formats']['medium']['url'] }}\""}]},{"type":"text","value":" \n                                "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string","double-quoted-string"]},"children":[{"type":"text","value":"\"card-img-top\""}]},{"type":"text","value":" alt"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string","double-quoted-string"]},"children":[{"type":"text","value":"\"{{ "},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","variable"]},"children":[{"type":"text","value":"$post"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string","single-quoted-string"]},"children":[{"type":"text","value":"'attributes'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]}]},{"type":"text","value":"['image']['data']['attributes']['alternativeText'] }}\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n                            "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"div "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string","double-quoted-string"]},"children":[{"type":"text","value":"\"card-body\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n                                "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"h5 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string","double-quoted-string"]},"children":[{"type":"text","value":"\"card-title\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","variable"]},"children":[{"type":"text","value":"$post"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string","single-quoted-string"]},"children":[{"type":"text","value":"'attributes'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string","single-quoted-string"]},"children":[{"type":"text","value":"'title'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"h5"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n                                "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"p "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string","double-quoted-string"]},"children":[{"type":"text","value":"\"card-text\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"substr"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","variable"]},"children":[{"type":"text","value":"$post"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string","single-quoted-string"]},"children":[{"type":"text","value":"'attributes'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string","single-quoted-string"]},"children":[{"type":"text","value":"'content'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"50"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"..."}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"p"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n                                "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"a href"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string","double-quoted-string"]},"children":[{"type":"text","value":"\"/post/{{ "},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","variable"]},"children":[{"type":"text","value":"$post"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string","single-quoted-string"]},"children":[{"type":"text","value":"'id'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]}]},{"type":"text","value":" }}\""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string","double-quoted-string"]},"children":[{"type":"text","value":"\"btn btn-primary\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"Read More"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"a"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n                            "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"div"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n                            "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"div "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string","double-quoted-string"]},"children":[{"type":"text","value":"\"card-footer\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n                                @"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"count"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","variable"]},"children":[{"type":"text","value":"$post"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string","single-quoted-string"]},"children":[{"type":"text","value":"'attributes'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string","single-quoted-string"]},"children":[{"type":"text","value":"'tags'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string","single-quoted-string"]},"children":[{"type":"text","value":"'data'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n                                    @"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"foreach"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","variable"]},"children":[{"type":"text","value":"$post"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string","single-quoted-string"]},"children":[{"type":"text","value":"'attributes'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string","single-quoted-string"]},"children":[{"type":"text","value":"'tags'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string","single-quoted-string"]},"children":[{"type":"text","value":"'data'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","variable"]},"children":[{"type":"text","value":"$tag"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n                                        "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"span "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string","double-quoted-string"]},"children":[{"type":"text","value":"\"badge bg-success\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","variable"]},"children":[{"type":"text","value":"$tag"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string","single-quoted-string"]},"children":[{"type":"text","value":"'attributes'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string","single-quoted-string"]},"children":[{"type":"text","value":"'name'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"span"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n                                    @"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"endforeach"}]},{"type":"text","value":"\n                                @"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"endif"}]},{"type":"text","value":"\n                            "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"div"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n                        "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"div"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n                    "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"div"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n                @"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"empty"}]},{"type":"text","value":"\n                    "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"div "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string","double-quoted-string"]},"children":[{"type":"text","value":"\"col\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n                        "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"div "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string","double-quoted-string"]},"children":[{"type":"text","value":"\"card\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n                            "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"div "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string","double-quoted-string"]},"children":[{"type":"text","value":"\"card-body\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n                                This is some text within a card body"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"\n                            "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"div"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n                        "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"div"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n                    "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"div"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n                @endforelse\n            "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"div"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n        "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"div"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"body"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"html"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This just displays the posts as "},{"type":"element","tagName":"a","properties":{"href":"https://getbootstrap.com/docs/5.1/components/card/"},"children":[{"type":"text","value":"cards"}]},{"type":"text","value":" using "},{"type":"element","tagName":"a","properties":{"href":"https://getbootstrap.com"},"children":[{"type":"text","value":"Bootstrap"}]},{"type":"text","value":". Notice that the content type entries that Strapi return has the following format:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"json"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-json"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-json"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n\t"},{"type":"element","tagName":"span","properties":{"className":["token","property"]},"children":[{"type":"text","value":"\"data\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n    \t"},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"id\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n        "},{"type":"element","tagName":"span","properties":{"className":["token","property"]},"children":[{"type":"text","value":"\"attributes\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n        \t"},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"title\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n            ...\n       \t"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"So, you'll find the content type's fields inside the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"attributes"}]},{"type":"text","value":" key of "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"data"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Finally, change the current route in "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"routes/web.php"}]},{"type":"text","value":" to the following:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"php"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-php"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-php"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","class-name","static-context"]},"children":[{"type":"text","value":"Route"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"::"}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"get"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string","single-quoted-string"]},"children":[{"type":"text","value":"'/'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","class-name","class-name-fully-qualified","static-context"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"\\"}]},{"type":"text","value":"App"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"\\"}]},{"type":"text","value":"Http"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"\\"}]},{"type":"text","value":"Controllers"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"\\"}]},{"type":"text","value":"BlogController"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"::"}]},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string","single-quoted-string"]},"children":[{"type":"text","value":"'home'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's test it out. Make sure that both Laravel and Strapi's servers are running. Then, open "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"localhost:8000"}]},{"type":"text","value":". You'll see the posts you added as cards."}]},{"type":"element","tagName":"figure","properties":{"className":["kg-card","kg-image-card"]},"children":[{"type":"element","tagName":"img","properties":{"src":"https://backend.shahednasser.com/content/images/2021/12/Screen-Shot-2021-12-16-at-7.48.15-PM.png","className":["kg-image"],"alt":"","loading":"lazy","width":2000,"height":1021,"srcSet":["https://backend.shahednasser.com/content/images/size/w600/2021/12/Screen-Shot-2021-12-16-at-7.48.15-PM.png 600w","https://backend.shahednasser.com/content/images/size/w1000/2021/12/Screen-Shot-2021-12-16-at-7.48.15-PM.png 1000w","https://backend.shahednasser.com/content/images/size/w1600/2021/12/Screen-Shot-2021-12-16-at-7.48.15-PM.png 1600w","https://backend.shahednasser.com/content/images/size/w2400/2021/12/Screen-Shot-2021-12-16-at-7.48.15-PM.png 2400w"],"sizes":"(min-width: 720px) 720px"},"children":[]}]},{"type":"element","tagName":"h3","properties":{"id":"add-view-post-page"},"children":[{"type":"text","value":"Add View Post Page"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Next, you'll add the page to view a post. This page receives the post ID as a parameter, then queries the post's data from Strapi."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"app/Http/Controllers/BlogController.php"}]},{"type":"text","value":" add a new method:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"php"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-php"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-php"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"public"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"function"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function-definition","function"]},"children":[{"type":"text","value":"viewPost"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","variable"]},"children":[{"type":"text","value":"$id"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n        "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"//retrieve the post from Strapi"}]},{"type":"text","value":"\n        "},{"type":"element","tagName":"span","properties":{"className":["token","variable"]},"children":[{"type":"text","value":"$response"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name","static-context"]},"children":[{"type":"text","value":"Http"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"::"}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"withToken"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"env"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string","single-quoted-string"]},"children":[{"type":"text","value":"'STRAPI_API_TOKEN'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"->"}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"get"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"env"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string","single-quoted-string"]},"children":[{"type":"text","value":"'STRAPI_URL'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string","single-quoted-string"]},"children":[{"type":"text","value":"'/api/posts/'"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","variable"]},"children":[{"type":"text","value":"$id"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string","single-quoted-string"]},"children":[{"type":"text","value":"'?populate=image,tags,comments'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n\n        "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","variable"]},"children":[{"type":"text","value":"$response"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"->"}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"failed"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n            "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"isset"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","variable"]},"children":[{"type":"text","value":"$data"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string","single-quoted-string"]},"children":[{"type":"text","value":"'error'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n                "},{"type":"element","tagName":"span","properties":{"className":["token","class-name","static-context"]},"children":[{"type":"text","value":"Log"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"::"}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"error"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string","single-quoted-string"]},"children":[{"type":"text","value":"'Server error: '"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","variable"]},"children":[{"type":"text","value":"$data"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string","single-quoted-string"]},"children":[{"type":"text","value":"'error'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string","single-quoted-string"]},"children":[{"type":"text","value":"'message'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n            "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n                "},{"type":"element","tagName":"span","properties":{"className":["token","class-name","static-context"]},"children":[{"type":"text","value":"Log"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"::"}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"error"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string","single-quoted-string"]},"children":[{"type":"text","value":"'Request Failed'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n            "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n\n            "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"response"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"->"}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"redirectTo"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string","single-quoted-string"]},"children":[{"type":"text","value":"'/'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n        "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n\n        "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"//get post from response"}]},{"type":"text","value":"\n        "},{"type":"element","tagName":"span","properties":{"className":["token","variable"]},"children":[{"type":"text","value":"$post"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","variable"]},"children":[{"type":"text","value":"$response"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"->"}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"json"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string","single-quoted-string"]},"children":[{"type":"text","value":"'data'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n\n        "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"view"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string","single-quoted-string"]},"children":[{"type":"text","value":"'post'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string","single-quoted-string"]},"children":[{"type":"text","value":"'post'"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=>"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","variable"]},"children":[{"type":"text","value":"$post"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In this method, you use the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"$id"}]},{"type":"text","value":" parameter, which is the post ID, to send a request to Strapi's single entry endpoint. The endpoint's pattern is "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"/api/{collection_name}/{id}"}]},{"type":"text","value":". Similar to the previous endpoint, you can also pass it parameters like "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"populate"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If the request fails, you redirect the user to the home page and log the error. If the request is done successfully, you retrieve the post from the response's body and render the view "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"post"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Now, create "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"resources/views/post.blade.php"}]},{"type":"text","value":" with the following content:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"php"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-php"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-php"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"!"}]},{"type":"element","tagName":"span","properties":{"className":["token","constant"]},"children":[{"type":"text","value":"DOCTYPE"}]},{"type":"text","value":" html"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"html lang"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string","double-quoted-string"]},"children":[{"type":"text","value":"\"{{ str_replace('_', '-', app()->getLocale()) }}\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"head"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n        "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"meta charset"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string","double-quoted-string"]},"children":[{"type":"text","value":"\"utf-8\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n        "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"meta name"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string","double-quoted-string"]},"children":[{"type":"text","value":"\"viewport\""}]},{"type":"text","value":" content"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string","double-quoted-string"]},"children":[{"type":"text","value":"\"width=device-width, initial-scale=1\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n\n        "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"title"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","variable"]},"children":[{"type":"text","value":"$post"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string","single-quoted-string"]},"children":[{"type":"text","value":"'attributes'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string","single-quoted-string"]},"children":[{"type":"text","value":"'title'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":" Blog"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"title"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n\n        "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"link href"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string","double-quoted-string"]},"children":[{"type":"text","value":"\"https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css\""}]},{"type":"text","value":" rel"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string","double-quoted-string"]},"children":[{"type":"text","value":"\"stylesheet\""}]},{"type":"text","value":" integrity"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string","double-quoted-string"]},"children":[{"type":"text","value":"\"sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3\""}]},{"type":"text","value":" crossorigin"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string","double-quoted-string"]},"children":[{"type":"text","value":"\"anonymous\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"head"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"body "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string","double-quoted-string"]},"children":[{"type":"text","value":"\"antialiased bg-light\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n        "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"div "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string","double-quoted-string"]},"children":[{"type":"text","value":"\"container mt-4 py-3 px-5 mx-auto bg-white rounded shadow-sm\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n            "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"h1"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","variable"]},"children":[{"type":"text","value":"$post"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string","single-quoted-string"]},"children":[{"type":"text","value":"'attributes'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string","single-quoted-string"]},"children":[{"type":"text","value":"'title'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"h1"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n            "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"small "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string","double-quoted-string"]},"children":[{"type":"text","value":"\"text-muted d-block\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","variable"]},"children":[{"type":"text","value":"$post"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string","single-quoted-string"]},"children":[{"type":"text","value":"'attributes'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string","single-quoted-string"]},"children":[{"type":"text","value":"'date_posted'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"small"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n            "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"img src"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string","double-quoted-string"]},"children":[{"type":"text","value":"\"{{ env('STRAPI_URL') . "},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","variable"]},"children":[{"type":"text","value":"$post"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string","single-quoted-string"]},"children":[{"type":"text","value":"'attributes'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]}]},{"type":"text","value":"['image']['data']['attributes']['formats']['medium']['url'] }}\""}]},{"type":"text","value":" \n                                "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string","double-quoted-string"]},"children":[{"type":"text","value":"\"img-fluid mx-auto d-block my-3\""}]},{"type":"text","value":" alt"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string","double-quoted-string"]},"children":[{"type":"text","value":"\"{{ "},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","variable"]},"children":[{"type":"text","value":"$post"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string","single-quoted-string"]},"children":[{"type":"text","value":"'attributes'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]}]},{"type":"text","value":"['image']['data']['attributes']['alternativeText'] }}\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n            @"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"count"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","variable"]},"children":[{"type":"text","value":"$post"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string","single-quoted-string"]},"children":[{"type":"text","value":"'attributes'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string","single-quoted-string"]},"children":[{"type":"text","value":"'tags'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string","single-quoted-string"]},"children":[{"type":"text","value":"'data'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n              "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"div "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string","double-quoted-string"]},"children":[{"type":"text","value":"\"mb-3\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n                @"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"foreach"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","variable"]},"children":[{"type":"text","value":"$post"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string","single-quoted-string"]},"children":[{"type":"text","value":"'attributes'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string","single-quoted-string"]},"children":[{"type":"text","value":"'tags'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string","single-quoted-string"]},"children":[{"type":"text","value":"'data'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","variable"]},"children":[{"type":"text","value":"$tag"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n                  "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"span "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string","double-quoted-string"]},"children":[{"type":"text","value":"\"badge bg-success\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","variable"]},"children":[{"type":"text","value":"$tag"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string","single-quoted-string"]},"children":[{"type":"text","value":"'attributes'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string","single-quoted-string"]},"children":[{"type":"text","value":"'name'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"span"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n                @"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"endforeach"}]},{"type":"text","value":"\n              "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"div"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n            @"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"endif"}]},{"type":"text","value":"\n            "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"p "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string","double-quoted-string"]},"children":[{"type":"text","value":"\"content\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n              "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","variable"]},"children":[{"type":"text","value":"$post"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string","single-quoted-string"]},"children":[{"type":"text","value":"'attributes'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string","single-quoted-string"]},"children":[{"type":"text","value":"'content'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n            "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"p"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n\n            "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"hr "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n            "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"form action"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string","double-quoted-string"]},"children":[{"type":"text","value":"\"/post/{{ "},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","variable"]},"children":[{"type":"text","value":"$post"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string","single-quoted-string"]},"children":[{"type":"text","value":"'id'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]}]},{"type":"text","value":" }}\""}]},{"type":"text","value":" method"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string","double-quoted-string"]},"children":[{"type":"text","value":"\"POST\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n              @csrf\n              "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"h2"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"Add Your Comment"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"h2"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n              "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"div "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string","double-quoted-string"]},"children":[{"type":"text","value":"\"mb-3\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n                "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"label "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string","double-quoted-string"]},"children":[{"type":"text","value":"\"email\""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string","double-quoted-string"]},"children":[{"type":"text","value":"\"form-label\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"Email address"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"label"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n                "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"input type"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string","double-quoted-string"]},"children":[{"type":"text","value":"\"email\""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string","double-quoted-string"]},"children":[{"type":"text","value":"\"form-control\""}]},{"type":"text","value":" id"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string","double-quoted-string"]},"children":[{"type":"text","value":"\"email\""}]},{"type":"text","value":" name"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string","double-quoted-string"]},"children":[{"type":"text","value":"\"email\""}]},{"type":"text","value":" required"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n              "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"div"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n              "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"div "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string","double-quoted-string"]},"children":[{"type":"text","value":"\"mb-3\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n                "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"label "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string","double-quoted-string"]},"children":[{"type":"text","value":"\"content\""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string","double-quoted-string"]},"children":[{"type":"text","value":"\"form-label\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"Your Comment"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"label"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n                "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"textarea rows"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string","double-quoted-string"]},"children":[{"type":"text","value":"\"5\""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string","double-quoted-string"]},"children":[{"type":"text","value":"\"form-control\""}]},{"type":"text","value":" id"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string","double-quoted-string"]},"children":[{"type":"text","value":"\"content\""}]},{"type":"text","value":" name"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string","double-quoted-string"]},"children":[{"type":"text","value":"\"content\""}]},{"type":"text","value":" required"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"textarea"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n              "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"div"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n              "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"button type"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string","double-quoted-string"]},"children":[{"type":"text","value":"\"submit\""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string","double-quoted-string"]},"children":[{"type":"text","value":"\"btn btn-primary\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"Submit"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"button"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n            "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"form"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n        "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"div"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"body"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"html"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This page just shows the post's details. Similar to how you extracted the post's data, the post's field is nested inside the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"attributes"}]},{"type":"text","value":" field."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This page also has a comments form at the end of it. You'll implement its functionality after this."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Finally, add the new route in "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"routes/web.php"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"php"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-php"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-php"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","class-name","static-context"]},"children":[{"type":"text","value":"Route"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"::"}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"get"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string","single-quoted-string"]},"children":[{"type":"text","value":"'/post/{id}'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","class-name","class-name-fully-qualified","static-context"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"\\"}]},{"type":"text","value":"App"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"\\"}]},{"type":"text","value":"Http"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"\\"}]},{"type":"text","value":"Controllers"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"\\"}]},{"type":"text","value":"BlogController"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"::"}]},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string","single-quoted-string"]},"children":[{"type":"text","value":"'viewPost'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Now, open the home page again and click on "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Read More"}]},{"type":"text","value":" for one of the posts. A new page will open with the post's content."}]},{"type":"element","tagName":"figure","properties":{"className":["kg-card","kg-image-card"]},"children":[{"type":"element","tagName":"img","properties":{"src":"https://backend.shahednasser.com/content/images/2021/12/Screen-Shot-2021-12-16-at-7.55.39-PM.png","className":["kg-image"],"alt":"","loading":"lazy","width":2000,"height":1161,"srcSet":["https://backend.shahednasser.com/content/images/size/w600/2021/12/Screen-Shot-2021-12-16-at-7.55.39-PM.png 600w","https://backend.shahednasser.com/content/images/size/w1000/2021/12/Screen-Shot-2021-12-16-at-7.55.39-PM.png 1000w","https://backend.shahednasser.com/content/images/size/w1600/2021/12/Screen-Shot-2021-12-16-at-7.55.39-PM.png 1600w","https://backend.shahednasser.com/content/images/size/w2400/2021/12/Screen-Shot-2021-12-16-at-7.55.39-PM.png 2400w"],"sizes":"(min-width: 720px) 720px"},"children":[]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If you scroll down, you'll see a form to add your comment."}]},{"type":"element","tagName":"figure","properties":{"className":["kg-card","kg-image-card"]},"children":[{"type":"element","tagName":"img","properties":{"src":"https://backend.shahednasser.com/content/images/2021/12/Screen-Shot-2021-12-16-at-8.10.42-PM.png","className":["kg-image"],"alt":"","loading":"lazy","width":2000,"height":589,"srcSet":["https://backend.shahednasser.com/content/images/size/w600/2021/12/Screen-Shot-2021-12-16-at-8.10.42-PM.png 600w","https://backend.shahednasser.com/content/images/size/w1000/2021/12/Screen-Shot-2021-12-16-at-8.10.42-PM.png 1000w","https://backend.shahednasser.com/content/images/size/w1600/2021/12/Screen-Shot-2021-12-16-at-8.10.42-PM.png 1600w","https://backend.shahednasser.com/content/images/size/w2400/2021/12/Screen-Shot-2021-12-16-at-8.10.42-PM.png 2400w"],"sizes":"(min-width: 720px) 720px"},"children":[]}]},{"type":"element","tagName":"h3","properties":{"id":"add-comment-functionality"},"children":[{"type":"text","value":"Add Comment Functionality"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The last thing you'll do in this tutorial is add the commenting functionality. The form is already added, so you just need to add the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"POST"}]},{"type":"text","value":" route to add the comment."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Add the following method in "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"app/Http/Controllers/BlogController.php"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"php"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-php"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-php"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"public"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"function"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function-definition","function"]},"children":[{"type":"text","value":"addComment"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","class-name","type-declaration"]},"children":[{"type":"text","value":"Request"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","variable"]},"children":[{"type":"text","value":"$request"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","variable"]},"children":[{"type":"text","value":"$id"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n        "},{"type":"element","tagName":"span","properties":{"className":["token","variable"]},"children":[{"type":"text","value":"$data"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"\n            "},{"type":"element","tagName":"span","properties":{"className":["token","string","double-quoted-string"]},"children":[{"type":"text","value":"\"data\""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=>"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"\n                "},{"type":"element","tagName":"span","properties":{"className":["token","string","single-quoted-string"]},"children":[{"type":"text","value":"'email'"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=>"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","variable"]},"children":[{"type":"text","value":"$request"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"->"}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"get"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string","single-quoted-string"]},"children":[{"type":"text","value":"'email'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n                "},{"type":"element","tagName":"span","properties":{"className":["token","string","single-quoted-string"]},"children":[{"type":"text","value":"'content'"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=>"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","variable"]},"children":[{"type":"text","value":"$request"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"->"}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"get"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string","single-quoted-string"]},"children":[{"type":"text","value":"'content'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n                "},{"type":"element","tagName":"span","properties":{"className":["token","string","single-quoted-string"]},"children":[{"type":"text","value":"'post'"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=>"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","variable"]},"children":[{"type":"text","value":"$id"}]},{"type":"text","value":"\n            "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n        "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n\n        "},{"type":"element","tagName":"span","properties":{"className":["token","variable"]},"children":[{"type":"text","value":"$response"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name","static-context"]},"children":[{"type":"text","value":"Http"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"::"}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"withToken"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"env"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string","single-quoted-string"]},"children":[{"type":"text","value":"'STRAPI_API_TOKEN'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"->"}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"post"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"env"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string","single-quoted-string"]},"children":[{"type":"text","value":"'STRAPI_URL'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string","single-quoted-string"]},"children":[{"type":"text","value":"'/api/comments'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","variable"]},"children":[{"type":"text","value":"$data"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n\n        "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","variable"]},"children":[{"type":"text","value":"$response"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"->"}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"failed"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n            "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"isset"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","variable"]},"children":[{"type":"text","value":"$data"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string","single-quoted-string"]},"children":[{"type":"text","value":"'error'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n                "},{"type":"element","tagName":"span","properties":{"className":["token","class-name","static-context"]},"children":[{"type":"text","value":"Log"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"::"}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"error"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string","single-quoted-string"]},"children":[{"type":"text","value":"'Server error: '"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","variable"]},"children":[{"type":"text","value":"$data"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string","single-quoted-string"]},"children":[{"type":"text","value":"'error'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string","single-quoted-string"]},"children":[{"type":"text","value":"'message'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n            "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n                "},{"type":"element","tagName":"span","properties":{"className":["token","class-name","static-context"]},"children":[{"type":"text","value":"Log"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"::"}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"error"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string","single-quoted-string"]},"children":[{"type":"text","value":"'Request Failed'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n            "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n\n            "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"response"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"->"}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"redirectTo"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string","single-quoted-string"]},"children":[{"type":"text","value":"'/'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n        "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n\n        "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"//successfully added"}]},{"type":"text","value":"\n        "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"response"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"->"}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"redirectTo"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string","single-quoted-string"]},"children":[{"type":"text","value":"'/post/'"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","variable"]},"children":[{"type":"text","value":"$id"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You first format the data as Strapi likes it. When adding a content type entry, you should nest the data inside a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"data"}]},{"type":"text","value":" parameter. Here, you add the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"email"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"content"}]},{"type":"text","value":", and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"post"}]},{"type":"text","value":" fields. Notice that we are skipping validation here for tutorial simplicity."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Then, you send a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"POST"}]},{"type":"text","value":" request to the endpoint "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"/api/comments"}]},{"type":"text","value":". Strapi's endpoint pattern for adding a content type entry is "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"/api/{collection_name}"}]},{"type":"text","value":". You pass the data as a second parameter to the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"post"}]},{"type":"text","value":" method."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If the request fails, the user is redirected to the home page. If it's successful, the user is redirected back to the post's page."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Next, add before the comment form in "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"resources/views/post.blade.php"}]},{"type":"text","value":" the following:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"php"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-php"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-php"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"hr"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n@"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"count"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","variable"]},"children":[{"type":"text","value":"$post"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string","single-quoted-string"]},"children":[{"type":"text","value":"'attributes'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string","single-quoted-string"]},"children":[{"type":"text","value":"'comments'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string","single-quoted-string"]},"children":[{"type":"text","value":"'data'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"div "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string","double-quoted-string"]},"children":[{"type":"text","value":"\"comments\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n        "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"h2"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"Comments"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"h2"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n        @"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"foreach"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","variable"]},"children":[{"type":"text","value":"$post"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string","single-quoted-string"]},"children":[{"type":"text","value":"'attributes'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string","single-quoted-string"]},"children":[{"type":"text","value":"'comments'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string","single-quoted-string"]},"children":[{"type":"text","value":"'data'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","variable"]},"children":[{"type":"text","value":"$comment"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n            "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"div "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string","double-quoted-string"]},"children":[{"type":"text","value":"\"card mb-3\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n                "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"div "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string","double-quoted-string"]},"children":[{"type":"text","value":"\"card-body\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n                \t"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","variable"]},"children":[{"type":"text","value":"$comment"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string","single-quoted-string"]},"children":[{"type":"text","value":"'attributes'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string","single-quoted-string"]},"children":[{"type":"text","value":"'content'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n                "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"div"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n                "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"div "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string","double-quoted-string"]},"children":[{"type":"text","value":"\"card-footer\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n                \tBy "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","variable"]},"children":[{"type":"text","value":"$comment"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string","single-quoted-string"]},"children":[{"type":"text","value":"'attributes'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string","single-quoted-string"]},"children":[{"type":"text","value":"'email'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n                "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"div"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n            "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"div"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n        @"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"endforeach"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"div"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n@"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"endif"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This will show the comments if a post has any."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Finally, add the new route in "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"routes/web.php"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"php"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-php"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-php"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","class-name","static-context"]},"children":[{"type":"text","value":"Route"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"::"}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"post"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string","single-quoted-string"]},"children":[{"type":"text","value":"'/post/{id}'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","class-name","class-name-fully-qualified","static-context"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"\\"}]},{"type":"text","value":"App"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"\\"}]},{"type":"text","value":"Http"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"\\"}]},{"type":"text","value":"Controllers"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"\\"}]},{"type":"text","value":"BlogController"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"::"}]},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string","single-quoted-string"]},"children":[{"type":"text","value":"'addComment'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's test it out. Go to a post's page, then go to the comment form. Add your comment and click Submit. You'll be redirected back to the post's page, but you can see the comment below the post."}]},{"type":"element","tagName":"figure","properties":{"className":["kg-card","kg-image-card"]},"children":[{"type":"element","tagName":"img","properties":{"src":"https://backend.shahednasser.com/content/images/2021/12/Screen-Shot-2021-12-16-at-8.10.34-PM.png","className":["kg-image"],"alt":"","loading":"lazy","width":2000,"height":443,"srcSet":["https://backend.shahednasser.com/content/images/size/w600/2021/12/Screen-Shot-2021-12-16-at-8.10.34-PM.png 600w","https://backend.shahednasser.com/content/images/size/w1000/2021/12/Screen-Shot-2021-12-16-at-8.10.34-PM.png 1000w","https://backend.shahednasser.com/content/images/size/w1600/2021/12/Screen-Shot-2021-12-16-at-8.10.34-PM.png 1600w","https://backend.shahednasser.com/content/images/size/w2400/2021/12/Screen-Shot-2021-12-16-at-8.10.34-PM.png 2400w"],"sizes":"(min-width: 720px) 720px"},"children":[]}]},{"type":"element","tagName":"h2","properties":{"id":"conclusion"},"children":[{"type":"text","value":"Conclusion"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In this tutorial, you learned how to build a blog with Laravel and Strapi. Strapi is completely customizable, and that simplifies the process of adding content types, adding entries, and using its endpoints to query the content types or add new entries in them."}]}],"data":{"quirksMode":false}},"tableOfContents":[{"id":"prerequisites","heading":"Prerequisites "},{"id":"setup-strapi","heading":"Setup Strapi","items":[{"id":"create-content-types","heading":"Create Content-Types"},{"id":"add-content","heading":"Add Content"},{"id":"change-permissions","heading":"Change Permissions"}]},{"id":"setup-laravel","heading":"Setup Laravel","items":[{"id":"add-environment-variables","heading":"Add Environment Variables"},{"id":"add-home-page","heading":"Add Home Page"},{"id":"add-view-post-page","heading":"Add View Post Page"},{"id":"add-comment-functionality","heading":"Add Comment Functionality"}]},{"id":"conclusion","heading":"Conclusion"}]},"featureImageSharp":{"base":"lum3n--RBuQ2PK_L8-unsplash-2.jpg","publicURL":"/static/36dac6a27c1e3c4e16cc2c2e55053679/lum3n--RBuQ2PK_L8-unsplash-2.jpg","imageMeta":{"width":1920,"height":1280},"childImageSharp":{"fluid":{"base64":"data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAANABQDASIAAhEBAxEB/8QAFwAAAwEAAAAAAAAAAAAAAAAAAAEDBf/EABYBAQEBAAAAAAAAAAAAAAAAAAABAv/aAAwDAQACEAMQAAAB1XNzVSQf/8QAFxABAQEBAAAAAAAAAAAAAAAAABEBEP/aAAgBAQABBQLmqqv/xAAUEQEAAAAAAAAAAAAAAAAAAAAQ/9oACAEDAQE/AT//xAAUEQEAAAAAAAAAAAAAAAAAAAAQ/9oACAECAQE/AT//xAAUEAEAAAAAAAAAAAAAAAAAAAAg/9oACAEBAAY/Al//xAAZEAACAwEAAAAAAAAAAAAAAAAAARARMUH/2gAIAQEAAT8hOoZWak//2gAMAwEAAgADAAAAELTv/8QAFhEBAQEAAAAAAAAAAAAAAAAAAQBR/9oACAEDAQE/EFcgb//EABQRAQAAAAAAAAAAAAAAAAAAABD/2gAIAQIBAT8QP//EABsQAQADAQADAAAAAAAAAAAAAAEAITERQXGh/9oACAEBAAE/EOi7B57CZiZ4YJVFMa59gkCjlZP/2Q==","aspectRatio":1.4957264957264957,"src":"/static/36dac6a27c1e3c4e16cc2c2e55053679/ea4ab/lum3n--RBuQ2PK_L8-unsplash-2.jpg","srcSet":"/static/36dac6a27c1e3c4e16cc2c2e55053679/477ba/lum3n--RBuQ2PK_L8-unsplash-2.jpg 175w,\n/static/36dac6a27c1e3c4e16cc2c2e55053679/06776/lum3n--RBuQ2PK_L8-unsplash-2.jpg 350w,\n/static/36dac6a27c1e3c4e16cc2c2e55053679/ea4ab/lum3n--RBuQ2PK_L8-unsplash-2.jpg 700w,\n/static/36dac6a27c1e3c4e16cc2c2e55053679/3055e/lum3n--RBuQ2PK_L8-unsplash-2.jpg 1050w,\n/static/36dac6a27c1e3c4e16cc2c2e55053679/eff08/lum3n--RBuQ2PK_L8-unsplash-2.jpg 1400w,\n/static/36dac6a27c1e3c4e16cc2c2e55053679/74fd5/lum3n--RBuQ2PK_L8-unsplash-2.jpg 1920w","srcWebp":"/static/36dac6a27c1e3c4e16cc2c2e55053679/89afa/lum3n--RBuQ2PK_L8-unsplash-2.webp","srcSetWebp":"/static/36dac6a27c1e3c4e16cc2c2e55053679/9fca7/lum3n--RBuQ2PK_L8-unsplash-2.webp 175w,\n/static/36dac6a27c1e3c4e16cc2c2e55053679/37a4e/lum3n--RBuQ2PK_L8-unsplash-2.webp 350w,\n/static/36dac6a27c1e3c4e16cc2c2e55053679/89afa/lum3n--RBuQ2PK_L8-unsplash-2.webp 700w,\n/static/36dac6a27c1e3c4e16cc2c2e55053679/78e7a/lum3n--RBuQ2PK_L8-unsplash-2.webp 1050w,\n/static/36dac6a27c1e3c4e16cc2c2e55053679/03d34/lum3n--RBuQ2PK_L8-unsplash-2.webp 1400w,\n/static/36dac6a27c1e3c4e16cc2c2e55053679/6833b/lum3n--RBuQ2PK_L8-unsplash-2.webp 1920w","sizes":"(max-width: 700px) 100vw, 700px"}}}},"next":{"id":"Ghost__Post__61b1cb7ad752820612f17bad","title":"React Native Navigation Tutorial","slug":"react-native-navigation-tutorial","featured":false,"feature_image":"https://backend.shahednasser.com/content/images/2021/12/nathan-da-silva-FO7kUmBYVi0-unsplash-2.jpg","excerpt":"In this tutorial, you'll learn how to use React Navigation in your React Native app.","custom_excerpt":"In this tutorial, you'll learn how to use React Navigation in your React Native app.","visibility":"public","created_at_pretty":"9 Dec 2021","published_at_pretty":"9 Dec 2021","updated_at_pretty":"20 Dec 2021","created_at":"2021-12-09T09:25:14.000+00:00","published_at":"2021-12-09T13:38:31.000+00:00","updated_at":"2021-12-20T08:50:59.000+00:00","meta_title":null,"meta_description":null,"og_description":null,"og_image":null,"og_title":null,"twitter_description":null,"twitter_image":null,"twitter_title":null,"authors":[{"slug":"shahed","url":"https://backend.shahednasser.com/author/shahed/","name":"Shahed Nasser","bio":null,"cover_image":null,"profile_image":"https://backend.shahednasser.com/content/images/2022/03/IMG_0591.jpg","location":null,"website":null,"twitter":null,"facebook":null,"meta_title":null,"meta_description":null,"coverImageSharp":null,"profileImageSharp":null}],"primary_author":{"slug":"shahed","url":"https://backend.shahednasser.com/author/shahed/","name":"Shahed Nasser","bio":null,"cover_image":null,"profile_image":"https://backend.shahednasser.com/content/images/2022/03/IMG_0591.jpg","location":null,"website":null,"twitter":null,"facebook":null,"meta_title":null,"meta_description":null,"coverImageSharp":null,"profileImageSharp":{"base":"IMG_0591.jpg","publicURL":"/static/ceb49c3c631485453e71e00d7f84b069/IMG_0591.jpg","imageMeta":{"width":1182,"height":1179},"childImageSharp":{"fluid":{"base64":"data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAUABQDASIAAhEBAxEB/8QAGAABAQEBAQAAAAAAAAAAAAAAAAMEAQL/xAAWAQEBAQAAAAAAAAAAAAAAAAADBAL/2gAMAwEAAhADEAAAAdXiFM6i0CohUWXoKn//xAAcEAACAgIDAAAAAAAAAAAAAAACAwESBBEhM0H/2gAIAQEAAQUCWySE3WEr7SzbXjAj4iKty+sOQ//EABYRAQEBAAAAAAAAAAAAAAAAAAERIP/aAAgBAwEBPwEhj//EABQRAQAAAAAAAAAAAAAAAAAAACD/2gAIAQIBAT8BH//EAB4QAAIBBAMBAAAAAAAAAAAAAAABIRESMUECECJx/9oACAEBAAY/ApVGWvOjzgtUwLlTZA0sdL4f/8QAHBAAAwACAwEAAAAAAAAAAAAAAAERITFBkbHB/9oACAEBAAE/IahkCy+N2GwZpjQiJHJCspUFY0QrSi+HqiW2rgf/2gAMAwEAAgADAAAAEPw3/wD/xAAYEQEBAAMAAAAAAAAAAAAAAAAAARExQf/aAAgBAwEBPxCtjDqP/8QAFxEBAQEBAAAAAAAAAAAAAAAAAQAxEf/aAAgBAgEBPxBFus6Tt//EAB8QAQEAAgIBBQAAAAAAAAAAAAERACExQWFRcYGR0f/aAAgBAQABPxAuaBPPzkO1wyX7F4wkwXanfZrFQgeqE9JgS14vVOvrERIJomVBKwt2jebAeP0yVa8h1n//2Q==","aspectRatio":1,"src":"/static/ceb49c3c631485453e71e00d7f84b069/31709/IMG_0591.jpg","srcSet":"/static/ceb49c3c631485453e71e00d7f84b069/f340b/IMG_0591.jpg 28w,\n/static/ceb49c3c631485453e71e00d7f84b069/22d64/IMG_0591.jpg 55w,\n/static/ceb49c3c631485453e71e00d7f84b069/31709/IMG_0591.jpg 110w,\n/static/ceb49c3c631485453e71e00d7f84b069/aa249/IMG_0591.jpg 165w,\n/static/ceb49c3c631485453e71e00d7f84b069/0dc33/IMG_0591.jpg 220w,\n/static/ceb49c3c631485453e71e00d7f84b069/d8257/IMG_0591.jpg 1182w","srcWebp":"/static/ceb49c3c631485453e71e00d7f84b069/8678c/IMG_0591.webp","srcSetWebp":"/static/ceb49c3c631485453e71e00d7f84b069/59cda/IMG_0591.webp 28w,\n/static/ceb49c3c631485453e71e00d7f84b069/7da75/IMG_0591.webp 55w,\n/static/ceb49c3c631485453e71e00d7f84b069/8678c/IMG_0591.webp 110w,\n/static/ceb49c3c631485453e71e00d7f84b069/f282e/IMG_0591.webp 165w,\n/static/ceb49c3c631485453e71e00d7f84b069/a7b21/IMG_0591.webp 220w,\n/static/ceb49c3c631485453e71e00d7f84b069/63099/IMG_0591.webp 1182w","sizes":"(max-width: 110px) 100vw, 110px"}}}},"primary_tag":{"slug":"react-native","url":"https://backend.shahednasser.com/tag/react-native/","name":"React Native","visibility":"public","feature_image":null,"description":null,"meta_title":null,"meta_description":null,"featureImageSharp":null},"tags":[{"slug":"react-native","url":"https://backend.shahednasser.com/tag/react-native/","name":"React Native","visibility":"public","feature_image":null,"description":null,"meta_title":null,"meta_description":null,"featureImageSharp":null},{"slug":"js","url":"https://backend.shahednasser.com/tag/js/","name":"Javascript","visibility":"public","feature_image":"https://backend.shahednasser.com/content/images/2022/01/photo-1592609931095-54a2168ae893-2.jpeg","description":"Learn more about Javascript through tutorials, articles, and tips.","meta_title":null,"meta_description":"Learn more about Javascript through tutorials, articles and tips.","featureImageSharp":null},{"slug":"beginner","url":"https://backend.shahednasser.com/tag/beginner/","name":"Beginners","visibility":"public","feature_image":"https://backend.shahednasser.com/content/images/2022/01/photo-1521185496955-15097b20c5fe-2.jpeg","description":"Tutorials, articles, and tips to help beginners accelerate their journey in programming.","meta_title":"Beginners","meta_description":"Tutorials, articles, and tips to help beginners accelerate their journey in programming.","featureImageSharp":null}],"plaintext":"React Native [https://reactnative.dev] is one of the most popular cross-platform\napp development frameworks. Using JavaScript, you can develop native apps for\nboth Android and iOS.\n\nOne important part of creating apps is being able to navigate between screens.\nIn this tutorial, you'll learn how to use React Navigation\n[https://reactnavigation.org] in your app to add navigation. This will include\nbasic navigation from one screen to another, going back to the previous screen,\npassing parameters, listening to events, and more.\n\nThis tutorial will not explain the basics of creating a React Native app. If\nyou're interested in learning about that, you can check out my other tutorial \nReact Native Tutorial: Create Your First App\n[https://blog.shahednasser.com/react-native-tutorial-create-your-first-app].\n\nYou can find the code for this tutorial on this GitHub repository\n[https://github.com/shahednasser/react-navigation-tutorial].\n\nPrerequisites \nMake sure you have both Node.js [https://nodejs.org/en/] and NPM installed on\nyour machine. You can check if they're installed with the following command:\n\nnode -v\nnpm -v\n\nIf they're not installed, you can install Node.js [https://nodejs.org/en/] and\nNPM will be installed automatically with it.\n\n After that, make sure you install Expo [https://expo.dev]'s CLI if you don't\nhave it installed:\n\nnpm install -g expo-cli\n\nIf you're not familiar with what Expo is, Expo provides a set of tools built\naround React. Most importantly, it allows you to write and build your React\nNative app in minutes.\n\nYou should also install Expo Go [https://expo.dev/client] on your device to be\nable to test the app as we go through the tutorial.\n\nProject Setup\nStart by creating a new React Native project using Expo's CLI:\n\nexpo init react-navigation-tutorial —npm\n\nYou'll be then prompted to choose the type of app, choose \"blank\".\n\nAfter that the init command will install the dependencies you need for your\nReact Native project.\n\nOnce it's done, change to the directory of the project:\n\ncd react-navigation-tutorial\n\nNow, you'll install the dependencies required to use React Navigation. First,\ninstall the building blocks libraries using Expo's CLI with the following\ncommand:\n\nexpo install react-native-screens react-native-safe-area-context\n\nSecond, install the native stack navigator library:\n\nnpm install @react-navigation/native-stack\n\nNow, you have all the libraries required to start using React Navigation.\n\nFinally, install React Native Paper\n[https://callstack.github.io/react-native-paper/index.html] to have\nbeautifully-styled components in your app:\n\nnpm install react-native-paper\n\nSet Up React Navigation\nBefore you can start adding routes to your app, you need to create a Stack\nNavigator. This stack navigator will be used to create screens and navigate\nbetween them.\n\nIn App.js, change the content to the following:\n\nimport React from 'react';\nimport { NavigationContainer } from '@react-navigation/native';\nimport { createNativeStackNavigator } from '@react-navigation/native-stack';\nimport { Provider as PaperProvider } from 'react-native-paper';\nimport HomeScreen from './screens/HomeScreen';\n\nconst Stack = createNativeStackNavigator()\n\nexport default function App() {\n  return (\n    <PaperProvider>\n      <NavigationContainer>\n        <Stack.Navigator>\n          <Stack.Screen name=\"Home\" component={HomeScreen} />\n        </Stack.Navigator>\n    </NavigationContainer>\n    </PaperProvider>\n  );\n}\n\n\nNotice that in the returned value of App, you first have <PaperProvider> at the\ntop of the elements. This is just used to make sure all screens share the same\ntheme.\n\nNext, you have the <NavigationContainer>. This component manages the navigation\ntree and contains the navigation state. So, it should wrap Stack navigators.\n\nInside <NavigationContainer> you have <Stack.Navigator>. Stack is created using \ncreateNativeStackNavigator. This function returns an object containing 2\nproperties: Screen and Navigator. The Navigator property is a React element and\nit should be used to wrap the Screen elements to manage routing configurations.\n\nSo, inside <Stack.Navigator> you'll have one or more <Stack.Screen> components.\nFor each route, you'll have a <Stack.Screen> component. Now, you just have one\ncomponent that points to the Home route.\n\nNotice that a Screen component takes the name prop, which is the name of the\nroute and will be referenced later on when navigating to this route, and the \ncomponent property which is the component to render for this route.\n\nAs you can see, you're passing HomeScreen component to the Home route, so you\nneed to create it now.\n\nCreate the directory screens, and inside that directory create a new file \nHomeScreen.js with the following content:\n\nimport React from 'react';\nimport { ScrollView, StyleSheet } from 'react-native';\nimport { Button, Card } from 'react-native-paper';\nimport { DefaultTheme } from 'react-native-paper';\n\nfunction HomeScreen () {\n  return (\n    <ScrollView style={styles.scrollView}>\n      <Card style={styles.card}>\n        <Card.Title title=\"Home Screen\" />\n      </Card>\n    </ScrollView>\n  )\n}\n\nconst styles = StyleSheet.create({\n  scrollView: {\n    backgroundColor: DefaultTheme.colors.background,\n    paddingTop: 10\n  },\n  card: {\n    width: '90%',\n    marginLeft: 'auto',\n    marginRight: 'auto'\n  }\n});\n\nexport default HomeScreen\n\nThis will create a basic home screen with a ScrollView\n[https://reactnative.dev/docs/scrollview] component containing a Card\n[https://callstack.github.io/react-native-paper/card.html] component with the\ntitle \"Home Screen\".\n\nYou just created your first route with React Navigation! Let's test it out.\n\nIn your terminal run to start your app:\n\nnpm start\n\nThen, you'll be able to run the app on Expo Go on your phone by scanning the QR\ncode on the Expo Go app on Android or on the Camera app on iOS.\n\nOnce the app run, you'll see the Home screen that you created as it's the\ndefault and only route.\n\nNavigate to Another Screen\nIn this section, you'll learn how to navigate from one screen to another.\n\nScreens that are inside the Stack Navigation, like HomeScreen in this app,\nreceive a navigation prop. This prop allows us to navigate to other screens in\nthe app.\n\nLet's first create the screen you'll navigate to. Create the file \nscreens/BookScreen.js with the following content:\n\nimport React from 'react';\nimport { StyleSheet, View } from 'react-native';\nimport { Card } from 'react-native-paper';\nimport { DefaultTheme } from 'react-native-paper';\n\nfunction BookScreen () {\n  return (\n    <View style={styles.container}>\n      <Card style={styles.card}>\n        <Card.Title title=\"This is Books Screen\" />\n      </Card>\n    </View>\n  );\n}\n\nconst styles = StyleSheet.create({\n  container: {\n    flex: 1,\n    flexDirection: 'column',\n    backgroundColor: DefaultTheme.colors.background,\n    alignItems: 'center',\n    paddingTop: 10\n  },\n  card: {\n    width: '90%'\n  }\n});\n\nexport default BookScreen;\n\nThis screen is similar to HomeScreen. It just shows a card with the title \"This\nis Books Screen\".\n\nNext, add the new route in App.js for BookScreen below the \"Home\" route:\n\n<Stack.Screen name=\"Book\" component={BookScreen} />\n\nDon't forget to import BookScreen below the imports at the beginning of App.js:\n\nimport BookScreen from './screens/BookScreen';\n\nNow, you can navigate to the \"Book\" route inside this stack navigation. To see\nhow it works, you'll add a button in HomeScreen that when the user clicks will\nnavigate to BookScreen.\n\nIn HomeScreen, add the navigation prop to the function:\n\nfunction HomeScreen ({ navigation })\n\nThen, change the returned value of the function to the following:\n\nreturn (\n    <ScrollView style={styles.scrollView}>\n      <Card style={styles.card}>\n        <Card.Title title=\"Navigate to 'Book' Screen\" />\n        <Card.Content>\n          <Button mode=\"contained\" onPress={() => navigation.navigate('Book')}>\n            Navigate\n          </Button>\n        </Card.Content>\n      </Card>\n    </ScrollView>\n  )\n\nYou change the title of the card to \"Navigate to 'Book' Screen\". Then, inside\nthe content of the card, you add a Button\n[https://callstack.github.io/react-native-paper/button.html] component with \nonPress listener. This listener will be executed when the user presses on the\nbutton.\n\nInside the listener, you use the navigation prop to navigate to the Book screen\nusing the method navigate. The method navigate takes the name of the route to\nnavigate to.\n\nLet's test it out. Run the app again if it isn't running. You should see a new\nbutton on the Home screen.\n\nTry clicking on the Navigate button. You'll be navigated to the BookScreen.\n\nNotice how the back button in the navigation bar is added automatically when you\nnavigate to another screen. You can use it to go back to the previous screen,\nwhich is in this case the Home screen.\n\nNavigate with Parameters\nIn a lot of cases, you'll need to navigate to another screen and pass it\nparameters. In this section, you'll create an input that allows the user to\nenter their name, then when they submit you'll open a new screen called \nNameScreen which will show a greeting to the user with the name they entered.\n\nStart by adding a new state variable to hold the value of the input you'll add\nsoon in HomeScreen.js:\n\nconst [name, setName] = useState('');\n\nMake sure to add an import for useState at the beginning of the file:\n\nimport React, { useState } from 'react';\n\nThen, add a new Card component below the existing one:\n\n<Card style={styles.card}>\n    <Card.Title title=\"Navigate with Parameters\" />\n    <Card.Content>\n        <TextInput mode=\"outlined\" label=\"Name\" value={name} onChangeText={(text) => setName(text)} style={styles.textInput} />\n            <Button mode=\"contained\" disabled={name.length === 0} onPress={() => navigation.navigate('Name', { name })}>\n                Navigate\n    \t</Button>\n    </Card.Content>\n</Card>\n\nThis new card has a TextInput\n[https://callstack.github.io/react-native-paper/text-input.html] with the value \nname, and on the onChangeText event, which occurs when the user makes changes to\nthe text inside the input, you change the value of the name state variable.\n\nBelow the input, you have a button that is disabled when the name state variable\nis empty. You also add an onPress event listener which navigates the user to the \nName route. \n\nNotice that you're using the same navigation.navigate method that you used\nbefore to navigate to the \"Book\" route, however, this time you pass a second\nparameter to the method. This method accepts as a second parameter an object of\nparameters you want to pass. Here, you pass the name variable inside the object.\n\nOne last thing you need to do in the HomeScreen is change the styles object to\nthe following:\n\nconst styles = StyleSheet.create({\n  scrollView: {\n    backgroundColor: DefaultTheme.colors.background,\n    paddingTop: 10\n  },\n  card: {\n    width: '90%',\n    marginLeft: 'auto',\n    marginRight: 'auto',\n    marginBottom: 10\n  },\n  textInput: {\n    marginBottom: 10\n  }\n});\n\nThis just makes some changes to the card property and adds a new property \ntextInput.\n\nYou'll now create the NameScreen which will be the component for the \"Name\"\nroute you'll add soon. Create screens/NameScreen.js with the following content:\n\nimport React from 'react';\nimport { StyleSheet, View } from 'react-native';\nimport { Card } from 'react-native-paper';\nimport { DefaultTheme } from 'react-native-paper';\n\nfunction NameScreen ({ route }) {\n  const { name } = route.params;\n\n  return (\n    <View style={styles.container}>\n      <Card style={styles.card}>\n        <Card.Title title={`Hello, ${name}`} />\n      </Card>\n    </View>\n  );\n}\n\nconst styles = StyleSheet.create({\n  container: {\n    flex: 1,\n    flexDirection: 'column',\n    backgroundColor: DefaultTheme.colors.background,\n    alignItems: 'center',\n    paddingTop: 10\n  },\n  card: {\n    width: '90%'\n  }\n});\n\nexport default NameScreen;\n\nThis screen is essentially the same as BookScreen, however, notice the route \nprop passed to the function.\n\nAs mentioned before, the components inside the navigation stack receive a \nnavigation prop to navigate between screens. They also receive a route prop\nwhich you can use to get the parameters passed to the screen using route.params.\n\nSo, in NameScreen, you retrieve the value entered in the Name input in the \nHomeScreen, which is passed when navigating to the NameScreen when the button is\nclicked using route.params:\n\nconst { name } = route.params;\n\nYou then show the user a greeting inside a card using the name parameter.\n\nOne last thing left to do is add the \"Name\" route to the navigation stack. Add a\nnew <Stack.Screen> below the previous ones in App.js:\n\n<Stack.Screen name=\"Name\" component={NameScreen} />\n\nAnd import NameScreen below other imports at the beginning of the file:\n\nimport NameScreen from './screens/NameScreen';\n\nLet's test it out. Run the app if it isn't already running. You'll see that\nthere's a new card on the Home screen with an input and a button.\n\nTry entering your name, then click the \"Navigate\" button below the input. You'll\nbe navigated to the NameScreen and you'll see your name.\n\nYou can try going back then changing the value of the name. The name in the \nNameScreen will change accordingly.\n\nConfigure the Header\nChange the title\nSometimes, you want the title of a route to be different than the name of the\nroute, or you want the title to be dynamic based on a parameter passed or some\nother logic.\n\nIn this section, you'll add a new input in HomeScreen that will let you to enter\na title, then on clicking the navigate button you'll be navigated to a new\nscreen TitleScreen, whose title will change based on the title entered in the\ninput in HomeScreen.\n\nStart by adding a new state variable in HomeScreen.js to store the title:\n\nconst [title, setTitle] = useState('');\n\nThen, add a new card below the previous cards:\n\n<Card style={styles.card}>\n        <Card.Title title=\"Navigate to Route with Title\" />\n        <Card.Content>\n          <TextInput mode=\"outlined\" label=\"Title\" value={title} onChangeText={(text) => setTitle(text)} style={styles.textInput} />\n          <Button mode=\"contained\" disabled={title.length === 0} onPress={() => navigation.navigate('Title', { title })}>\n            Navigate\n          </Button>\n        </Card.Content>\n      </Card>\n\nThis is pretty much the same as the card we added in the previous section. The\nonly difference is that it uses the title state variable, and the listener for \nonPress for the button navigates to the route \"Title\", passing the title as a\nparameter.\n\nNext, create the file screens/Title.js with the following content:\n\nimport React from 'react';\nimport { StyleSheet, View } from 'react-native';\nimport { Card } from 'react-native-paper';\nimport { DefaultTheme } from 'react-native-paper';\n\nfunction TitleScreen () {\n  return (\n    <View style={styles.container}>\n      <Card style={styles.card}>\n        <Card.Title title=\"This is title screen\" />\n      </Card>\n    </View>\n  );\n}\n\nconst styles = StyleSheet.create({\n  container: {\n    flex: 1,\n    flexDirection: 'column',\n    backgroundColor: DefaultTheme.colors.background,\n    alignItems: 'center',\n    paddingTop: 10\n  },\n  card: {\n    width: '90%'\n  }\n});\n\nexport default TitleScreen;\n\nThis screen has nothing special. It just shows a card with the title \"This is\ntitle screen\". It doesn't even use the title parameter.\n\nThat's because you can change the title of a screen by passing an options \nparameter to <Stack.Screen> when adding the route.\n\nIn App.js, add an import for TitleScreen below other imports at the beginning of\nthe file:\n\nimport TitleScreen from './screens/TitleScreen';\n\nThen, add the new route below previous <Stack.Screen>:\n\n<Stack.Screen name=\"Title\" component={TitleScreen} options={({route}) => ({title: route.params.title})} />\n\nEvery Stack.Screen component can take an options prop. The options prop can be\nan object like so:\n\noptions={{title: \"Hello\"}}\n\nThis is enough if your options are static. However, if the value for any of the\noptions is dynamic, like in this example, you can instead pass it a function\nthat returns an object. This function can receive as a parameter route, which is\nsimilar to the route parameter you used in NameScreen:\n\noptions={({route}) => ({title: route.params.title})}\n\nIn this function, you return an object that has a title property with the value \nroute.params.title. So, when the user enters a title in the input and clicks on\nthe Navigate button, the title will be passed to the route, which will change\nthe title of \"Title\" route using the function passed to the options prop.\n\nLet's test it out. Run the app if it's not running already. You should see a new\ncard on the Home screen with a new input:\n\nTry entering any title you want in the input then click on Navigate. You'll see\nthat you'll be navigated to the TitleScreen, but the title will be the value you\nentered in the input.\n\nYou can try going back then changing the value entered in the input. The title\nof the screen will change accordingly.\n\nAdd Header Button\nAnother option you can add is header buttons. You can customize what your header\nshows on the left and right by using the headerRight and headerLeft properties\nin the options param of <Stack.Screen>.\n\nIn this section, you'll add a button to the right of the header of the Home\nscreen which will show a user an alert when they click on it.\n\nIn App.js, add to the \"Home\" route a prop option:\n\n<Stack.Screen name=\"Home\" component={HomeScreen} options={{\n            headerRight: () => (\n              <IconButton icon=\"alert-outline\" onPress={() => alert('You\\'re awesome!')} color={DefaultTheme.colors.notification} />\n            )\n}} />\n\nIn the options prop, you pass an object with the property headerRight. The \nheaderRight property can be a component that you can render elements inside. You\nrender an IconButton\n[https://callstack.github.io/react-native-paper/icon-button.html] component,\nwhich is a button that only has an icon without text. In the onPress event\nlistener, you show an alert to the user.\n\nThat's all. Run the app if it isn't running already. You'll see a new icon at\nthe top right of the header in Home Screen.\n\nClick on it and you'll see an alert.\n\nNavigation Events\nIn React Navigation, you can subscribe to two events: focus and blur. The focus \nevent will occur every time the screen is opened and is currently the main\nscreen. The blur event will occur whenever a screen was in focus but navigation\noccurs that makes the screen not the current main screen. Simply put, the focus \nevent is when the screen is visible to the user, and the blur event occurs when\na screen was visible to a user but isn't anymore. \n\nIn this section, you'll add a counter that will count how many times you\nnavigated to other screens from the home screen. So, this counter will increment\nin the blur event.\n\nTo subscribe to events, in a useEffect callback inside a component, you can use \nnavigation.addListener passing it the name of the event as a first parameter,\nand the event handler as a second parameter. The addListener method returns an\nunsubscribe function which you should return in the useEffect callback.\n\nIn HomeScreen.js, add an import for useEffect at the beginning of the file:\n\nimport React, { useEffect, useState } from 'react';\n\nThen, add a new state variable counter inside the HomeScreen component:\n\nconst [counter, setCounter] = useState(0);\n\nAfter that add the useEffect callback that will register the event handle for\nthe blur event:\n\nuseEffect(() => {\n    return navigation.addListener('blur', () => {\n      setCounter(counter + 1);\n    });\n  });\n\nAs you can see, you use navigation.addListener to add an event listener to the \nblur event. The function passed as a second parameter is the handler of the\nevent. It will be executed whenever the screen is out of focus (not the screen\nvisible to the user). Inside the event handler, you increment the counter by 1.\n\nFinally, you'll show the counter at the top of the screen. In ScrollView, add a\nnew card before the previously added cards:\n\n<Card style={styles.card}>\n\t<Card.Title title={`Navigation Count: ${counter}`} />\n</Card>\n\nLet's test it out. Run the app if it's not already running. You should see a\ncard with a count of navigation at the top of the screen.\n\nGo Back\nThe last thing you'll learn in this tutorial is how to go back to the previous\nscreen. As you've seen throughout the tutorial, when you navigate to a screen\nyou'll see a back button in the header which you can use to go back to the\nprevious screen.\n\nAlthough this is sufficient in most cases, sometimes you want to go back to the\nprevious screen programmatically. You'll create in this section a new screen \nBackScreen which you can go to from the HomeScreen. This screen will have a back\nbutton in its content and when you click on it you can go back to the previous\nscreen, which is the HomeScreen.\n\nCreate the file screens/BackScreen.js with the following content:\n\nimport React from 'react';\nimport { StyleSheet, View } from 'react-native';\nimport { Button, Card } from 'react-native-paper';\nimport { DefaultTheme } from 'react-native-paper';\n\nfunction BackScreen ({ navigation }) {\n  return (\n    <View style={styles.container}>\n      <Card style={styles.card}>\n        <Card.Title title=\"This is Back Screen\" />\n        <Card.Content>\n          <Button mode=\"contained\" onPress={() => navigation.goBack()}>\n            Go Back\n          </Button>\n        </Card.Content>\n      </Card>\n    </View>\n  );\n}\n\nconst styles = StyleSheet.create({\n  container: {\n    flex: 1,\n    flexDirection: 'column',\n    backgroundColor: DefaultTheme.colors.background,\n    alignItems: 'center',\n    paddingTop: 10\n  },\n  card: {\n    width: '90%'\n  }\n});\n\nexport default BackScreen;\n\nThis screen is pretty much the same as other screens. The things you should note\nis that, first, the screen has uses the navigation prop that is passed to\ncomponents inside the navigation stack. Second, the screen has a button inside a\ncard which, on press, will use the navigation prop to go back using the goBack \nmethod:\n\n<Button mode=\"contained\" onPress={() => navigation.goBack()}>\n\nNow, go to the HomeScreen and add a new card at the end of the ScrollView:\n\n<Card style={styles.card}>\n    <Card.Title title=\"Navigate to 'Back' Screen\" />\n    <Card.Content>\n        <Button mode=\"contained\" onPress={() => navigation.navigate('Back')}>\n            Navigate\n    \t</Button>\n    </Card.Content>\n</Card>\n\nThis card just shows a button to navigate to the route \"Back\". Let's add that\nroute now.\n\nIn App.js add a new <Stack.Screen> component below the previously added ones:\n\n<Stack.Screen name=\"Back\" component={BackScreen} />\n\nIt's ready now. Run the app if it isn't already running. You'll see a new card\nat the end of the list.\n\nClick on \"Navigate\" on the last card. You'll be navigated to the BackScreen.\n\nClick on the \"Go Back\" button in the card. You'll be taken back to the \nHomeScreen.\n\nConclusion\nIn this tutorial, you learned how to use React Navigation to navigate between\nscreens, pass a parameter from one screen to another, configure the header of a\nscreen, add navigation event listeners, and go back programmatically to the\nprevious screen.\n\nAlthough this covers most use cases for using React Navigation, be sure to check\nout their documentation [https://reactnavigation.org/docs/getting-started] for\nmore details on how to use it.","html":"<p><a href=\"https://reactnative.dev\">React Native</a> is one of the most popular cross-platform app development frameworks. Using JavaScript, you can develop native apps for both Android and iOS.</p><p>One important part of creating apps is being able to navigate between screens. In this tutorial, you'll learn how to use <a href=\"https://reactnavigation.org\">React Navigation</a> in your app to add navigation. This will include basic navigation from one screen to another, going back to the previous screen, passing parameters, listening to events, and more.</p><p>This tutorial will not explain the basics of creating a React Native app. If you're interested in learning about that, you can check out my other tutorial <a href=\"https://blog.shahednasser.com/react-native-tutorial-create-your-first-app\">React Native Tutorial: Create Your First App</a>.</p><p>You can find the code for this tutorial on <a href=\"https://github.com/shahednasser/react-navigation-tutorial\">this GitHub repository</a>.</p><h2 id=\"prerequisites\">Prerequisites </h2><p>Make sure you have both <a href=\"https://nodejs.org/en/\">Node.js</a> and NPM installed on your machine. You can check if they're installed with the following command:</p><pre><code class=\"language-bash\">node -v\nnpm -v</code></pre><p>If they're not installed, you can <a href=\"https://nodejs.org/en/\">install Node.js</a> and NPM will be installed automatically with it.</p><p> After that, make sure you install <a href=\"https://expo.dev\">Expo</a>'s CLI if you don't have it installed:</p><pre><code class=\"language-bash\">npm install -g expo-cli</code></pre><p>If you're not familiar with what Expo is, Expo provides a set of tools built around React. Most importantly, it allows you to write and build your React Native app in minutes.</p><p>You should also install <a href=\"https://expo.dev/client\">Expo Go</a> on your device to be able to test the app as we go through the tutorial.</p><h2 id=\"project-setup\">Project Setup</h2><p>Start by creating a new React Native project using Expo's CLI:</p><pre><code class=\"language-bash\">expo init react-navigation-tutorial —npm</code></pre><p>You'll be then prompted to choose the type of app, choose \"blank\".</p><p>After that the <code>init</code> command will install the dependencies you need for your React Native project.</p><p>Once it's done, change to the directory of the project:</p><pre><code class=\"language-bash\">cd react-navigation-tutorial</code></pre><p>Now, you'll install the dependencies required to use React Navigation. First, install the building blocks libraries using Expo's CLI with the following command:</p><pre><code class=\"language-bash\">expo install react-native-screens react-native-safe-area-context</code></pre><p>Second, install the native stack navigator library:</p><pre><code class=\"language-bash\">npm install @react-navigation/native-stack</code></pre><p>Now, you have all the libraries required to start using React Navigation.</p><p>Finally, install <a href=\"https://callstack.github.io/react-native-paper/index.html\">React Native Paper</a> to have beautifully-styled components in your app:</p><pre><code class=\"language-bash\">npm install react-native-paper</code></pre><h2 id=\"set-up-react-navigation\">Set Up React Navigation</h2><p>Before you can start adding routes to your app, you need to create a Stack Navigator. This stack navigator will be used to create screens and navigate between them.</p><p>In <code>App.js</code>, change the content to the following:</p><pre><code class=\"language-js\">import React from 'react';\nimport { NavigationContainer } from '@react-navigation/native';\nimport { createNativeStackNavigator } from '@react-navigation/native-stack';\nimport { Provider as PaperProvider } from 'react-native-paper';\nimport HomeScreen from './screens/HomeScreen';\n\nconst Stack = createNativeStackNavigator()\n\nexport default function App() {\n  return (\n    &lt;PaperProvider&gt;\n      &lt;NavigationContainer&gt;\n        &lt;Stack.Navigator&gt;\n          &lt;Stack.Screen name=\"Home\" component={HomeScreen} /&gt;\n        &lt;/Stack.Navigator&gt;\n    &lt;/NavigationContainer&gt;\n    &lt;/PaperProvider&gt;\n  );\n}\n</code></pre><p>Notice that in the returned value of <code>App</code>, you first have <code>&lt;PaperProvider&gt;</code> at the top of the elements. This is just used to make sure all screens share the same theme.</p><p>Next, you have the <code>&lt;NavigationContainer&gt;</code>. This component manages the navigation tree and contains the navigation state. So, it should wrap Stack navigators.</p><p>Inside <code>&lt;NavigationContainer&gt;</code> you have <code>&lt;Stack.Navigator&gt;</code>. <code>Stack</code> is created using <code>createNativeStackNavigator</code>. This function returns an object containing 2 properties: <code>Screen</code> and <code>Navigator</code>. The <code>Navigator</code> property is a React element and it should be used to wrap the <code>Screen</code> elements to manage routing configurations.</p><p>So, inside <code>&lt;Stack.Navigator&gt;</code> you'll have one or more <code>&lt;Stack.Screen&gt;</code> components. For each route, you'll have a <code>&lt;Stack.Screen&gt;</code> component. Now, you just have one component that points to the <code>Home</code> route.</p><p>Notice that a <code>Screen</code> component takes the <code>name</code> prop, which is the name of the route and will be referenced later on when navigating to this route, and the <code>component</code> property which is the component to render for this route.</p><p>As you can see, you're passing <code>HomeScreen</code> component to the <code>Home</code> route, so you need to create it now.</p><p>Create the directory <code>screens</code>, and inside that directory create a new file <code>HomeScreen.js</code> with the following content:</p><pre><code class=\"language-js\">import React from 'react';\nimport { ScrollView, StyleSheet } from 'react-native';\nimport { Button, Card } from 'react-native-paper';\nimport { DefaultTheme } from 'react-native-paper';\n\nfunction HomeScreen () {\n  return (\n    &lt;ScrollView style={styles.scrollView}&gt;\n      &lt;Card style={styles.card}&gt;\n        &lt;Card.Title title=\"Home Screen\" /&gt;\n      &lt;/Card&gt;\n    &lt;/ScrollView&gt;\n  )\n}\n\nconst styles = StyleSheet.create({\n  scrollView: {\n    backgroundColor: DefaultTheme.colors.background,\n    paddingTop: 10\n  },\n  card: {\n    width: '90%',\n    marginLeft: 'auto',\n    marginRight: 'auto'\n  }\n});\n\nexport default HomeScreen</code></pre><p>This will create a basic home screen with a <a href=\"https://reactnative.dev/docs/scrollview\">ScrollView</a> component containing a <a href=\"https://callstack.github.io/react-native-paper/card.html\">Card</a> component with the title \"Home Screen\".</p><p>You just created your first route with React Navigation! Let's test it out.</p><p>In your terminal run to start your app:</p><pre><code class=\"language-bash\">npm start</code></pre><p>Then, you'll be able to run the app on Expo Go on your phone by scanning the QR code on the Expo Go app on Android or on the Camera app on iOS.</p><p>Once the app run, you'll see the Home screen that you created as it's the default and only route.</p><figure class=\"kg-card kg-image-card\"><img src=\"https://backend.shahednasser.com/content/images/2021/12/IMG_1545.PNG\" class=\"kg-image\" alt loading=\"lazy\" width=\"1170\" height=\"2532\" srcset=\"https://backend.shahednasser.com/content/images/size/w600/2021/12/IMG_1545.PNG 600w, https://backend.shahednasser.com/content/images/size/w1000/2021/12/IMG_1545.PNG 1000w, https://backend.shahednasser.com/content/images/2021/12/IMG_1545.PNG 1170w\" sizes=\"(min-width: 720px) 720px\"></figure><h2 id=\"navigate-to-another-screen\">Navigate to Another Screen</h2><p>In this section, you'll learn how to navigate from one screen to another.</p><p>Screens that are inside the Stack Navigation, like HomeScreen in this app, receive a <code>navigation</code> prop. This prop allows us to navigate to other screens in the app.</p><p>Let's first create the screen you'll navigate to. Create the file <code>screens/BookScreen.js</code> with the following content:</p><pre><code class=\"language-js\">import React from 'react';\nimport { StyleSheet, View } from 'react-native';\nimport { Card } from 'react-native-paper';\nimport { DefaultTheme } from 'react-native-paper';\n\nfunction BookScreen () {\n  return (\n    &lt;View style={styles.container}&gt;\n      &lt;Card style={styles.card}&gt;\n        &lt;Card.Title title=\"This is Books Screen\" /&gt;\n      &lt;/Card&gt;\n    &lt;/View&gt;\n  );\n}\n\nconst styles = StyleSheet.create({\n  container: {\n    flex: 1,\n    flexDirection: 'column',\n    backgroundColor: DefaultTheme.colors.background,\n    alignItems: 'center',\n    paddingTop: 10\n  },\n  card: {\n    width: '90%'\n  }\n});\n\nexport default BookScreen;</code></pre><p>This screen is similar to <code>HomeScreen</code>. It just shows a card with the title \"This is Books Screen\".</p><p>Next, add the new route in <code>App.js</code> for <code>BookScreen</code> below the \"Home\" route:</p><pre><code class=\"language-js\">&lt;Stack.Screen name=\"Book\" component={BookScreen} /&gt;</code></pre><p>Don't forget to import <code>BookScreen</code> below the imports at the beginning of <code>App.js</code>:</p><pre><code class=\"language-js\">import BookScreen from './screens/BookScreen';</code></pre><p>Now, you can navigate to the \"Book\" route inside this stack navigation. To see how it works, you'll add a button in <code>HomeScreen</code> that when the user clicks will navigate to <code>BookScreen</code>.</p><p>In <code>HomeScreen</code>, add the <code>navigation</code> prop to the function:</p><pre><code class=\"language-js\">function HomeScreen ({ navigation })</code></pre><p>Then, change the returned value of the function to the following:</p><pre><code class=\"language-js\">return (\n    &lt;ScrollView style={styles.scrollView}&gt;\n      &lt;Card style={styles.card}&gt;\n        &lt;Card.Title title=\"Navigate to 'Book' Screen\" /&gt;\n        &lt;Card.Content&gt;\n          &lt;Button mode=\"contained\" onPress={() =&gt; navigation.navigate('Book')}&gt;\n            Navigate\n          &lt;/Button&gt;\n        &lt;/Card.Content&gt;\n      &lt;/Card&gt;\n    &lt;/ScrollView&gt;\n  )</code></pre><p>You change the title of the card to \"Navigate to 'Book' Screen\". Then, inside the content of the card, you add a <a href=\"https://callstack.github.io/react-native-paper/button.html\">Button</a> component with <code>onPress</code> listener. This listener will be executed when the user presses on the button.</p><p>Inside the listener, you use the <code>navigation</code> prop to navigate to the <code>Book</code> screen using the method <code>navigate</code>. The method <code>navigate</code> takes the name of the route to navigate to.</p><p>Let's test it out. Run the app again if it isn't running. You should see a new button on the Home screen.</p><figure class=\"kg-card kg-image-card\"><img src=\"https://backend.shahednasser.com/content/images/2021/12/IMG_1543.PNG\" class=\"kg-image\" alt loading=\"lazy\" width=\"1170\" height=\"2532\" srcset=\"https://backend.shahednasser.com/content/images/size/w600/2021/12/IMG_1543.PNG 600w, https://backend.shahednasser.com/content/images/size/w1000/2021/12/IMG_1543.PNG 1000w, https://backend.shahednasser.com/content/images/2021/12/IMG_1543.PNG 1170w\" sizes=\"(min-width: 720px) 720px\"></figure><p>Try clicking on the <code>Navigate</code> button. You'll be navigated to the <code>BookScreen</code>.</p><figure class=\"kg-card kg-image-card\"><img src=\"https://backend.shahednasser.com/content/images/2021/12/IMG_1544.PNG\" class=\"kg-image\" alt loading=\"lazy\" width=\"1170\" height=\"2532\" srcset=\"https://backend.shahednasser.com/content/images/size/w600/2021/12/IMG_1544.PNG 600w, https://backend.shahednasser.com/content/images/size/w1000/2021/12/IMG_1544.PNG 1000w, https://backend.shahednasser.com/content/images/2021/12/IMG_1544.PNG 1170w\" sizes=\"(min-width: 720px) 720px\"></figure><p>Notice how the back button in the navigation bar is added automatically when you navigate to another screen. You can use it to go back to the previous screen, which is in this case the Home screen.</p><h2 id=\"navigate-with-parameters\">Navigate with Parameters</h2><p>In a lot of cases, you'll need to navigate to another screen and pass it parameters. In this section, you'll create an input that allows the user to enter their name, then when they submit you'll open a new screen called <code>NameScreen</code> which will show a greeting to the user with the name they entered.</p><p>Start by adding a new state variable to hold the value of the input you'll add soon in <code>HomeScreen.js</code>:</p><pre><code class=\"language-js\">const [name, setName] = useState('');</code></pre><p>Make sure to add an import for <code>useState</code> at the beginning of the file:</p><pre><code class=\"language-js\">import React, { useState } from 'react';</code></pre><p>Then, add a new Card component below the existing one:</p><pre><code class=\"language-js\">&lt;Card style={styles.card}&gt;\n    &lt;Card.Title title=\"Navigate with Parameters\" /&gt;\n    &lt;Card.Content&gt;\n        &lt;TextInput mode=\"outlined\" label=\"Name\" value={name} onChangeText={(text) =&gt; setName(text)} style={styles.textInput} /&gt;\n            &lt;Button mode=\"contained\" disabled={name.length === 0} onPress={() =&gt; navigation.navigate('Name', { name })}&gt;\n                Navigate\n    \t&lt;/Button&gt;\n    &lt;/Card.Content&gt;\n&lt;/Card&gt;</code></pre><p>This new card has a <a href=\"https://callstack.github.io/react-native-paper/text-input.html\">TextInput</a> with the value <code>name</code>, and on the <code>onChangeText</code> event, which occurs when the user makes changes to the text inside the input, you change the value of the <code>name</code> state variable.</p><p>Below the input, you have a button that is disabled when the <code>name</code> state variable is empty. You also add an <code>onPress</code> event listener which navigates the user to the <code>Name</code> route. </p><p>Notice that you're using the same <code>navigation.navigate</code> method that you used before to navigate to the \"Book\" route, however, this time you pass a second parameter to the method. This method accepts as a second parameter an object of parameters you want to pass. Here, you pass the <code>name</code> variable inside the object.</p><p>One last thing you need to do in the <code>HomeScreen</code> is change the <code>styles</code> object to the following:</p><pre><code class=\"language-js\">const styles = StyleSheet.create({\n  scrollView: {\n    backgroundColor: DefaultTheme.colors.background,\n    paddingTop: 10\n  },\n  card: {\n    width: '90%',\n    marginLeft: 'auto',\n    marginRight: 'auto',\n    marginBottom: 10\n  },\n  textInput: {\n    marginBottom: 10\n  }\n});</code></pre><p>This just makes some changes to the <code>card</code> property and adds a new property <code>textInput</code>.</p><p>You'll now create the <code>NameScreen</code> which will be the component for the \"Name\" route you'll add soon. Create <code>screens/NameScreen.js</code> with the following content:</p><pre><code class=\"language-js\">import React from 'react';\nimport { StyleSheet, View } from 'react-native';\nimport { Card } from 'react-native-paper';\nimport { DefaultTheme } from 'react-native-paper';\n\nfunction NameScreen ({ route }) {\n  const { name } = route.params;\n\n  return (\n    &lt;View style={styles.container}&gt;\n      &lt;Card style={styles.card}&gt;\n        &lt;Card.Title title={`Hello, ${name}`} /&gt;\n      &lt;/Card&gt;\n    &lt;/View&gt;\n  );\n}\n\nconst styles = StyleSheet.create({\n  container: {\n    flex: 1,\n    flexDirection: 'column',\n    backgroundColor: DefaultTheme.colors.background,\n    alignItems: 'center',\n    paddingTop: 10\n  },\n  card: {\n    width: '90%'\n  }\n});\n\nexport default NameScreen;</code></pre><p>This screen is essentially the same as <code>BookScreen</code>, however, notice the <code>route</code> prop passed to the function.</p><p>As mentioned before, the components inside the navigation stack receive a <code>navigation</code> prop to navigate between screens. They also receive a <code>route</code> prop which you can use to get the parameters passed to the screen using <code>route.params</code>.</p><p>So, in <code>NameScreen</code>, you retrieve the value entered in the <code>Name</code> input in the <code>HomeScreen</code>, which is passed when navigating to the <code>NameScreen</code> when the button is clicked using <code>route.params</code>:</p><pre><code class=\"language-js\">const { name } = route.params;</code></pre><p>You then show the user a greeting inside a card using the name parameter.</p><p>One last thing left to do is add the \"Name\" route to the navigation stack. Add a new <code>&lt;Stack.Screen&gt;</code> below the previous ones in <code>App.js</code>:</p><pre><code class=\"language-js\">&lt;Stack.Screen name=\"Name\" component={NameScreen} /&gt;</code></pre><p>And import <code>NameScreen</code> below other imports at the beginning of the file:</p><pre><code class=\"language-js\">import NameScreen from './screens/NameScreen';</code></pre><p>Let's test it out. Run the app if it isn't already running. You'll see that there's a new card on the Home screen with an input and a button.</p><figure class=\"kg-card kg-image-card\"><img src=\"https://backend.shahednasser.com/content/images/2021/12/IMG_1546.PNG\" class=\"kg-image\" alt loading=\"lazy\" width=\"1170\" height=\"2532\" srcset=\"https://backend.shahednasser.com/content/images/size/w600/2021/12/IMG_1546.PNG 600w, https://backend.shahednasser.com/content/images/size/w1000/2021/12/IMG_1546.PNG 1000w, https://backend.shahednasser.com/content/images/2021/12/IMG_1546.PNG 1170w\" sizes=\"(min-width: 720px) 720px\"></figure><p>Try entering your name, then click the \"Navigate\" button below the input. You'll be navigated to the <code>NameScreen</code> and you'll see your name.</p><figure class=\"kg-card kg-image-card\"><img src=\"https://backend.shahednasser.com/content/images/2021/12/IMG_1547.PNG\" class=\"kg-image\" alt loading=\"lazy\" width=\"1170\" height=\"2532\" srcset=\"https://backend.shahednasser.com/content/images/size/w600/2021/12/IMG_1547.PNG 600w, https://backend.shahednasser.com/content/images/size/w1000/2021/12/IMG_1547.PNG 1000w, https://backend.shahednasser.com/content/images/2021/12/IMG_1547.PNG 1170w\" sizes=\"(min-width: 720px) 720px\"></figure><p>You can try going back then changing the value of the name. The name in the <code>NameScreen</code> will change accordingly.</p><h2 id=\"configure-the-header\">Configure the Header</h2><h3 id=\"change-the-title\">Change the title</h3><p>Sometimes, you want the title of a route to be different than the name of the route, or you want the title to be dynamic based on a parameter passed or some other logic.</p><p>In this section, you'll add a new input in <code>HomeScreen</code> that will let you to enter a title, then on clicking the navigate button you'll be navigated to a new screen <code>TitleScreen</code>, whose title will change based on the title entered in the input in HomeScreen.</p><p>Start by adding a new state variable in <code>HomeScreen.js</code> to store the title:</p><pre><code class=\"language-js\">const [title, setTitle] = useState('');</code></pre><p>Then, add a new card below the previous cards:</p><pre><code class=\"language-js\">&lt;Card style={styles.card}&gt;\n        &lt;Card.Title title=\"Navigate to Route with Title\" /&gt;\n        &lt;Card.Content&gt;\n          &lt;TextInput mode=\"outlined\" label=\"Title\" value={title} onChangeText={(text) =&gt; setTitle(text)} style={styles.textInput} /&gt;\n          &lt;Button mode=\"contained\" disabled={title.length === 0} onPress={() =&gt; navigation.navigate('Title', { title })}&gt;\n            Navigate\n          &lt;/Button&gt;\n        &lt;/Card.Content&gt;\n      &lt;/Card&gt;</code></pre><p>This is pretty much the same as the card we added in the previous section. The only difference is that it uses the <code>title</code> state variable, and the listener for <code>onPress</code> for the button navigates to the route \"Title\", passing the <code>title</code> as a parameter.</p><p>Next, create the file <code>screens/Title.js</code> with the following content:</p><pre><code class=\"language-js\">import React from 'react';\nimport { StyleSheet, View } from 'react-native';\nimport { Card } from 'react-native-paper';\nimport { DefaultTheme } from 'react-native-paper';\n\nfunction TitleScreen () {\n  return (\n    &lt;View style={styles.container}&gt;\n      &lt;Card style={styles.card}&gt;\n        &lt;Card.Title title=\"This is title screen\" /&gt;\n      &lt;/Card&gt;\n    &lt;/View&gt;\n  );\n}\n\nconst styles = StyleSheet.create({\n  container: {\n    flex: 1,\n    flexDirection: 'column',\n    backgroundColor: DefaultTheme.colors.background,\n    alignItems: 'center',\n    paddingTop: 10\n  },\n  card: {\n    width: '90%'\n  }\n});\n\nexport default TitleScreen;</code></pre><p>This screen has nothing special. It just shows a card with the title \"This is title screen\". It doesn't even use the title parameter.</p><p>That's because you can change the title of a screen by passing an <code>options</code> parameter to <code>&lt;Stack.Screen&gt;</code> when adding the route.</p><p>In <code>App.js</code>, add an import for <code>TitleScreen</code> below other imports at the beginning of the file:</p><pre><code class=\"language-js\">import TitleScreen from './screens/TitleScreen';</code></pre><p>Then, add the new route below previous <code>&lt;Stack.Screen&gt;</code>:</p><pre><code class=\"language-js\">&lt;Stack.Screen name=\"Title\" component={TitleScreen} options={({route}) =&gt; ({title: route.params.title})} /&gt;</code></pre><p>Every <code>Stack.Screen</code> component can take an <code>options</code> prop. The <code>options</code> prop can be an object like so:</p><pre><code class=\"language-js\">options={{title: \"Hello\"}}</code></pre><p>This is enough if your options are static. However, if the value for any of the options is dynamic, like in this example, you can instead pass it a function that returns an object. This function can receive as a parameter <code>route</code>, which is similar to the <code>route</code> parameter you used in <code>NameScreen</code>:</p><pre><code class=\"language-js\">options={({route}) =&gt; ({title: route.params.title})}</code></pre><p>In this function, you return an object that has a <code>title</code> property with the value <code>route.params.title</code>. So, when the user enters a title in the input and clicks on the Navigate button, the title will be passed to the route, which will change the title of \"Title\" route using the function passed to the <code>options</code> prop.</p><p>Let's test it out. Run the app if it's not running already. You should see a new card on the Home screen with a new input:</p><figure class=\"kg-card kg-image-card\"><img src=\"https://backend.shahednasser.com/content/images/2021/12/IMG_1548.PNG\" class=\"kg-image\" alt loading=\"lazy\" width=\"1170\" height=\"2532\" srcset=\"https://backend.shahednasser.com/content/images/size/w600/2021/12/IMG_1548.PNG 600w, https://backend.shahednasser.com/content/images/size/w1000/2021/12/IMG_1548.PNG 1000w, https://backend.shahednasser.com/content/images/2021/12/IMG_1548.PNG 1170w\" sizes=\"(min-width: 720px) 720px\"></figure><p>Try entering any title you want in the input then click on Navigate. You'll see that you'll be navigated to the <code>TitleScreen</code>, but the title will be the value you entered in the input.</p><figure class=\"kg-card kg-image-card\"><img src=\"https://backend.shahednasser.com/content/images/2021/12/IMG_1549.PNG\" class=\"kg-image\" alt loading=\"lazy\" width=\"1170\" height=\"2532\" srcset=\"https://backend.shahednasser.com/content/images/size/w600/2021/12/IMG_1549.PNG 600w, https://backend.shahednasser.com/content/images/size/w1000/2021/12/IMG_1549.PNG 1000w, https://backend.shahednasser.com/content/images/2021/12/IMG_1549.PNG 1170w\" sizes=\"(min-width: 720px) 720px\"></figure><p>You can try going back then changing the value entered in the input. The title of the screen will change accordingly.</p><h3 id=\"add-header-button\">Add Header Button</h3><p>Another option you can add is header buttons. You can customize what your header shows on the left and right by using the <code>headerRight</code> and <code>headerLeft</code> properties in the <code>options</code> param of <code>&lt;Stack.Screen&gt;</code>.</p><p>In this section, you'll add a button to the right of the header of the Home screen which will show a user an alert when they click on it.</p><p>In <code>App.js</code>, add to the \"Home\" route a prop <code>option</code>:</p><pre><code class=\"language-js\">&lt;Stack.Screen name=\"Home\" component={HomeScreen} options={{\n            headerRight: () =&gt; (\n              &lt;IconButton icon=\"alert-outline\" onPress={() =&gt; alert('You\\'re awesome!')} color={DefaultTheme.colors.notification} /&gt;\n            )\n}} /&gt;</code></pre><p>In the <code>options</code> prop, you pass an object with the property <code>headerRight</code>. The <code>headerRight</code> property can be a component that you can render elements inside. You render an <a href=\"https://callstack.github.io/react-native-paper/icon-button.html\">IconButton</a> component, which is a button that only has an icon without text. In the <code>onPress</code> event listener, you show an alert to the user.</p><p>That's all. Run the app if it isn't running already. You'll see a new icon at the top right of the header in Home Screen.</p><figure class=\"kg-card kg-image-card\"><img src=\"https://backend.shahednasser.com/content/images/2021/12/IMG_1550.PNG\" class=\"kg-image\" alt loading=\"lazy\" width=\"1170\" height=\"2532\" srcset=\"https://backend.shahednasser.com/content/images/size/w600/2021/12/IMG_1550.PNG 600w, https://backend.shahednasser.com/content/images/size/w1000/2021/12/IMG_1550.PNG 1000w, https://backend.shahednasser.com/content/images/2021/12/IMG_1550.PNG 1170w\" sizes=\"(min-width: 720px) 720px\"></figure><p>Click on it and you'll see an alert.</p><figure class=\"kg-card kg-image-card\"><img src=\"https://backend.shahednasser.com/content/images/2021/12/IMG_1551.PNG\" class=\"kg-image\" alt loading=\"lazy\" width=\"1170\" height=\"2532\" srcset=\"https://backend.shahednasser.com/content/images/size/w600/2021/12/IMG_1551.PNG 600w, https://backend.shahednasser.com/content/images/size/w1000/2021/12/IMG_1551.PNG 1000w, https://backend.shahednasser.com/content/images/2021/12/IMG_1551.PNG 1170w\" sizes=\"(min-width: 720px) 720px\"></figure><h2 id=\"navigation-events\">Navigation Events</h2><p>In React Navigation, you can subscribe to two events: <code>focus</code> and <code>blur</code>. The <code>focus</code> event will occur every time the screen is opened and is currently the main screen. The <code>blur</code> event will occur whenever a screen was in focus but navigation occurs that makes the screen not the current main screen. Simply put, the <code>focus</code> event is when the screen is visible to the user, and the <code>blur</code> event occurs when a screen was visible to a user but isn't anymore. </p><p>In this section, you'll add a counter that will count how many times you navigated to other screens from the home screen. So, this counter will increment in the <code>blur</code> event.</p><p>To subscribe to events, in a <code>useEffect</code> callback inside a component, you can use <code>navigation.addListener</code> passing it the name of the event as a first parameter, and the event handler as a second parameter. The <code>addListener</code> method returns an unsubscribe function which you should return in the <code>useEffect</code> callback.</p><p>In <code>HomeScreen.js</code>, add an import for <code>useEffect</code> at the beginning of the file:</p><pre><code class=\"language-js\">import React, { useEffect, useState } from 'react';</code></pre><p>Then, add a new state variable <code>counter</code> inside the <code>HomeScreen</code> component:</p><pre><code class=\"language-js\">const [counter, setCounter] = useState(0);</code></pre><p>After that add the <code>useEffect</code> callback that will register the event handle for the <code>blur</code> event:</p><pre><code class=\"language-js\">useEffect(() =&gt; {\n    return navigation.addListener('blur', () =&gt; {\n      setCounter(counter + 1);\n    });\n  });</code></pre><p>As you can see, you use <code>navigation.addListener</code> to add an event listener to the <code>blur</code> event. The function passed as a second parameter is the handler of the event. It will be executed whenever the screen is out of focus (not the screen visible to the user). Inside the event handler, you increment the counter by 1.</p><p>Finally, you'll show the counter at the top of the screen. In <code>ScrollView</code>, add a new card before the previously added cards:</p><pre><code class=\"language-js\">&lt;Card style={styles.card}&gt;\n\t&lt;Card.Title title={`Navigation Count: ${counter}`} /&gt;\n&lt;/Card&gt;</code></pre><p>Let's test it out. Run the app if it's not already running. You should see a card with a count of navigation at the top of the screen.</p><h2 id=\"go-back\">Go Back</h2><p>The last thing you'll learn in this tutorial is how to go back to the previous screen. As you've seen throughout the tutorial, when you navigate to a screen you'll see a back button in the header which you can use to go back to the previous screen.</p><p>Although this is sufficient in most cases, sometimes you want to go back to the previous screen programmatically. You'll create in this section a new screen <code>BackScreen</code> which you can go to from the <code>HomeScreen</code>. This screen will have a back button in its content and when you click on it you can go back to the previous screen, which is the <code>HomeScreen</code>.</p><p>Create the file <code>screens/BackScreen.js</code> with the following content:</p><pre><code class=\"language-js\">import React from 'react';\nimport { StyleSheet, View } from 'react-native';\nimport { Button, Card } from 'react-native-paper';\nimport { DefaultTheme } from 'react-native-paper';\n\nfunction BackScreen ({ navigation }) {\n  return (\n    &lt;View style={styles.container}&gt;\n      &lt;Card style={styles.card}&gt;\n        &lt;Card.Title title=\"This is Back Screen\" /&gt;\n        &lt;Card.Content&gt;\n          &lt;Button mode=\"contained\" onPress={() =&gt; navigation.goBack()}&gt;\n            Go Back\n          &lt;/Button&gt;\n        &lt;/Card.Content&gt;\n      &lt;/Card&gt;\n    &lt;/View&gt;\n  );\n}\n\nconst styles = StyleSheet.create({\n  container: {\n    flex: 1,\n    flexDirection: 'column',\n    backgroundColor: DefaultTheme.colors.background,\n    alignItems: 'center',\n    paddingTop: 10\n  },\n  card: {\n    width: '90%'\n  }\n});\n\nexport default BackScreen;</code></pre><p>This screen is pretty much the same as other screens. The things you should note is that, first, the screen has uses the <code>navigation</code> prop that is passed to components inside the navigation stack. Second, the screen has a button inside a card which, on press, will use the <code>navigation</code> prop to go back using the <code>goBack</code> method:</p><pre><code class=\"language-js\">&lt;Button mode=\"contained\" onPress={() =&gt; navigation.goBack()}&gt;</code></pre><p>Now, go to the <code>HomeScreen</code> and add a new card at the end of the ScrollView:</p><pre><code class=\"language-js\">&lt;Card style={styles.card}&gt;\n    &lt;Card.Title title=\"Navigate to 'Back' Screen\" /&gt;\n    &lt;Card.Content&gt;\n        &lt;Button mode=\"contained\" onPress={() =&gt; navigation.navigate('Back')}&gt;\n            Navigate\n    \t&lt;/Button&gt;\n    &lt;/Card.Content&gt;\n&lt;/Card&gt;</code></pre><p>This card just shows a button to navigate to the route \"Back\". Let's add that route now.</p><p>In <code>App.js</code> add a new <code>&lt;Stack.Screen&gt;</code> component below the previously added ones:</p><pre><code class=\"language-js\">&lt;Stack.Screen name=\"Back\" component={BackScreen} /&gt;</code></pre><p>It's ready now. Run the app if it isn't already running. You'll see a new card at the end of the list.</p><figure class=\"kg-card kg-image-card\"><img src=\"https://backend.shahednasser.com/content/images/2021/12/IMG_1554.PNG\" class=\"kg-image\" alt loading=\"lazy\" width=\"1170\" height=\"2532\" srcset=\"https://backend.shahednasser.com/content/images/size/w600/2021/12/IMG_1554.PNG 600w, https://backend.shahednasser.com/content/images/size/w1000/2021/12/IMG_1554.PNG 1000w, https://backend.shahednasser.com/content/images/2021/12/IMG_1554.PNG 1170w\" sizes=\"(min-width: 720px) 720px\"></figure><p>Click on \"Navigate\" on the last card. You'll be navigated to the <code>BackScreen</code>.</p><figure class=\"kg-card kg-image-card\"><img src=\"https://backend.shahednasser.com/content/images/2021/12/IMG_1555.PNG\" class=\"kg-image\" alt loading=\"lazy\" width=\"1170\" height=\"2532\" srcset=\"https://backend.shahednasser.com/content/images/size/w600/2021/12/IMG_1555.PNG 600w, https://backend.shahednasser.com/content/images/size/w1000/2021/12/IMG_1555.PNG 1000w, https://backend.shahednasser.com/content/images/2021/12/IMG_1555.PNG 1170w\" sizes=\"(min-width: 720px) 720px\"></figure><p>Click on the \"Go Back\" button in the card. You'll be taken back to the <code>HomeScreen</code>.</p><h2 id=\"conclusion\">Conclusion</h2><p>In this tutorial, you learned how to use React Navigation to navigate between screens, pass a parameter from one screen to another, configure the header of a screen, add navigation event listeners, and go back programmatically to the previous screen.</p><p>Although this covers most use cases for using React Navigation, be sure to check out <a href=\"https://reactnavigation.org/docs/getting-started\">their documentation</a> for more details on how to use it. </p>","url":"https://backend.shahednasser.com/react-native-navigation-tutorial/","canonical_url":null,"uuid":"c0afd92e-2e0d-4a81-b5c5-d41dd2553877","codeinjection_foot":null,"codeinjection_head":null,"codeinjection_styles":null,"comment_id":"61b1cb7ad752820612f17bad","reading_time":15,"send_email_when_published":null,"email_subject":null,"childHtmlRehype":{"html":"<p><a href=\"https://reactnative.dev\">React Native</a> is one of the most popular cross-platform app development frameworks. Using JavaScript, you can develop native apps for both Android and iOS.</p><p>One important part of creating apps is being able to navigate between screens. In this tutorial, you'll learn how to use <a href=\"https://reactnavigation.org\">React Navigation</a> in your app to add navigation. This will include basic navigation from one screen to another, going back to the previous screen, passing parameters, listening to events, and more.</p><p>This tutorial will not explain the basics of creating a React Native app. If you're interested in learning about that, you can check out my other tutorial <a href=\"https://blog.shahednasser.com/react-native-tutorial-create-your-first-app\">React Native Tutorial: Create Your First App</a>.</p><p>You can find the code for this tutorial on <a href=\"https://github.com/shahednasser/react-navigation-tutorial\">this GitHub repository</a>.</p><h2 id=\"prerequisites\">Prerequisites </h2><p>Make sure you have both <a href=\"https://nodejs.org/en/\">Node.js</a> and NPM installed on your machine. You can check if they're installed with the following command:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\"><span class=\"token function\">node</span> -v\n<span class=\"token function\">npm</span> -v</code></pre></div><p>If they're not installed, you can <a href=\"https://nodejs.org/en/\">install Node.js</a> and NPM will be installed automatically with it.</p><p> After that, make sure you install <a href=\"https://expo.dev\">Expo</a>'s CLI if you don't have it installed:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\"><span class=\"token function\">npm</span> <span class=\"token function\">install</span> -g expo-cli</code></pre></div><p>If you're not familiar with what Expo is, Expo provides a set of tools built around React. Most importantly, it allows you to write and build your React Native app in minutes.</p><p>You should also install <a href=\"https://expo.dev/client\">Expo Go</a> on your device to be able to test the app as we go through the tutorial.</p><h2 id=\"project-setup\">Project Setup</h2><p>Start by creating a new React Native project using Expo's CLI:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\">expo init react-navigation-tutorial —npm</code></pre></div><p>You'll be then prompted to choose the type of app, choose \"blank\".</p><p>After that the <code class=\"language-text\">init</code> command will install the dependencies you need for your React Native project.</p><p>Once it's done, change to the directory of the project:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\"><span class=\"token builtin class-name\">cd</span> react-navigation-tutorial</code></pre></div><p>Now, you'll install the dependencies required to use React Navigation. First, install the building blocks libraries using Expo's CLI with the following command:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\">expo <span class=\"token function\">install</span> react-native-screens react-native-safe-area-context</code></pre></div><p>Second, install the native stack navigator library:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\"><span class=\"token function\">npm</span> <span class=\"token function\">install</span> @react-navigation/native-stack</code></pre></div><p>Now, you have all the libraries required to start using React Navigation.</p><p>Finally, install <a href=\"https://callstack.github.io/react-native-paper/index.html\">React Native Paper</a> to have beautifully-styled components in your app:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\"><span class=\"token function\">npm</span> <span class=\"token function\">install</span> react-native-paper</code></pre></div><h2 id=\"set-up-react-navigation\">Set Up React Navigation</h2><p>Before you can start adding routes to your app, you need to create a Stack Navigator. This stack navigator will be used to create screens and navigate between them.</p><p>In <code class=\"language-text\">App.js</code>, change the content to the following:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token keyword\">import</span> React <span class=\"token keyword\">from</span> <span class=\"token string\">'react'</span><span class=\"token punctuation\">;</span>\n<span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> NavigationContainer <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'@react-navigation/native'</span><span class=\"token punctuation\">;</span>\n<span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> createNativeStackNavigator <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'@react-navigation/native-stack'</span><span class=\"token punctuation\">;</span>\n<span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> Provider <span class=\"token keyword\">as</span> PaperProvider <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'react-native-paper'</span><span class=\"token punctuation\">;</span>\n<span class=\"token keyword\">import</span> HomeScreen <span class=\"token keyword\">from</span> <span class=\"token string\">'./screens/HomeScreen'</span><span class=\"token punctuation\">;</span>\n\n<span class=\"token keyword\">const</span> Stack <span class=\"token operator\">=</span> <span class=\"token function\">createNativeStackNavigator</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\n\n<span class=\"token keyword\">export</span> <span class=\"token keyword\">default</span> <span class=\"token keyword\">function</span> <span class=\"token function\">App</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n  <span class=\"token keyword\">return</span> <span class=\"token punctuation\">(</span>\n    <span class=\"token operator\">&#x3C;</span>PaperProvider<span class=\"token operator\">></span>\n      <span class=\"token operator\">&#x3C;</span>NavigationContainer<span class=\"token operator\">></span>\n        <span class=\"token operator\">&#x3C;</span>Stack<span class=\"token punctuation\">.</span>Navigator<span class=\"token operator\">></span>\n          <span class=\"token operator\">&#x3C;</span>Stack<span class=\"token punctuation\">.</span>Screen name<span class=\"token operator\">=</span><span class=\"token string\">\"Home\"</span> component<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span>HomeScreen<span class=\"token punctuation\">}</span> <span class=\"token operator\">/</span><span class=\"token operator\">></span>\n        <span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>Stack<span class=\"token punctuation\">.</span>Navigator<span class=\"token operator\">></span>\n    <span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>NavigationContainer<span class=\"token operator\">></span>\n    <span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>PaperProvider<span class=\"token operator\">></span>\n  <span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span>\n</code></pre></div><p>Notice that in the returned value of <code class=\"language-text\">App</code>, you first have <code class=\"language-text\">&#x3C;PaperProvider></code> at the top of the elements. This is just used to make sure all screens share the same theme.</p><p>Next, you have the <code class=\"language-text\">&#x3C;NavigationContainer></code>. This component manages the navigation tree and contains the navigation state. So, it should wrap Stack navigators.</p><p>Inside <code class=\"language-text\">&#x3C;NavigationContainer></code> you have <code class=\"language-text\">&#x3C;Stack.Navigator></code>. <code class=\"language-text\">Stack</code> is created using <code class=\"language-text\">createNativeStackNavigator</code>. This function returns an object containing 2 properties: <code class=\"language-text\">Screen</code> and <code class=\"language-text\">Navigator</code>. The <code class=\"language-text\">Navigator</code> property is a React element and it should be used to wrap the <code class=\"language-text\">Screen</code> elements to manage routing configurations.</p><p>So, inside <code class=\"language-text\">&#x3C;Stack.Navigator></code> you'll have one or more <code class=\"language-text\">&#x3C;Stack.Screen></code> components. For each route, you'll have a <code class=\"language-text\">&#x3C;Stack.Screen></code> component. Now, you just have one component that points to the <code class=\"language-text\">Home</code> route.</p><p>Notice that a <code class=\"language-text\">Screen</code> component takes the <code class=\"language-text\">name</code> prop, which is the name of the route and will be referenced later on when navigating to this route, and the <code class=\"language-text\">component</code> property which is the component to render for this route.</p><p>As you can see, you're passing <code class=\"language-text\">HomeScreen</code> component to the <code class=\"language-text\">Home</code> route, so you need to create it now.</p><p>Create the directory <code class=\"language-text\">screens</code>, and inside that directory create a new file <code class=\"language-text\">HomeScreen.js</code> with the following content:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token keyword\">import</span> React <span class=\"token keyword\">from</span> <span class=\"token string\">'react'</span><span class=\"token punctuation\">;</span>\n<span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> ScrollView<span class=\"token punctuation\">,</span> StyleSheet <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'react-native'</span><span class=\"token punctuation\">;</span>\n<span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> Button<span class=\"token punctuation\">,</span> Card <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'react-native-paper'</span><span class=\"token punctuation\">;</span>\n<span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> DefaultTheme <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'react-native-paper'</span><span class=\"token punctuation\">;</span>\n\n<span class=\"token keyword\">function</span> <span class=\"token function\">HomeScreen</span> <span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n  <span class=\"token keyword\">return</span> <span class=\"token punctuation\">(</span>\n    <span class=\"token operator\">&#x3C;</span>ScrollView style<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span>styles<span class=\"token punctuation\">.</span>scrollView<span class=\"token punctuation\">}</span><span class=\"token operator\">></span>\n      <span class=\"token operator\">&#x3C;</span>Card style<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span>styles<span class=\"token punctuation\">.</span>card<span class=\"token punctuation\">}</span><span class=\"token operator\">></span>\n        <span class=\"token operator\">&#x3C;</span>Card<span class=\"token punctuation\">.</span>Title title<span class=\"token operator\">=</span><span class=\"token string\">\"Home Screen\"</span> <span class=\"token operator\">/</span><span class=\"token operator\">></span>\n      <span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>Card<span class=\"token operator\">></span>\n    <span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>ScrollView<span class=\"token operator\">></span>\n  <span class=\"token punctuation\">)</span>\n<span class=\"token punctuation\">}</span>\n\n<span class=\"token keyword\">const</span> styles <span class=\"token operator\">=</span> StyleSheet<span class=\"token punctuation\">.</span><span class=\"token function\">create</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span>\n  <span class=\"token literal-property property\">scrollView</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token literal-property property\">backgroundColor</span><span class=\"token operator\">:</span> DefaultTheme<span class=\"token punctuation\">.</span>colors<span class=\"token punctuation\">.</span>background<span class=\"token punctuation\">,</span>\n    <span class=\"token literal-property property\">paddingTop</span><span class=\"token operator\">:</span> <span class=\"token number\">10</span>\n  <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\n  <span class=\"token literal-property property\">card</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token literal-property property\">width</span><span class=\"token operator\">:</span> <span class=\"token string\">'90%'</span><span class=\"token punctuation\">,</span>\n    <span class=\"token literal-property property\">marginLeft</span><span class=\"token operator\">:</span> <span class=\"token string\">'auto'</span><span class=\"token punctuation\">,</span>\n    <span class=\"token literal-property property\">marginRight</span><span class=\"token operator\">:</span> <span class=\"token string\">'auto'</span>\n  <span class=\"token punctuation\">}</span>\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n\n<span class=\"token keyword\">export</span> <span class=\"token keyword\">default</span> HomeScreen</code></pre></div><p>This will create a basic home screen with a <a href=\"https://reactnative.dev/docs/scrollview\">ScrollView</a> component containing a <a href=\"https://callstack.github.io/react-native-paper/card.html\">Card</a> component with the title \"Home Screen\".</p><p>You just created your first route with React Navigation! Let's test it out.</p><p>In your terminal run to start your app:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\"><span class=\"token function\">npm</span> start</code></pre></div><p>Then, you'll be able to run the app on Expo Go on your phone by scanning the QR code on the Expo Go app on Android or on the Camera app on iOS.</p><p>Once the app run, you'll see the Home screen that you created as it's the default and only route.</p><figure class=\"kg-card kg-image-card\"><img src=\"https://backend.shahednasser.com/content/images/2021/12/IMG_1545.PNG\" class=\"kg-image\" alt=\"\" loading=\"lazy\" width=\"1170\" height=\"2532\" srcset=\"https://backend.shahednasser.com/content/images/size/w600/2021/12/IMG_1545.PNG 600w, https://backend.shahednasser.com/content/images/size/w1000/2021/12/IMG_1545.PNG 1000w, https://backend.shahednasser.com/content/images/2021/12/IMG_1545.PNG 1170w\" sizes=\"(min-width: 720px) 720px\"></figure><h2 id=\"navigate-to-another-screen\">Navigate to Another Screen</h2><p>In this section, you'll learn how to navigate from one screen to another.</p><p>Screens that are inside the Stack Navigation, like HomeScreen in this app, receive a <code class=\"language-text\">navigation</code> prop. This prop allows us to navigate to other screens in the app.</p><p>Let's first create the screen you'll navigate to. Create the file <code class=\"language-text\">screens/BookScreen.js</code> with the following content:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token keyword\">import</span> React <span class=\"token keyword\">from</span> <span class=\"token string\">'react'</span><span class=\"token punctuation\">;</span>\n<span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> StyleSheet<span class=\"token punctuation\">,</span> View <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'react-native'</span><span class=\"token punctuation\">;</span>\n<span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> Card <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'react-native-paper'</span><span class=\"token punctuation\">;</span>\n<span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> DefaultTheme <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'react-native-paper'</span><span class=\"token punctuation\">;</span>\n\n<span class=\"token keyword\">function</span> <span class=\"token function\">BookScreen</span> <span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n  <span class=\"token keyword\">return</span> <span class=\"token punctuation\">(</span>\n    <span class=\"token operator\">&#x3C;</span>View style<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span>styles<span class=\"token punctuation\">.</span>container<span class=\"token punctuation\">}</span><span class=\"token operator\">></span>\n      <span class=\"token operator\">&#x3C;</span>Card style<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span>styles<span class=\"token punctuation\">.</span>card<span class=\"token punctuation\">}</span><span class=\"token operator\">></span>\n        <span class=\"token operator\">&#x3C;</span>Card<span class=\"token punctuation\">.</span>Title title<span class=\"token operator\">=</span><span class=\"token string\">\"This is Books Screen\"</span> <span class=\"token operator\">/</span><span class=\"token operator\">></span>\n      <span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>Card<span class=\"token operator\">></span>\n    <span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>View<span class=\"token operator\">></span>\n  <span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span>\n\n<span class=\"token keyword\">const</span> styles <span class=\"token operator\">=</span> StyleSheet<span class=\"token punctuation\">.</span><span class=\"token function\">create</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span>\n  <span class=\"token literal-property property\">container</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token literal-property property\">flex</span><span class=\"token operator\">:</span> <span class=\"token number\">1</span><span class=\"token punctuation\">,</span>\n    <span class=\"token literal-property property\">flexDirection</span><span class=\"token operator\">:</span> <span class=\"token string\">'column'</span><span class=\"token punctuation\">,</span>\n    <span class=\"token literal-property property\">backgroundColor</span><span class=\"token operator\">:</span> DefaultTheme<span class=\"token punctuation\">.</span>colors<span class=\"token punctuation\">.</span>background<span class=\"token punctuation\">,</span>\n    <span class=\"token literal-property property\">alignItems</span><span class=\"token operator\">:</span> <span class=\"token string\">'center'</span><span class=\"token punctuation\">,</span>\n    <span class=\"token literal-property property\">paddingTop</span><span class=\"token operator\">:</span> <span class=\"token number\">10</span>\n  <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\n  <span class=\"token literal-property property\">card</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token literal-property property\">width</span><span class=\"token operator\">:</span> <span class=\"token string\">'90%'</span>\n  <span class=\"token punctuation\">}</span>\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n\n<span class=\"token keyword\">export</span> <span class=\"token keyword\">default</span> BookScreen<span class=\"token punctuation\">;</span></code></pre></div><p>This screen is similar to <code class=\"language-text\">HomeScreen</code>. It just shows a card with the title \"This is Books Screen\".</p><p>Next, add the new route in <code class=\"language-text\">App.js</code> for <code class=\"language-text\">BookScreen</code> below the \"Home\" route:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token operator\">&#x3C;</span>Stack<span class=\"token punctuation\">.</span>Screen name<span class=\"token operator\">=</span><span class=\"token string\">\"Book\"</span> component<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span>BookScreen<span class=\"token punctuation\">}</span> <span class=\"token operator\">/</span><span class=\"token operator\">></span></code></pre></div><p>Don't forget to import <code class=\"language-text\">BookScreen</code> below the imports at the beginning of <code class=\"language-text\">App.js</code>:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token keyword\">import</span> BookScreen <span class=\"token keyword\">from</span> <span class=\"token string\">'./screens/BookScreen'</span><span class=\"token punctuation\">;</span></code></pre></div><p>Now, you can navigate to the \"Book\" route inside this stack navigation. To see how it works, you'll add a button in <code class=\"language-text\">HomeScreen</code> that when the user clicks will navigate to <code class=\"language-text\">BookScreen</code>.</p><p>In <code class=\"language-text\">HomeScreen</code>, add the <code class=\"language-text\">navigation</code> prop to the function:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token keyword\">function</span> <span class=\"token function\">HomeScreen</span> <span class=\"token punctuation\">(</span><span class=\"token parameter\"><span class=\"token punctuation\">{</span> navigation <span class=\"token punctuation\">}</span></span><span class=\"token punctuation\">)</span></code></pre></div><p>Then, change the returned value of the function to the following:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token keyword\">return</span> <span class=\"token punctuation\">(</span>\n    <span class=\"token operator\">&#x3C;</span>ScrollView style<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span>styles<span class=\"token punctuation\">.</span>scrollView<span class=\"token punctuation\">}</span><span class=\"token operator\">></span>\n      <span class=\"token operator\">&#x3C;</span>Card style<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span>styles<span class=\"token punctuation\">.</span>card<span class=\"token punctuation\">}</span><span class=\"token operator\">></span>\n        <span class=\"token operator\">&#x3C;</span>Card<span class=\"token punctuation\">.</span>Title title<span class=\"token operator\">=</span><span class=\"token string\">\"Navigate to 'Book' Screen\"</span> <span class=\"token operator\">/</span><span class=\"token operator\">></span>\n        <span class=\"token operator\">&#x3C;</span>Card<span class=\"token punctuation\">.</span>Content<span class=\"token operator\">></span>\n          <span class=\"token operator\">&#x3C;</span>Button mode<span class=\"token operator\">=</span><span class=\"token string\">\"contained\"</span> onPress<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> navigation<span class=\"token punctuation\">.</span><span class=\"token function\">navigate</span><span class=\"token punctuation\">(</span><span class=\"token string\">'Book'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">}</span><span class=\"token operator\">></span>\n            Navigate\n          <span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>Button<span class=\"token operator\">></span>\n        <span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>Card<span class=\"token punctuation\">.</span>Content<span class=\"token operator\">></span>\n      <span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>Card<span class=\"token operator\">></span>\n    <span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>ScrollView<span class=\"token operator\">></span>\n  <span class=\"token punctuation\">)</span></code></pre></div><p>You change the title of the card to \"Navigate to 'Book' Screen\". Then, inside the content of the card, you add a <a href=\"https://callstack.github.io/react-native-paper/button.html\">Button</a> component with <code class=\"language-text\">onPress</code> listener. This listener will be executed when the user presses on the button.</p><p>Inside the listener, you use the <code class=\"language-text\">navigation</code> prop to navigate to the <code class=\"language-text\">Book</code> screen using the method <code class=\"language-text\">navigate</code>. The method <code class=\"language-text\">navigate</code> takes the name of the route to navigate to.</p><p>Let's test it out. Run the app again if it isn't running. You should see a new button on the Home screen.</p><figure class=\"kg-card kg-image-card\"><img src=\"https://backend.shahednasser.com/content/images/2021/12/IMG_1543.PNG\" class=\"kg-image\" alt=\"\" loading=\"lazy\" width=\"1170\" height=\"2532\" srcset=\"https://backend.shahednasser.com/content/images/size/w600/2021/12/IMG_1543.PNG 600w, https://backend.shahednasser.com/content/images/size/w1000/2021/12/IMG_1543.PNG 1000w, https://backend.shahednasser.com/content/images/2021/12/IMG_1543.PNG 1170w\" sizes=\"(min-width: 720px) 720px\"></figure><p>Try clicking on the <code class=\"language-text\">Navigate</code> button. You'll be navigated to the <code class=\"language-text\">BookScreen</code>.</p><figure class=\"kg-card kg-image-card\"><img src=\"https://backend.shahednasser.com/content/images/2021/12/IMG_1544.PNG\" class=\"kg-image\" alt=\"\" loading=\"lazy\" width=\"1170\" height=\"2532\" srcset=\"https://backend.shahednasser.com/content/images/size/w600/2021/12/IMG_1544.PNG 600w, https://backend.shahednasser.com/content/images/size/w1000/2021/12/IMG_1544.PNG 1000w, https://backend.shahednasser.com/content/images/2021/12/IMG_1544.PNG 1170w\" sizes=\"(min-width: 720px) 720px\"></figure><p>Notice how the back button in the navigation bar is added automatically when you navigate to another screen. You can use it to go back to the previous screen, which is in this case the Home screen.</p><h2 id=\"navigate-with-parameters\">Navigate with Parameters</h2><p>In a lot of cases, you'll need to navigate to another screen and pass it parameters. In this section, you'll create an input that allows the user to enter their name, then when they submit you'll open a new screen called <code class=\"language-text\">NameScreen</code> which will show a greeting to the user with the name they entered.</p><p>Start by adding a new state variable to hold the value of the input you'll add soon in <code class=\"language-text\">HomeScreen.js</code>:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token keyword\">const</span> <span class=\"token punctuation\">[</span>name<span class=\"token punctuation\">,</span> setName<span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token function\">useState</span><span class=\"token punctuation\">(</span><span class=\"token string\">''</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code></pre></div><p>Make sure to add an import for <code class=\"language-text\">useState</code> at the beginning of the file:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token keyword\">import</span> React<span class=\"token punctuation\">,</span> <span class=\"token punctuation\">{</span> useState <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'react'</span><span class=\"token punctuation\">;</span></code></pre></div><p>Then, add a new Card component below the existing one:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token operator\">&#x3C;</span>Card style<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span>styles<span class=\"token punctuation\">.</span>card<span class=\"token punctuation\">}</span><span class=\"token operator\">></span>\n    <span class=\"token operator\">&#x3C;</span>Card<span class=\"token punctuation\">.</span>Title title<span class=\"token operator\">=</span><span class=\"token string\">\"Navigate with Parameters\"</span> <span class=\"token operator\">/</span><span class=\"token operator\">></span>\n    <span class=\"token operator\">&#x3C;</span>Card<span class=\"token punctuation\">.</span>Content<span class=\"token operator\">></span>\n        <span class=\"token operator\">&#x3C;</span>TextInput mode<span class=\"token operator\">=</span><span class=\"token string\">\"outlined\"</span> label<span class=\"token operator\">=</span><span class=\"token string\">\"Name\"</span> value<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span>name<span class=\"token punctuation\">}</span> onChangeText<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">text</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token function\">setName</span><span class=\"token punctuation\">(</span>text<span class=\"token punctuation\">)</span><span class=\"token punctuation\">}</span> style<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span>styles<span class=\"token punctuation\">.</span>textInput<span class=\"token punctuation\">}</span> <span class=\"token operator\">/</span><span class=\"token operator\">></span>\n            <span class=\"token operator\">&#x3C;</span>Button mode<span class=\"token operator\">=</span><span class=\"token string\">\"contained\"</span> disabled<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span>name<span class=\"token punctuation\">.</span>length <span class=\"token operator\">===</span> <span class=\"token number\">0</span><span class=\"token punctuation\">}</span> onPress<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> navigation<span class=\"token punctuation\">.</span><span class=\"token function\">navigate</span><span class=\"token punctuation\">(</span><span class=\"token string\">'Name'</span><span class=\"token punctuation\">,</span> <span class=\"token punctuation\">{</span> name <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">}</span><span class=\"token operator\">></span>\n                Navigate\n    \t<span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>Button<span class=\"token operator\">></span>\n    <span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>Card<span class=\"token punctuation\">.</span>Content<span class=\"token operator\">></span>\n<span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>Card<span class=\"token operator\">></span></code></pre></div><p>This new card has a <a href=\"https://callstack.github.io/react-native-paper/text-input.html\">TextInput</a> with the value <code class=\"language-text\">name</code>, and on the <code class=\"language-text\">onChangeText</code> event, which occurs when the user makes changes to the text inside the input, you change the value of the <code class=\"language-text\">name</code> state variable.</p><p>Below the input, you have a button that is disabled when the <code class=\"language-text\">name</code> state variable is empty. You also add an <code class=\"language-text\">onPress</code> event listener which navigates the user to the <code class=\"language-text\">Name</code> route. </p><p>Notice that you're using the same <code class=\"language-text\">navigation.navigate</code> method that you used before to navigate to the \"Book\" route, however, this time you pass a second parameter to the method. This method accepts as a second parameter an object of parameters you want to pass. Here, you pass the <code class=\"language-text\">name</code> variable inside the object.</p><p>One last thing you need to do in the <code class=\"language-text\">HomeScreen</code> is change the <code class=\"language-text\">styles</code> object to the following:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token keyword\">const</span> styles <span class=\"token operator\">=</span> StyleSheet<span class=\"token punctuation\">.</span><span class=\"token function\">create</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span>\n  <span class=\"token literal-property property\">scrollView</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token literal-property property\">backgroundColor</span><span class=\"token operator\">:</span> DefaultTheme<span class=\"token punctuation\">.</span>colors<span class=\"token punctuation\">.</span>background<span class=\"token punctuation\">,</span>\n    <span class=\"token literal-property property\">paddingTop</span><span class=\"token operator\">:</span> <span class=\"token number\">10</span>\n  <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\n  <span class=\"token literal-property property\">card</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token literal-property property\">width</span><span class=\"token operator\">:</span> <span class=\"token string\">'90%'</span><span class=\"token punctuation\">,</span>\n    <span class=\"token literal-property property\">marginLeft</span><span class=\"token operator\">:</span> <span class=\"token string\">'auto'</span><span class=\"token punctuation\">,</span>\n    <span class=\"token literal-property property\">marginRight</span><span class=\"token operator\">:</span> <span class=\"token string\">'auto'</span><span class=\"token punctuation\">,</span>\n    <span class=\"token literal-property property\">marginBottom</span><span class=\"token operator\">:</span> <span class=\"token number\">10</span>\n  <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\n  <span class=\"token literal-property property\">textInput</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token literal-property property\">marginBottom</span><span class=\"token operator\">:</span> <span class=\"token number\">10</span>\n  <span class=\"token punctuation\">}</span>\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code></pre></div><p>This just makes some changes to the <code class=\"language-text\">card</code> property and adds a new property <code class=\"language-text\">textInput</code>.</p><p>You'll now create the <code class=\"language-text\">NameScreen</code> which will be the component for the \"Name\" route you'll add soon. Create <code class=\"language-text\">screens/NameScreen.js</code> with the following content:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token keyword\">import</span> React <span class=\"token keyword\">from</span> <span class=\"token string\">'react'</span><span class=\"token punctuation\">;</span>\n<span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> StyleSheet<span class=\"token punctuation\">,</span> View <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'react-native'</span><span class=\"token punctuation\">;</span>\n<span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> Card <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'react-native-paper'</span><span class=\"token punctuation\">;</span>\n<span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> DefaultTheme <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'react-native-paper'</span><span class=\"token punctuation\">;</span>\n\n<span class=\"token keyword\">function</span> <span class=\"token function\">NameScreen</span> <span class=\"token punctuation\">(</span><span class=\"token parameter\"><span class=\"token punctuation\">{</span> route <span class=\"token punctuation\">}</span></span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n  <span class=\"token keyword\">const</span> <span class=\"token punctuation\">{</span> name <span class=\"token punctuation\">}</span> <span class=\"token operator\">=</span> route<span class=\"token punctuation\">.</span>params<span class=\"token punctuation\">;</span>\n\n  <span class=\"token keyword\">return</span> <span class=\"token punctuation\">(</span>\n    <span class=\"token operator\">&#x3C;</span>View style<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span>styles<span class=\"token punctuation\">.</span>container<span class=\"token punctuation\">}</span><span class=\"token operator\">></span>\n      <span class=\"token operator\">&#x3C;</span>Card style<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span>styles<span class=\"token punctuation\">.</span>card<span class=\"token punctuation\">}</span><span class=\"token operator\">></span>\n        <span class=\"token operator\">&#x3C;</span>Card<span class=\"token punctuation\">.</span>Title title<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span><span class=\"token template-string\"><span class=\"token template-punctuation string\">`</span><span class=\"token string\">Hello, </span><span class=\"token interpolation\"><span class=\"token interpolation-punctuation punctuation\">${</span>name<span class=\"token interpolation-punctuation punctuation\">}</span></span><span class=\"token template-punctuation string\">`</span></span><span class=\"token punctuation\">}</span> <span class=\"token operator\">/</span><span class=\"token operator\">></span>\n      <span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>Card<span class=\"token operator\">></span>\n    <span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>View<span class=\"token operator\">></span>\n  <span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span>\n\n<span class=\"token keyword\">const</span> styles <span class=\"token operator\">=</span> StyleSheet<span class=\"token punctuation\">.</span><span class=\"token function\">create</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span>\n  <span class=\"token literal-property property\">container</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token literal-property property\">flex</span><span class=\"token operator\">:</span> <span class=\"token number\">1</span><span class=\"token punctuation\">,</span>\n    <span class=\"token literal-property property\">flexDirection</span><span class=\"token operator\">:</span> <span class=\"token string\">'column'</span><span class=\"token punctuation\">,</span>\n    <span class=\"token literal-property property\">backgroundColor</span><span class=\"token operator\">:</span> DefaultTheme<span class=\"token punctuation\">.</span>colors<span class=\"token punctuation\">.</span>background<span class=\"token punctuation\">,</span>\n    <span class=\"token literal-property property\">alignItems</span><span class=\"token operator\">:</span> <span class=\"token string\">'center'</span><span class=\"token punctuation\">,</span>\n    <span class=\"token literal-property property\">paddingTop</span><span class=\"token operator\">:</span> <span class=\"token number\">10</span>\n  <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\n  <span class=\"token literal-property property\">card</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token literal-property property\">width</span><span class=\"token operator\">:</span> <span class=\"token string\">'90%'</span>\n  <span class=\"token punctuation\">}</span>\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n\n<span class=\"token keyword\">export</span> <span class=\"token keyword\">default</span> NameScreen<span class=\"token punctuation\">;</span></code></pre></div><p>This screen is essentially the same as <code class=\"language-text\">BookScreen</code>, however, notice the <code class=\"language-text\">route</code> prop passed to the function.</p><p>As mentioned before, the components inside the navigation stack receive a <code class=\"language-text\">navigation</code> prop to navigate between screens. They also receive a <code class=\"language-text\">route</code> prop which you can use to get the parameters passed to the screen using <code class=\"language-text\">route.params</code>.</p><p>So, in <code class=\"language-text\">NameScreen</code>, you retrieve the value entered in the <code class=\"language-text\">Name</code> input in the <code class=\"language-text\">HomeScreen</code>, which is passed when navigating to the <code class=\"language-text\">NameScreen</code> when the button is clicked using <code class=\"language-text\">route.params</code>:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token keyword\">const</span> <span class=\"token punctuation\">{</span> name <span class=\"token punctuation\">}</span> <span class=\"token operator\">=</span> route<span class=\"token punctuation\">.</span>params<span class=\"token punctuation\">;</span></code></pre></div><p>You then show the user a greeting inside a card using the name parameter.</p><p>One last thing left to do is add the \"Name\" route to the navigation stack. Add a new <code class=\"language-text\">&#x3C;Stack.Screen></code> below the previous ones in <code class=\"language-text\">App.js</code>:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token operator\">&#x3C;</span>Stack<span class=\"token punctuation\">.</span>Screen name<span class=\"token operator\">=</span><span class=\"token string\">\"Name\"</span> component<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span>NameScreen<span class=\"token punctuation\">}</span> <span class=\"token operator\">/</span><span class=\"token operator\">></span></code></pre></div><p>And import <code class=\"language-text\">NameScreen</code> below other imports at the beginning of the file:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token keyword\">import</span> NameScreen <span class=\"token keyword\">from</span> <span class=\"token string\">'./screens/NameScreen'</span><span class=\"token punctuation\">;</span></code></pre></div><p>Let's test it out. Run the app if it isn't already running. You'll see that there's a new card on the Home screen with an input and a button.</p><figure class=\"kg-card kg-image-card\"><img src=\"https://backend.shahednasser.com/content/images/2021/12/IMG_1546.PNG\" class=\"kg-image\" alt=\"\" loading=\"lazy\" width=\"1170\" height=\"2532\" srcset=\"https://backend.shahednasser.com/content/images/size/w600/2021/12/IMG_1546.PNG 600w, https://backend.shahednasser.com/content/images/size/w1000/2021/12/IMG_1546.PNG 1000w, https://backend.shahednasser.com/content/images/2021/12/IMG_1546.PNG 1170w\" sizes=\"(min-width: 720px) 720px\"></figure><p>Try entering your name, then click the \"Navigate\" button below the input. You'll be navigated to the <code class=\"language-text\">NameScreen</code> and you'll see your name.</p><figure class=\"kg-card kg-image-card\"><img src=\"https://backend.shahednasser.com/content/images/2021/12/IMG_1547.PNG\" class=\"kg-image\" alt=\"\" loading=\"lazy\" width=\"1170\" height=\"2532\" srcset=\"https://backend.shahednasser.com/content/images/size/w600/2021/12/IMG_1547.PNG 600w, https://backend.shahednasser.com/content/images/size/w1000/2021/12/IMG_1547.PNG 1000w, https://backend.shahednasser.com/content/images/2021/12/IMG_1547.PNG 1170w\" sizes=\"(min-width: 720px) 720px\"></figure><p>You can try going back then changing the value of the name. The name in the <code class=\"language-text\">NameScreen</code> will change accordingly.</p><h2 id=\"configure-the-header\">Configure the Header</h2><h3 id=\"change-the-title\">Change the title</h3><p>Sometimes, you want the title of a route to be different than the name of the route, or you want the title to be dynamic based on a parameter passed or some other logic.</p><p>In this section, you'll add a new input in <code class=\"language-text\">HomeScreen</code> that will let you to enter a title, then on clicking the navigate button you'll be navigated to a new screen <code class=\"language-text\">TitleScreen</code>, whose title will change based on the title entered in the input in HomeScreen.</p><p>Start by adding a new state variable in <code class=\"language-text\">HomeScreen.js</code> to store the title:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token keyword\">const</span> <span class=\"token punctuation\">[</span>title<span class=\"token punctuation\">,</span> setTitle<span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token function\">useState</span><span class=\"token punctuation\">(</span><span class=\"token string\">''</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code></pre></div><p>Then, add a new card below the previous cards:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token operator\">&#x3C;</span>Card style<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span>styles<span class=\"token punctuation\">.</span>card<span class=\"token punctuation\">}</span><span class=\"token operator\">></span>\n        <span class=\"token operator\">&#x3C;</span>Card<span class=\"token punctuation\">.</span>Title title<span class=\"token operator\">=</span><span class=\"token string\">\"Navigate to Route with Title\"</span> <span class=\"token operator\">/</span><span class=\"token operator\">></span>\n        <span class=\"token operator\">&#x3C;</span>Card<span class=\"token punctuation\">.</span>Content<span class=\"token operator\">></span>\n          <span class=\"token operator\">&#x3C;</span>TextInput mode<span class=\"token operator\">=</span><span class=\"token string\">\"outlined\"</span> label<span class=\"token operator\">=</span><span class=\"token string\">\"Title\"</span> value<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span>title<span class=\"token punctuation\">}</span> onChangeText<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">text</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token function\">setTitle</span><span class=\"token punctuation\">(</span>text<span class=\"token punctuation\">)</span><span class=\"token punctuation\">}</span> style<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span>styles<span class=\"token punctuation\">.</span>textInput<span class=\"token punctuation\">}</span> <span class=\"token operator\">/</span><span class=\"token operator\">></span>\n          <span class=\"token operator\">&#x3C;</span>Button mode<span class=\"token operator\">=</span><span class=\"token string\">\"contained\"</span> disabled<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span>title<span class=\"token punctuation\">.</span>length <span class=\"token operator\">===</span> <span class=\"token number\">0</span><span class=\"token punctuation\">}</span> onPress<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> navigation<span class=\"token punctuation\">.</span><span class=\"token function\">navigate</span><span class=\"token punctuation\">(</span><span class=\"token string\">'Title'</span><span class=\"token punctuation\">,</span> <span class=\"token punctuation\">{</span> title <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">}</span><span class=\"token operator\">></span>\n            Navigate\n          <span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>Button<span class=\"token operator\">></span>\n        <span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>Card<span class=\"token punctuation\">.</span>Content<span class=\"token operator\">></span>\n      <span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>Card<span class=\"token operator\">></span></code></pre></div><p>This is pretty much the same as the card we added in the previous section. The only difference is that it uses the <code class=\"language-text\">title</code> state variable, and the listener for <code class=\"language-text\">onPress</code> for the button navigates to the route \"Title\", passing the <code class=\"language-text\">title</code> as a parameter.</p><p>Next, create the file <code class=\"language-text\">screens/Title.js</code> with the following content:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token keyword\">import</span> React <span class=\"token keyword\">from</span> <span class=\"token string\">'react'</span><span class=\"token punctuation\">;</span>\n<span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> StyleSheet<span class=\"token punctuation\">,</span> View <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'react-native'</span><span class=\"token punctuation\">;</span>\n<span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> Card <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'react-native-paper'</span><span class=\"token punctuation\">;</span>\n<span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> DefaultTheme <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'react-native-paper'</span><span class=\"token punctuation\">;</span>\n\n<span class=\"token keyword\">function</span> <span class=\"token function\">TitleScreen</span> <span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n  <span class=\"token keyword\">return</span> <span class=\"token punctuation\">(</span>\n    <span class=\"token operator\">&#x3C;</span>View style<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span>styles<span class=\"token punctuation\">.</span>container<span class=\"token punctuation\">}</span><span class=\"token operator\">></span>\n      <span class=\"token operator\">&#x3C;</span>Card style<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span>styles<span class=\"token punctuation\">.</span>card<span class=\"token punctuation\">}</span><span class=\"token operator\">></span>\n        <span class=\"token operator\">&#x3C;</span>Card<span class=\"token punctuation\">.</span>Title title<span class=\"token operator\">=</span><span class=\"token string\">\"This is title screen\"</span> <span class=\"token operator\">/</span><span class=\"token operator\">></span>\n      <span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>Card<span class=\"token operator\">></span>\n    <span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>View<span class=\"token operator\">></span>\n  <span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span>\n\n<span class=\"token keyword\">const</span> styles <span class=\"token operator\">=</span> StyleSheet<span class=\"token punctuation\">.</span><span class=\"token function\">create</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span>\n  <span class=\"token literal-property property\">container</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token literal-property property\">flex</span><span class=\"token operator\">:</span> <span class=\"token number\">1</span><span class=\"token punctuation\">,</span>\n    <span class=\"token literal-property property\">flexDirection</span><span class=\"token operator\">:</span> <span class=\"token string\">'column'</span><span class=\"token punctuation\">,</span>\n    <span class=\"token literal-property property\">backgroundColor</span><span class=\"token operator\">:</span> DefaultTheme<span class=\"token punctuation\">.</span>colors<span class=\"token punctuation\">.</span>background<span class=\"token punctuation\">,</span>\n    <span class=\"token literal-property property\">alignItems</span><span class=\"token operator\">:</span> <span class=\"token string\">'center'</span><span class=\"token punctuation\">,</span>\n    <span class=\"token literal-property property\">paddingTop</span><span class=\"token operator\">:</span> <span class=\"token number\">10</span>\n  <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\n  <span class=\"token literal-property property\">card</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token literal-property property\">width</span><span class=\"token operator\">:</span> <span class=\"token string\">'90%'</span>\n  <span class=\"token punctuation\">}</span>\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n\n<span class=\"token keyword\">export</span> <span class=\"token keyword\">default</span> TitleScreen<span class=\"token punctuation\">;</span></code></pre></div><p>This screen has nothing special. It just shows a card with the title \"This is title screen\". It doesn't even use the title parameter.</p><p>That's because you can change the title of a screen by passing an <code class=\"language-text\">options</code> parameter to <code class=\"language-text\">&#x3C;Stack.Screen></code> when adding the route.</p><p>In <code class=\"language-text\">App.js</code>, add an import for <code class=\"language-text\">TitleScreen</code> below other imports at the beginning of the file:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token keyword\">import</span> TitleScreen <span class=\"token keyword\">from</span> <span class=\"token string\">'./screens/TitleScreen'</span><span class=\"token punctuation\">;</span></code></pre></div><p>Then, add the new route below previous <code class=\"language-text\">&#x3C;Stack.Screen></code>:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token operator\">&#x3C;</span>Stack<span class=\"token punctuation\">.</span>Screen name<span class=\"token operator\">=</span><span class=\"token string\">\"Title\"</span> component<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span>TitleScreen<span class=\"token punctuation\">}</span> options<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span><span class=\"token punctuation\">(</span><span class=\"token parameter\"><span class=\"token punctuation\">{</span>route<span class=\"token punctuation\">}</span></span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span><span class=\"token literal-property property\">title</span><span class=\"token operator\">:</span> route<span class=\"token punctuation\">.</span>params<span class=\"token punctuation\">.</span>title<span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">}</span> <span class=\"token operator\">/</span><span class=\"token operator\">></span></code></pre></div><p>Every <code class=\"language-text\">Stack.Screen</code> component can take an <code class=\"language-text\">options</code> prop. The <code class=\"language-text\">options</code> prop can be an object like so:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\">options<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span><span class=\"token punctuation\">{</span><span class=\"token literal-property property\">title</span><span class=\"token operator\">:</span> <span class=\"token string\">\"Hello\"</span><span class=\"token punctuation\">}</span><span class=\"token punctuation\">}</span></code></pre></div><p>This is enough if your options are static. However, if the value for any of the options is dynamic, like in this example, you can instead pass it a function that returns an object. This function can receive as a parameter <code class=\"language-text\">route</code>, which is similar to the <code class=\"language-text\">route</code> parameter you used in <code class=\"language-text\">NameScreen</code>:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\">options<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span><span class=\"token punctuation\">(</span><span class=\"token parameter\"><span class=\"token punctuation\">{</span>route<span class=\"token punctuation\">}</span></span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span><span class=\"token literal-property property\">title</span><span class=\"token operator\">:</span> route<span class=\"token punctuation\">.</span>params<span class=\"token punctuation\">.</span>title<span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">}</span></code></pre></div><p>In this function, you return an object that has a <code class=\"language-text\">title</code> property with the value <code class=\"language-text\">route.params.title</code>. So, when the user enters a title in the input and clicks on the Navigate button, the title will be passed to the route, which will change the title of \"Title\" route using the function passed to the <code class=\"language-text\">options</code> prop.</p><p>Let's test it out. Run the app if it's not running already. You should see a new card on the Home screen with a new input:</p><figure class=\"kg-card kg-image-card\"><img src=\"https://backend.shahednasser.com/content/images/2021/12/IMG_1548.PNG\" class=\"kg-image\" alt=\"\" loading=\"lazy\" width=\"1170\" height=\"2532\" srcset=\"https://backend.shahednasser.com/content/images/size/w600/2021/12/IMG_1548.PNG 600w, https://backend.shahednasser.com/content/images/size/w1000/2021/12/IMG_1548.PNG 1000w, https://backend.shahednasser.com/content/images/2021/12/IMG_1548.PNG 1170w\" sizes=\"(min-width: 720px) 720px\"></figure><p>Try entering any title you want in the input then click on Navigate. You'll see that you'll be navigated to the <code class=\"language-text\">TitleScreen</code>, but the title will be the value you entered in the input.</p><figure class=\"kg-card kg-image-card\"><img src=\"https://backend.shahednasser.com/content/images/2021/12/IMG_1549.PNG\" class=\"kg-image\" alt=\"\" loading=\"lazy\" width=\"1170\" height=\"2532\" srcset=\"https://backend.shahednasser.com/content/images/size/w600/2021/12/IMG_1549.PNG 600w, https://backend.shahednasser.com/content/images/size/w1000/2021/12/IMG_1549.PNG 1000w, https://backend.shahednasser.com/content/images/2021/12/IMG_1549.PNG 1170w\" sizes=\"(min-width: 720px) 720px\"></figure><p>You can try going back then changing the value entered in the input. The title of the screen will change accordingly.</p><h3 id=\"add-header-button\">Add Header Button</h3><p>Another option you can add is header buttons. You can customize what your header shows on the left and right by using the <code class=\"language-text\">headerRight</code> and <code class=\"language-text\">headerLeft</code> properties in the <code class=\"language-text\">options</code> param of <code class=\"language-text\">&#x3C;Stack.Screen></code>.</p><p>In this section, you'll add a button to the right of the header of the Home screen which will show a user an alert when they click on it.</p><p>In <code class=\"language-text\">App.js</code>, add to the \"Home\" route a prop <code class=\"language-text\">option</code>:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token operator\">&#x3C;</span>Stack<span class=\"token punctuation\">.</span>Screen name<span class=\"token operator\">=</span><span class=\"token string\">\"Home\"</span> component<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span>HomeScreen<span class=\"token punctuation\">}</span> options<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span><span class=\"token punctuation\">{</span>\n            <span class=\"token function-variable function\">headerRight</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">(</span>\n              <span class=\"token operator\">&#x3C;</span>IconButton icon<span class=\"token operator\">=</span><span class=\"token string\">\"alert-outline\"</span> onPress<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token function\">alert</span><span class=\"token punctuation\">(</span><span class=\"token string\">'You\\'re awesome!'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">}</span> color<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span>DefaultTheme<span class=\"token punctuation\">.</span>colors<span class=\"token punctuation\">.</span>notification<span class=\"token punctuation\">}</span> <span class=\"token operator\">/</span><span class=\"token operator\">></span>\n            <span class=\"token punctuation\">)</span>\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">}</span> <span class=\"token operator\">/</span><span class=\"token operator\">></span></code></pre></div><p>In the <code class=\"language-text\">options</code> prop, you pass an object with the property <code class=\"language-text\">headerRight</code>. The <code class=\"language-text\">headerRight</code> property can be a component that you can render elements inside. You render an <a href=\"https://callstack.github.io/react-native-paper/icon-button.html\">IconButton</a> component, which is a button that only has an icon without text. In the <code class=\"language-text\">onPress</code> event listener, you show an alert to the user.</p><p>That's all. Run the app if it isn't running already. You'll see a new icon at the top right of the header in Home Screen.</p><figure class=\"kg-card kg-image-card\"><img src=\"https://backend.shahednasser.com/content/images/2021/12/IMG_1550.PNG\" class=\"kg-image\" alt=\"\" loading=\"lazy\" width=\"1170\" height=\"2532\" srcset=\"https://backend.shahednasser.com/content/images/size/w600/2021/12/IMG_1550.PNG 600w, https://backend.shahednasser.com/content/images/size/w1000/2021/12/IMG_1550.PNG 1000w, https://backend.shahednasser.com/content/images/2021/12/IMG_1550.PNG 1170w\" sizes=\"(min-width: 720px) 720px\"></figure><p>Click on it and you'll see an alert.</p><figure class=\"kg-card kg-image-card\"><img src=\"https://backend.shahednasser.com/content/images/2021/12/IMG_1551.PNG\" class=\"kg-image\" alt=\"\" loading=\"lazy\" width=\"1170\" height=\"2532\" srcset=\"https://backend.shahednasser.com/content/images/size/w600/2021/12/IMG_1551.PNG 600w, https://backend.shahednasser.com/content/images/size/w1000/2021/12/IMG_1551.PNG 1000w, https://backend.shahednasser.com/content/images/2021/12/IMG_1551.PNG 1170w\" sizes=\"(min-width: 720px) 720px\"></figure><h2 id=\"navigation-events\">Navigation Events</h2><p>In React Navigation, you can subscribe to two events: <code class=\"language-text\">focus</code> and <code class=\"language-text\">blur</code>. The <code class=\"language-text\">focus</code> event will occur every time the screen is opened and is currently the main screen. The <code class=\"language-text\">blur</code> event will occur whenever a screen was in focus but navigation occurs that makes the screen not the current main screen. Simply put, the <code class=\"language-text\">focus</code> event is when the screen is visible to the user, and the <code class=\"language-text\">blur</code> event occurs when a screen was visible to a user but isn't anymore. </p><p>In this section, you'll add a counter that will count how many times you navigated to other screens from the home screen. So, this counter will increment in the <code class=\"language-text\">blur</code> event.</p><p>To subscribe to events, in a <code class=\"language-text\">useEffect</code> callback inside a component, you can use <code class=\"language-text\">navigation.addListener</code> passing it the name of the event as a first parameter, and the event handler as a second parameter. The <code class=\"language-text\">addListener</code> method returns an unsubscribe function which you should return in the <code class=\"language-text\">useEffect</code> callback.</p><p>In <code class=\"language-text\">HomeScreen.js</code>, add an import for <code class=\"language-text\">useEffect</code> at the beginning of the file:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token keyword\">import</span> React<span class=\"token punctuation\">,</span> <span class=\"token punctuation\">{</span> useEffect<span class=\"token punctuation\">,</span> useState <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'react'</span><span class=\"token punctuation\">;</span></code></pre></div><p>Then, add a new state variable <code class=\"language-text\">counter</code> inside the <code class=\"language-text\">HomeScreen</code> component:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token keyword\">const</span> <span class=\"token punctuation\">[</span>counter<span class=\"token punctuation\">,</span> setCounter<span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token function\">useState</span><span class=\"token punctuation\">(</span><span class=\"token number\">0</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code></pre></div><p>After that add the <code class=\"language-text\">useEffect</code> callback that will register the event handle for the <code class=\"language-text\">blur</code> event:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token function\">useEffect</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\n    <span class=\"token keyword\">return</span> navigation<span class=\"token punctuation\">.</span><span class=\"token function\">addListener</span><span class=\"token punctuation\">(</span><span class=\"token string\">'blur'</span><span class=\"token punctuation\">,</span> <span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\n      <span class=\"token function\">setCounter</span><span class=\"token punctuation\">(</span>counter <span class=\"token operator\">+</span> <span class=\"token number\">1</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n    <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n  <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code></pre></div><p>As you can see, you use <code class=\"language-text\">navigation.addListener</code> to add an event listener to the <code class=\"language-text\">blur</code> event. The function passed as a second parameter is the handler of the event. It will be executed whenever the screen is out of focus (not the screen visible to the user). Inside the event handler, you increment the counter by 1.</p><p>Finally, you'll show the counter at the top of the screen. In <code class=\"language-text\">ScrollView</code>, add a new card before the previously added cards:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token operator\">&#x3C;</span>Card style<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span>styles<span class=\"token punctuation\">.</span>card<span class=\"token punctuation\">}</span><span class=\"token operator\">></span>\n\t<span class=\"token operator\">&#x3C;</span>Card<span class=\"token punctuation\">.</span>Title title<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span><span class=\"token template-string\"><span class=\"token template-punctuation string\">`</span><span class=\"token string\">Navigation Count: </span><span class=\"token interpolation\"><span class=\"token interpolation-punctuation punctuation\">${</span>counter<span class=\"token interpolation-punctuation punctuation\">}</span></span><span class=\"token template-punctuation string\">`</span></span><span class=\"token punctuation\">}</span> <span class=\"token operator\">/</span><span class=\"token operator\">></span>\n<span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>Card<span class=\"token operator\">></span></code></pre></div><p>Let's test it out. Run the app if it's not already running. You should see a card with a count of navigation at the top of the screen.</p><h2 id=\"go-back\">Go Back</h2><p>The last thing you'll learn in this tutorial is how to go back to the previous screen. As you've seen throughout the tutorial, when you navigate to a screen you'll see a back button in the header which you can use to go back to the previous screen.</p><p>Although this is sufficient in most cases, sometimes you want to go back to the previous screen programmatically. You'll create in this section a new screen <code class=\"language-text\">BackScreen</code> which you can go to from the <code class=\"language-text\">HomeScreen</code>. This screen will have a back button in its content and when you click on it you can go back to the previous screen, which is the <code class=\"language-text\">HomeScreen</code>.</p><p>Create the file <code class=\"language-text\">screens/BackScreen.js</code> with the following content:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token keyword\">import</span> React <span class=\"token keyword\">from</span> <span class=\"token string\">'react'</span><span class=\"token punctuation\">;</span>\n<span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> StyleSheet<span class=\"token punctuation\">,</span> View <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'react-native'</span><span class=\"token punctuation\">;</span>\n<span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> Button<span class=\"token punctuation\">,</span> Card <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'react-native-paper'</span><span class=\"token punctuation\">;</span>\n<span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> DefaultTheme <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'react-native-paper'</span><span class=\"token punctuation\">;</span>\n\n<span class=\"token keyword\">function</span> <span class=\"token function\">BackScreen</span> <span class=\"token punctuation\">(</span><span class=\"token parameter\"><span class=\"token punctuation\">{</span> navigation <span class=\"token punctuation\">}</span></span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n  <span class=\"token keyword\">return</span> <span class=\"token punctuation\">(</span>\n    <span class=\"token operator\">&#x3C;</span>View style<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span>styles<span class=\"token punctuation\">.</span>container<span class=\"token punctuation\">}</span><span class=\"token operator\">></span>\n      <span class=\"token operator\">&#x3C;</span>Card style<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span>styles<span class=\"token punctuation\">.</span>card<span class=\"token punctuation\">}</span><span class=\"token operator\">></span>\n        <span class=\"token operator\">&#x3C;</span>Card<span class=\"token punctuation\">.</span>Title title<span class=\"token operator\">=</span><span class=\"token string\">\"This is Back Screen\"</span> <span class=\"token operator\">/</span><span class=\"token operator\">></span>\n        <span class=\"token operator\">&#x3C;</span>Card<span class=\"token punctuation\">.</span>Content<span class=\"token operator\">></span>\n          <span class=\"token operator\">&#x3C;</span>Button mode<span class=\"token operator\">=</span><span class=\"token string\">\"contained\"</span> onPress<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> navigation<span class=\"token punctuation\">.</span><span class=\"token function\">goBack</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">}</span><span class=\"token operator\">></span>\n            Go Back\n          <span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>Button<span class=\"token operator\">></span>\n        <span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>Card<span class=\"token punctuation\">.</span>Content<span class=\"token operator\">></span>\n      <span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>Card<span class=\"token operator\">></span>\n    <span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>View<span class=\"token operator\">></span>\n  <span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span>\n\n<span class=\"token keyword\">const</span> styles <span class=\"token operator\">=</span> StyleSheet<span class=\"token punctuation\">.</span><span class=\"token function\">create</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span>\n  <span class=\"token literal-property property\">container</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token literal-property property\">flex</span><span class=\"token operator\">:</span> <span class=\"token number\">1</span><span class=\"token punctuation\">,</span>\n    <span class=\"token literal-property property\">flexDirection</span><span class=\"token operator\">:</span> <span class=\"token string\">'column'</span><span class=\"token punctuation\">,</span>\n    <span class=\"token literal-property property\">backgroundColor</span><span class=\"token operator\">:</span> DefaultTheme<span class=\"token punctuation\">.</span>colors<span class=\"token punctuation\">.</span>background<span class=\"token punctuation\">,</span>\n    <span class=\"token literal-property property\">alignItems</span><span class=\"token operator\">:</span> <span class=\"token string\">'center'</span><span class=\"token punctuation\">,</span>\n    <span class=\"token literal-property property\">paddingTop</span><span class=\"token operator\">:</span> <span class=\"token number\">10</span>\n  <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\n  <span class=\"token literal-property property\">card</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token literal-property property\">width</span><span class=\"token operator\">:</span> <span class=\"token string\">'90%'</span>\n  <span class=\"token punctuation\">}</span>\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n\n<span class=\"token keyword\">export</span> <span class=\"token keyword\">default</span> BackScreen<span class=\"token punctuation\">;</span></code></pre></div><p>This screen is pretty much the same as other screens. The things you should note is that, first, the screen has uses the <code class=\"language-text\">navigation</code> prop that is passed to components inside the navigation stack. Second, the screen has a button inside a card which, on press, will use the <code class=\"language-text\">navigation</code> prop to go back using the <code class=\"language-text\">goBack</code> method:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token operator\">&#x3C;</span>Button mode<span class=\"token operator\">=</span><span class=\"token string\">\"contained\"</span> onPress<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> navigation<span class=\"token punctuation\">.</span><span class=\"token function\">goBack</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">}</span><span class=\"token operator\">></span></code></pre></div><p>Now, go to the <code class=\"language-text\">HomeScreen</code> and add a new card at the end of the ScrollView:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token operator\">&#x3C;</span>Card style<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span>styles<span class=\"token punctuation\">.</span>card<span class=\"token punctuation\">}</span><span class=\"token operator\">></span>\n    <span class=\"token operator\">&#x3C;</span>Card<span class=\"token punctuation\">.</span>Title title<span class=\"token operator\">=</span><span class=\"token string\">\"Navigate to 'Back' Screen\"</span> <span class=\"token operator\">/</span><span class=\"token operator\">></span>\n    <span class=\"token operator\">&#x3C;</span>Card<span class=\"token punctuation\">.</span>Content<span class=\"token operator\">></span>\n        <span class=\"token operator\">&#x3C;</span>Button mode<span class=\"token operator\">=</span><span class=\"token string\">\"contained\"</span> onPress<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> navigation<span class=\"token punctuation\">.</span><span class=\"token function\">navigate</span><span class=\"token punctuation\">(</span><span class=\"token string\">'Back'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">}</span><span class=\"token operator\">></span>\n            Navigate\n    \t<span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>Button<span class=\"token operator\">></span>\n    <span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>Card<span class=\"token punctuation\">.</span>Content<span class=\"token operator\">></span>\n<span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>Card<span class=\"token operator\">></span></code></pre></div><p>This card just shows a button to navigate to the route \"Back\". Let's add that route now.</p><p>In <code class=\"language-text\">App.js</code> add a new <code class=\"language-text\">&#x3C;Stack.Screen></code> component below the previously added ones:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token operator\">&#x3C;</span>Stack<span class=\"token punctuation\">.</span>Screen name<span class=\"token operator\">=</span><span class=\"token string\">\"Back\"</span> component<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span>BackScreen<span class=\"token punctuation\">}</span> <span class=\"token operator\">/</span><span class=\"token operator\">></span></code></pre></div><p>It's ready now. Run the app if it isn't already running. You'll see a new card at the end of the list.</p><figure class=\"kg-card kg-image-card\"><img src=\"https://backend.shahednasser.com/content/images/2021/12/IMG_1554.PNG\" class=\"kg-image\" alt=\"\" loading=\"lazy\" width=\"1170\" height=\"2532\" srcset=\"https://backend.shahednasser.com/content/images/size/w600/2021/12/IMG_1554.PNG 600w, https://backend.shahednasser.com/content/images/size/w1000/2021/12/IMG_1554.PNG 1000w, https://backend.shahednasser.com/content/images/2021/12/IMG_1554.PNG 1170w\" sizes=\"(min-width: 720px) 720px\"></figure><p>Click on \"Navigate\" on the last card. You'll be navigated to the <code class=\"language-text\">BackScreen</code>.</p><figure class=\"kg-card kg-image-card\"><img src=\"https://backend.shahednasser.com/content/images/2021/12/IMG_1555.PNG\" class=\"kg-image\" alt=\"\" loading=\"lazy\" width=\"1170\" height=\"2532\" srcset=\"https://backend.shahednasser.com/content/images/size/w600/2021/12/IMG_1555.PNG 600w, https://backend.shahednasser.com/content/images/size/w1000/2021/12/IMG_1555.PNG 1000w, https://backend.shahednasser.com/content/images/2021/12/IMG_1555.PNG 1170w\" sizes=\"(min-width: 720px) 720px\"></figure><p>Click on the \"Go Back\" button in the card. You'll be taken back to the <code class=\"language-text\">HomeScreen</code>.</p><h2 id=\"conclusion\">Conclusion</h2><p>In this tutorial, you learned how to use React Navigation to navigate between screens, pass a parameter from one screen to another, configure the header of a screen, add navigation event listeners, and go back programmatically to the previous screen.</p><p>Although this covers most use cases for using React Navigation, be sure to check out <a href=\"https://reactnavigation.org/docs/getting-started\">their documentation</a> for more details on how to use it. </p>","htmlAst":{"type":"root","children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"https://reactnative.dev"},"children":[{"type":"text","value":"React Native"}]},{"type":"text","value":" is one of the most popular cross-platform app development frameworks. Using JavaScript, you can develop native apps for both Android and iOS."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"One important part of creating apps is being able to navigate between screens. In this tutorial, you'll learn how to use "},{"type":"element","tagName":"a","properties":{"href":"https://reactnavigation.org"},"children":[{"type":"text","value":"React Navigation"}]},{"type":"text","value":" in your app to add navigation. This will include basic navigation from one screen to another, going back to the previous screen, passing parameters, listening to events, and more."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This tutorial will not explain the basics of creating a React Native app. If you're interested in learning about that, you can check out my other tutorial "},{"type":"element","tagName":"a","properties":{"href":"https://blog.shahednasser.com/react-native-tutorial-create-your-first-app"},"children":[{"type":"text","value":"React Native Tutorial: Create Your First App"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You can find the code for this tutorial on "},{"type":"element","tagName":"a","properties":{"href":"https://github.com/shahednasser/react-navigation-tutorial"},"children":[{"type":"text","value":"this GitHub repository"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"h2","properties":{"id":"prerequisites"},"children":[{"type":"text","value":"Prerequisites "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Make sure you have both "},{"type":"element","tagName":"a","properties":{"href":"https://nodejs.org/en/"},"children":[{"type":"text","value":"Node.js"}]},{"type":"text","value":" and NPM installed on your machine. You can check if they're installed with the following command:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"bash"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-bash"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-bash"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"node"}]},{"type":"text","value":" -v\n"},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"npm"}]},{"type":"text","value":" -v"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If they're not installed, you can "},{"type":"element","tagName":"a","properties":{"href":"https://nodejs.org/en/"},"children":[{"type":"text","value":"install Node.js"}]},{"type":"text","value":" and NPM will be installed automatically with it."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":" After that, make sure you install "},{"type":"element","tagName":"a","properties":{"href":"https://expo.dev"},"children":[{"type":"text","value":"Expo"}]},{"type":"text","value":"'s CLI if you don't have it installed:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"bash"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-bash"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-bash"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"npm"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"install"}]},{"type":"text","value":" -g expo-cli"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If you're not familiar with what Expo is, Expo provides a set of tools built around React. Most importantly, it allows you to write and build your React Native app in minutes."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You should also install "},{"type":"element","tagName":"a","properties":{"href":"https://expo.dev/client"},"children":[{"type":"text","value":"Expo Go"}]},{"type":"text","value":" on your device to be able to test the app as we go through the tutorial."}]},{"type":"element","tagName":"h2","properties":{"id":"project-setup"},"children":[{"type":"text","value":"Project Setup"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Start by creating a new React Native project using Expo's CLI:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"bash"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-bash"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-bash"]},"children":[{"type":"text","value":"expo init react-navigation-tutorial —npm"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You'll be then prompted to choose the type of app, choose \"blank\"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"After that the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"init"}]},{"type":"text","value":" command will install the dependencies you need for your React Native project."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Once it's done, change to the directory of the project:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"bash"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-bash"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-bash"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","builtin","class-name"]},"children":[{"type":"text","value":"cd"}]},{"type":"text","value":" react-navigation-tutorial"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Now, you'll install the dependencies required to use React Navigation. First, install the building blocks libraries using Expo's CLI with the following command:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"bash"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-bash"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-bash"]},"children":[{"type":"text","value":"expo "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"install"}]},{"type":"text","value":" react-native-screens react-native-safe-area-context"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Second, install the native stack navigator library:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"bash"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-bash"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-bash"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"npm"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"install"}]},{"type":"text","value":" @react-navigation/native-stack"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Now, you have all the libraries required to start using React Navigation."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Finally, install "},{"type":"element","tagName":"a","properties":{"href":"https://callstack.github.io/react-native-paper/index.html"},"children":[{"type":"text","value":"React Native Paper"}]},{"type":"text","value":" to have beautifully-styled components in your app:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"bash"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-bash"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-bash"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"npm"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"install"}]},{"type":"text","value":" react-native-paper"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"set-up-react-navigation"},"children":[{"type":"text","value":"Set Up React Navigation"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Before you can start adding routes to your app, you need to create a Stack Navigator. This stack navigator will be used to create screens and navigate between them."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"App.js"}]},{"type":"text","value":", change the content to the following:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"js"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" React "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'react'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":" NavigationContainer "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'@react-navigation/native'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":" createNativeStackNavigator "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'@react-navigation/native-stack'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":" Provider "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" PaperProvider "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'react-native-paper'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" HomeScreen "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'./screens/HomeScreen'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"const"}]},{"type":"text","value":" Stack "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"createNativeStackNavigator"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"export"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"default"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"function"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"App"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n  "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"PaperProvider"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n      "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"NavigationContainer"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n        "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"Stack"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"Navigator"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n          "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"Stack"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"Screen name"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Home\""}]},{"type":"text","value":" component"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"HomeScreen"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n        "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"Stack"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"Navigator"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"NavigationContainer"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"PaperProvider"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n  "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Notice that in the returned value of "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"App"}]},{"type":"text","value":", you first have "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"<PaperProvider>"}]},{"type":"text","value":" at the top of the elements. This is just used to make sure all screens share the same theme."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Next, you have the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"<NavigationContainer>"}]},{"type":"text","value":". This component manages the navigation tree and contains the navigation state. So, it should wrap Stack navigators."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Inside "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"<NavigationContainer>"}]},{"type":"text","value":" you have "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"<Stack.Navigator>"}]},{"type":"text","value":". "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Stack"}]},{"type":"text","value":" is created using "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"createNativeStackNavigator"}]},{"type":"text","value":". This function returns an object containing 2 properties: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Screen"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Navigator"}]},{"type":"text","value":". The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Navigator"}]},{"type":"text","value":" property is a React element and it should be used to wrap the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Screen"}]},{"type":"text","value":" elements to manage routing configurations."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"So, inside "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"<Stack.Navigator>"}]},{"type":"text","value":" you'll have one or more "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"<Stack.Screen>"}]},{"type":"text","value":" components. For each route, you'll have a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"<Stack.Screen>"}]},{"type":"text","value":" component. Now, you just have one component that points to the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Home"}]},{"type":"text","value":" route."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Notice that a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Screen"}]},{"type":"text","value":" component takes the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"name"}]},{"type":"text","value":" prop, which is the name of the route and will be referenced later on when navigating to this route, and the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"component"}]},{"type":"text","value":" property which is the component to render for this route."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"As you can see, you're passing "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"HomeScreen"}]},{"type":"text","value":" component to the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Home"}]},{"type":"text","value":" route, so you need to create it now."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Create the directory "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"screens"}]},{"type":"text","value":", and inside that directory create a new file "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"HomeScreen.js"}]},{"type":"text","value":" with the following content:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"js"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" React "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'react'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":" ScrollView"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" StyleSheet "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'react-native'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":" Button"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" Card "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'react-native-paper'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":" DefaultTheme "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'react-native-paper'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"function"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"HomeScreen"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n  "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"ScrollView style"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"styles"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"scrollView"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n      "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"Card style"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"styles"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"card"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n        "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"Card"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"Title title"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Home Screen\""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n      "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"Card"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"ScrollView"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n  "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"const"}]},{"type":"text","value":" styles "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" StyleSheet"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"create"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n  "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"scrollView"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"backgroundColor"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" DefaultTheme"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"colors"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"background"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"paddingTop"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"text","value":"\n  "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n  "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"card"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"width"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'90%'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"marginLeft"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'auto'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"marginRight"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'auto'"}]},{"type":"text","value":"\n  "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"export"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"default"}]},{"type":"text","value":" HomeScreen"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This will create a basic home screen with a "},{"type":"element","tagName":"a","properties":{"href":"https://reactnative.dev/docs/scrollview"},"children":[{"type":"text","value":"ScrollView"}]},{"type":"text","value":" component containing a "},{"type":"element","tagName":"a","properties":{"href":"https://callstack.github.io/react-native-paper/card.html"},"children":[{"type":"text","value":"Card"}]},{"type":"text","value":" component with the title \"Home Screen\"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You just created your first route with React Navigation! Let's test it out."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In your terminal run to start your app:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"bash"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-bash"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-bash"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"npm"}]},{"type":"text","value":" start"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Then, you'll be able to run the app on Expo Go on your phone by scanning the QR code on the Expo Go app on Android or on the Camera app on iOS."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Once the app run, you'll see the Home screen that you created as it's the default and only route."}]},{"type":"element","tagName":"figure","properties":{"className":["kg-card","kg-image-card"]},"children":[{"type":"element","tagName":"img","properties":{"src":"https://backend.shahednasser.com/content/images/2021/12/IMG_1545.PNG","className":["kg-image"],"alt":"","loading":"lazy","width":1170,"height":2532,"srcSet":["https://backend.shahednasser.com/content/images/size/w600/2021/12/IMG_1545.PNG 600w","https://backend.shahednasser.com/content/images/size/w1000/2021/12/IMG_1545.PNG 1000w","https://backend.shahednasser.com/content/images/2021/12/IMG_1545.PNG 1170w"],"sizes":"(min-width: 720px) 720px"},"children":[]}]},{"type":"element","tagName":"h2","properties":{"id":"navigate-to-another-screen"},"children":[{"type":"text","value":"Navigate to Another Screen"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In this section, you'll learn how to navigate from one screen to another."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Screens that are inside the Stack Navigation, like HomeScreen in this app, receive a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"navigation"}]},{"type":"text","value":" prop. This prop allows us to navigate to other screens in the app."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's first create the screen you'll navigate to. Create the file "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"screens/BookScreen.js"}]},{"type":"text","value":" with the following content:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"js"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" React "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'react'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":" StyleSheet"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" View "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'react-native'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":" Card "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'react-native-paper'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":" DefaultTheme "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'react-native-paper'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"function"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"BookScreen"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n  "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"View style"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"styles"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"container"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n      "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"Card style"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"styles"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"card"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n        "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"Card"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"Title title"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"This is Books Screen\""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n      "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"Card"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"View"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n  "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"const"}]},{"type":"text","value":" styles "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" StyleSheet"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"create"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n  "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"container"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"flex"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"flexDirection"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'column'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"backgroundColor"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" DefaultTheme"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"colors"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"background"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"alignItems"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'center'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"paddingTop"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"text","value":"\n  "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n  "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"card"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"width"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'90%'"}]},{"type":"text","value":"\n  "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"export"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"default"}]},{"type":"text","value":" BookScreen"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This screen is similar to "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"HomeScreen"}]},{"type":"text","value":". It just shows a card with the title \"This is Books Screen\"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Next, add the new route in "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"App.js"}]},{"type":"text","value":" for "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"BookScreen"}]},{"type":"text","value":" below the \"Home\" route:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"js"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"Stack"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"Screen name"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Book\""}]},{"type":"text","value":" component"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"BookScreen"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Don't forget to import "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"BookScreen"}]},{"type":"text","value":" below the imports at the beginning of "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"App.js"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"js"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" BookScreen "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'./screens/BookScreen'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Now, you can navigate to the \"Book\" route inside this stack navigation. To see how it works, you'll add a button in "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"HomeScreen"}]},{"type":"text","value":" that when the user clicks will navigate to "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"BookScreen"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"HomeScreen"}]},{"type":"text","value":", add the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"navigation"}]},{"type":"text","value":" prop to the function:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"js"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"function"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"HomeScreen"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","parameter"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":" navigation "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Then, change the returned value of the function to the following:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"js"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"ScrollView style"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"styles"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"scrollView"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n      "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"Card style"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"styles"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"card"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n        "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"Card"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"Title title"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Navigate to 'Book' Screen\""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n        "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"Card"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"Content"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n          "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"Button mode"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"contained\""}]},{"type":"text","value":" onPress"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=>"}]},{"type":"text","value":" navigation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"navigate"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'Book'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n            Navigate\n          "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"Button"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n        "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"Card"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"Content"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n      "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"Card"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"ScrollView"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n  "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You change the title of the card to \"Navigate to 'Book' Screen\". Then, inside the content of the card, you add a "},{"type":"element","tagName":"a","properties":{"href":"https://callstack.github.io/react-native-paper/button.html"},"children":[{"type":"text","value":"Button"}]},{"type":"text","value":" component with "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"onPress"}]},{"type":"text","value":" listener. This listener will be executed when the user presses on the button."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Inside the listener, you use the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"navigation"}]},{"type":"text","value":" prop to navigate to the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Book"}]},{"type":"text","value":" screen using the method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"navigate"}]},{"type":"text","value":". The method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"navigate"}]},{"type":"text","value":" takes the name of the route to navigate to."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's test it out. Run the app again if it isn't running. You should see a new button on the Home screen."}]},{"type":"element","tagName":"figure","properties":{"className":["kg-card","kg-image-card"]},"children":[{"type":"element","tagName":"img","properties":{"src":"https://backend.shahednasser.com/content/images/2021/12/IMG_1543.PNG","className":["kg-image"],"alt":"","loading":"lazy","width":1170,"height":2532,"srcSet":["https://backend.shahednasser.com/content/images/size/w600/2021/12/IMG_1543.PNG 600w","https://backend.shahednasser.com/content/images/size/w1000/2021/12/IMG_1543.PNG 1000w","https://backend.shahednasser.com/content/images/2021/12/IMG_1543.PNG 1170w"],"sizes":"(min-width: 720px) 720px"},"children":[]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Try clicking on the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Navigate"}]},{"type":"text","value":" button. You'll be navigated to the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"BookScreen"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"figure","properties":{"className":["kg-card","kg-image-card"]},"children":[{"type":"element","tagName":"img","properties":{"src":"https://backend.shahednasser.com/content/images/2021/12/IMG_1544.PNG","className":["kg-image"],"alt":"","loading":"lazy","width":1170,"height":2532,"srcSet":["https://backend.shahednasser.com/content/images/size/w600/2021/12/IMG_1544.PNG 600w","https://backend.shahednasser.com/content/images/size/w1000/2021/12/IMG_1544.PNG 1000w","https://backend.shahednasser.com/content/images/2021/12/IMG_1544.PNG 1170w"],"sizes":"(min-width: 720px) 720px"},"children":[]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Notice how the back button in the navigation bar is added automatically when you navigate to another screen. You can use it to go back to the previous screen, which is in this case the Home screen."}]},{"type":"element","tagName":"h2","properties":{"id":"navigate-with-parameters"},"children":[{"type":"text","value":"Navigate with Parameters"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In a lot of cases, you'll need to navigate to another screen and pass it parameters. In this section, you'll create an input that allows the user to enter their name, then when they submit you'll open a new screen called "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"NameScreen"}]},{"type":"text","value":" which will show a greeting to the user with the name they entered."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Start by adding a new state variable to hold the value of the input you'll add soon in "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"HomeScreen.js"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"js"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"const"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" setName"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"useState"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"''"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Make sure to add an import for "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"useState"}]},{"type":"text","value":" at the beginning of the file:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"js"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" React"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":" useState "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'react'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Then, add a new Card component below the existing one:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"js"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"Card style"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"styles"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"card"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"Card"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"Title title"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Navigate with Parameters\""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"Card"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"Content"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n        "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"TextInput mode"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"outlined\""}]},{"type":"text","value":" label"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Name\""}]},{"type":"text","value":" value"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":" onChangeText"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","parameter"]},"children":[{"type":"text","value":"text"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=>"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"setName"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"text"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":" style"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"styles"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"textInput"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n            "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"Button mode"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"contained\""}]},{"type":"text","value":" disabled"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"length "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"==="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":" onPress"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=>"}]},{"type":"text","value":" navigation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"navigate"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'Name'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":" name "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n                Navigate\n    \t"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"Button"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"Card"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"Content"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"Card"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This new card has a "},{"type":"element","tagName":"a","properties":{"href":"https://callstack.github.io/react-native-paper/text-input.html"},"children":[{"type":"text","value":"TextInput"}]},{"type":"text","value":" with the value "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"name"}]},{"type":"text","value":", and on the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"onChangeText"}]},{"type":"text","value":" event, which occurs when the user makes changes to the text inside the input, you change the value of the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"name"}]},{"type":"text","value":" state variable."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Below the input, you have a button that is disabled when the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"name"}]},{"type":"text","value":" state variable is empty. You also add an "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"onPress"}]},{"type":"text","value":" event listener which navigates the user to the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Name"}]},{"type":"text","value":" route. "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Notice that you're using the same "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"navigation.navigate"}]},{"type":"text","value":" method that you used before to navigate to the \"Book\" route, however, this time you pass a second parameter to the method. This method accepts as a second parameter an object of parameters you want to pass. Here, you pass the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"name"}]},{"type":"text","value":" variable inside the object."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"One last thing you need to do in the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"HomeScreen"}]},{"type":"text","value":" is change the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"styles"}]},{"type":"text","value":" object to the following:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"js"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"const"}]},{"type":"text","value":" styles "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" StyleSheet"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"create"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n  "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"scrollView"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"backgroundColor"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" DefaultTheme"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"colors"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"background"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"paddingTop"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"text","value":"\n  "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n  "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"card"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"width"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'90%'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"marginLeft"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'auto'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"marginRight"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'auto'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"marginBottom"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"text","value":"\n  "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n  "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"textInput"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"marginBottom"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"text","value":"\n  "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This just makes some changes to the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"card"}]},{"type":"text","value":" property and adds a new property "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"textInput"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You'll now create the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"NameScreen"}]},{"type":"text","value":" which will be the component for the \"Name\" route you'll add soon. Create "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"screens/NameScreen.js"}]},{"type":"text","value":" with the following content:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"js"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" React "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'react'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":" StyleSheet"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" View "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'react-native'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":" Card "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'react-native-paper'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":" DefaultTheme "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'react-native-paper'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"function"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"NameScreen"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","parameter"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":" route "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n  "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"const"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":" name "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" route"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"params"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n\n  "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"View style"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"styles"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"container"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n      "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"Card style"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"styles"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"card"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n        "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"Card"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"Title title"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","template-string"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","template-punctuation","string"]},"children":[{"type":"text","value":"`"}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"Hello, "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","interpolation-punctuation","punctuation"]},"children":[{"type":"text","value":"${"}]},{"type":"text","value":"name"},{"type":"element","tagName":"span","properties":{"className":["token","interpolation-punctuation","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","template-punctuation","string"]},"children":[{"type":"text","value":"`"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n      "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"Card"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"View"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n  "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"const"}]},{"type":"text","value":" styles "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" StyleSheet"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"create"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n  "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"container"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"flex"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"flexDirection"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'column'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"backgroundColor"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" DefaultTheme"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"colors"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"background"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"alignItems"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'center'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"paddingTop"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"text","value":"\n  "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n  "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"card"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"width"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'90%'"}]},{"type":"text","value":"\n  "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"export"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"default"}]},{"type":"text","value":" NameScreen"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This screen is essentially the same as "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"BookScreen"}]},{"type":"text","value":", however, notice the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"route"}]},{"type":"text","value":" prop passed to the function."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"As mentioned before, the components inside the navigation stack receive a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"navigation"}]},{"type":"text","value":" prop to navigate between screens. They also receive a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"route"}]},{"type":"text","value":" prop which you can use to get the parameters passed to the screen using "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"route.params"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"So, in "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"NameScreen"}]},{"type":"text","value":", you retrieve the value entered in the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Name"}]},{"type":"text","value":" input in the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"HomeScreen"}]},{"type":"text","value":", which is passed when navigating to the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"NameScreen"}]},{"type":"text","value":" when the button is clicked using "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"route.params"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"js"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"const"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":" name "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" route"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"params"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You then show the user a greeting inside a card using the name parameter."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"One last thing left to do is add the \"Name\" route to the navigation stack. Add a new "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"<Stack.Screen>"}]},{"type":"text","value":" below the previous ones in "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"App.js"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"js"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"Stack"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"Screen name"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Name\""}]},{"type":"text","value":" component"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"NameScreen"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"And import "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"NameScreen"}]},{"type":"text","value":" below other imports at the beginning of the file:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"js"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" NameScreen "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'./screens/NameScreen'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's test it out. Run the app if it isn't already running. You'll see that there's a new card on the Home screen with an input and a button."}]},{"type":"element","tagName":"figure","properties":{"className":["kg-card","kg-image-card"]},"children":[{"type":"element","tagName":"img","properties":{"src":"https://backend.shahednasser.com/content/images/2021/12/IMG_1546.PNG","className":["kg-image"],"alt":"","loading":"lazy","width":1170,"height":2532,"srcSet":["https://backend.shahednasser.com/content/images/size/w600/2021/12/IMG_1546.PNG 600w","https://backend.shahednasser.com/content/images/size/w1000/2021/12/IMG_1546.PNG 1000w","https://backend.shahednasser.com/content/images/2021/12/IMG_1546.PNG 1170w"],"sizes":"(min-width: 720px) 720px"},"children":[]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Try entering your name, then click the \"Navigate\" button below the input. You'll be navigated to the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"NameScreen"}]},{"type":"text","value":" and you'll see your name."}]},{"type":"element","tagName":"figure","properties":{"className":["kg-card","kg-image-card"]},"children":[{"type":"element","tagName":"img","properties":{"src":"https://backend.shahednasser.com/content/images/2021/12/IMG_1547.PNG","className":["kg-image"],"alt":"","loading":"lazy","width":1170,"height":2532,"srcSet":["https://backend.shahednasser.com/content/images/size/w600/2021/12/IMG_1547.PNG 600w","https://backend.shahednasser.com/content/images/size/w1000/2021/12/IMG_1547.PNG 1000w","https://backend.shahednasser.com/content/images/2021/12/IMG_1547.PNG 1170w"],"sizes":"(min-width: 720px) 720px"},"children":[]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You can try going back then changing the value of the name. The name in the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"NameScreen"}]},{"type":"text","value":" will change accordingly."}]},{"type":"element","tagName":"h2","properties":{"id":"configure-the-header"},"children":[{"type":"text","value":"Configure the Header"}]},{"type":"element","tagName":"h3","properties":{"id":"change-the-title"},"children":[{"type":"text","value":"Change the title"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Sometimes, you want the title of a route to be different than the name of the route, or you want the title to be dynamic based on a parameter passed or some other logic."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In this section, you'll add a new input in "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"HomeScreen"}]},{"type":"text","value":" that will let you to enter a title, then on clicking the navigate button you'll be navigated to a new screen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"TitleScreen"}]},{"type":"text","value":", whose title will change based on the title entered in the input in HomeScreen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Start by adding a new state variable in "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"HomeScreen.js"}]},{"type":"text","value":" to store the title:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"js"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"const"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"title"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" setTitle"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"useState"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"''"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Then, add a new card below the previous cards:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"js"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"Card style"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"styles"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"card"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n        "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"Card"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"Title title"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Navigate to Route with Title\""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n        "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"Card"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"Content"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n          "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"TextInput mode"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"outlined\""}]},{"type":"text","value":" label"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Title\""}]},{"type":"text","value":" value"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"title"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":" onChangeText"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","parameter"]},"children":[{"type":"text","value":"text"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=>"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"setTitle"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"text"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":" style"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"styles"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"textInput"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n          "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"Button mode"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"contained\""}]},{"type":"text","value":" disabled"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"title"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"length "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"==="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":" onPress"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=>"}]},{"type":"text","value":" navigation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"navigate"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'Title'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":" title "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n            Navigate\n          "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"Button"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n        "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"Card"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"Content"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n      "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"Card"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This is pretty much the same as the card we added in the previous section. The only difference is that it uses the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"title"}]},{"type":"text","value":" state variable, and the listener for "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"onPress"}]},{"type":"text","value":" for the button navigates to the route \"Title\", passing the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"title"}]},{"type":"text","value":" as a parameter."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Next, create the file "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"screens/Title.js"}]},{"type":"text","value":" with the following content:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"js"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" React "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'react'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":" StyleSheet"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" View "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'react-native'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":" Card "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'react-native-paper'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":" DefaultTheme "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'react-native-paper'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"function"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"TitleScreen"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n  "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"View style"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"styles"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"container"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n      "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"Card style"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"styles"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"card"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n        "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"Card"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"Title title"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"This is title screen\""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n      "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"Card"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"View"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n  "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"const"}]},{"type":"text","value":" styles "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" StyleSheet"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"create"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n  "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"container"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"flex"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"flexDirection"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'column'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"backgroundColor"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" DefaultTheme"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"colors"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"background"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"alignItems"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'center'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"paddingTop"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"text","value":"\n  "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n  "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"card"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"width"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'90%'"}]},{"type":"text","value":"\n  "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"export"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"default"}]},{"type":"text","value":" TitleScreen"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This screen has nothing special. It just shows a card with the title \"This is title screen\". It doesn't even use the title parameter."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"That's because you can change the title of a screen by passing an "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"options"}]},{"type":"text","value":" parameter to "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"<Stack.Screen>"}]},{"type":"text","value":" when adding the route."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"App.js"}]},{"type":"text","value":", add an import for "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"TitleScreen"}]},{"type":"text","value":" below other imports at the beginning of the file:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"js"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" TitleScreen "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'./screens/TitleScreen'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Then, add the new route below previous "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"<Stack.Screen>"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"js"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"Stack"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"Screen name"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Title\""}]},{"type":"text","value":" component"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"TitleScreen"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":" options"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","parameter"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"route"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=>"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"title"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" route"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"params"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"title"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Every "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Stack.Screen"}]},{"type":"text","value":" component can take an "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"options"}]},{"type":"text","value":" prop. The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"options"}]},{"type":"text","value":" prop can be an object like so:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"js"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-js"]},"children":[{"type":"text","value":"options"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"title"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hello\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This is enough if your options are static. However, if the value for any of the options is dynamic, like in this example, you can instead pass it a function that returns an object. This function can receive as a parameter "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"route"}]},{"type":"text","value":", which is similar to the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"route"}]},{"type":"text","value":" parameter you used in "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"NameScreen"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"js"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-js"]},"children":[{"type":"text","value":"options"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","parameter"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"route"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=>"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"title"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" route"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"params"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"title"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In this function, you return an object that has a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"title"}]},{"type":"text","value":" property with the value "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"route.params.title"}]},{"type":"text","value":". So, when the user enters a title in the input and clicks on the Navigate button, the title will be passed to the route, which will change the title of \"Title\" route using the function passed to the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"options"}]},{"type":"text","value":" prop."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's test it out. Run the app if it's not running already. You should see a new card on the Home screen with a new input:"}]},{"type":"element","tagName":"figure","properties":{"className":["kg-card","kg-image-card"]},"children":[{"type":"element","tagName":"img","properties":{"src":"https://backend.shahednasser.com/content/images/2021/12/IMG_1548.PNG","className":["kg-image"],"alt":"","loading":"lazy","width":1170,"height":2532,"srcSet":["https://backend.shahednasser.com/content/images/size/w600/2021/12/IMG_1548.PNG 600w","https://backend.shahednasser.com/content/images/size/w1000/2021/12/IMG_1548.PNG 1000w","https://backend.shahednasser.com/content/images/2021/12/IMG_1548.PNG 1170w"],"sizes":"(min-width: 720px) 720px"},"children":[]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Try entering any title you want in the input then click on Navigate. You'll see that you'll be navigated to the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"TitleScreen"}]},{"type":"text","value":", but the title will be the value you entered in the input."}]},{"type":"element","tagName":"figure","properties":{"className":["kg-card","kg-image-card"]},"children":[{"type":"element","tagName":"img","properties":{"src":"https://backend.shahednasser.com/content/images/2021/12/IMG_1549.PNG","className":["kg-image"],"alt":"","loading":"lazy","width":1170,"height":2532,"srcSet":["https://backend.shahednasser.com/content/images/size/w600/2021/12/IMG_1549.PNG 600w","https://backend.shahednasser.com/content/images/size/w1000/2021/12/IMG_1549.PNG 1000w","https://backend.shahednasser.com/content/images/2021/12/IMG_1549.PNG 1170w"],"sizes":"(min-width: 720px) 720px"},"children":[]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You can try going back then changing the value entered in the input. The title of the screen will change accordingly."}]},{"type":"element","tagName":"h3","properties":{"id":"add-header-button"},"children":[{"type":"text","value":"Add Header Button"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Another option you can add is header buttons. You can customize what your header shows on the left and right by using the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"headerRight"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"headerLeft"}]},{"type":"text","value":" properties in the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"options"}]},{"type":"text","value":" param of "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"<Stack.Screen>"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In this section, you'll add a button to the right of the header of the Home screen which will show a user an alert when they click on it."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"App.js"}]},{"type":"text","value":", add to the \"Home\" route a prop "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"option"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"js"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"Stack"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"Screen name"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Home\""}]},{"type":"text","value":" component"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"HomeScreen"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":" options"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n            "},{"type":"element","tagName":"span","properties":{"className":["token","function-variable","function"]},"children":[{"type":"text","value":"headerRight"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=>"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"\n              "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"IconButton icon"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"alert-outline\""}]},{"type":"text","value":" onPress"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=>"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"alert"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'You\\'re awesome!'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":" color"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"DefaultTheme"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"colors"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"notification"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n            "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"options"}]},{"type":"text","value":" prop, you pass an object with the property "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"headerRight"}]},{"type":"text","value":". The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"headerRight"}]},{"type":"text","value":" property can be a component that you can render elements inside. You render an "},{"type":"element","tagName":"a","properties":{"href":"https://callstack.github.io/react-native-paper/icon-button.html"},"children":[{"type":"text","value":"IconButton"}]},{"type":"text","value":" component, which is a button that only has an icon without text. In the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"onPress"}]},{"type":"text","value":" event listener, you show an alert to the user."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"That's all. Run the app if it isn't running already. You'll see a new icon at the top right of the header in Home Screen."}]},{"type":"element","tagName":"figure","properties":{"className":["kg-card","kg-image-card"]},"children":[{"type":"element","tagName":"img","properties":{"src":"https://backend.shahednasser.com/content/images/2021/12/IMG_1550.PNG","className":["kg-image"],"alt":"","loading":"lazy","width":1170,"height":2532,"srcSet":["https://backend.shahednasser.com/content/images/size/w600/2021/12/IMG_1550.PNG 600w","https://backend.shahednasser.com/content/images/size/w1000/2021/12/IMG_1550.PNG 1000w","https://backend.shahednasser.com/content/images/2021/12/IMG_1550.PNG 1170w"],"sizes":"(min-width: 720px) 720px"},"children":[]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Click on it and you'll see an alert."}]},{"type":"element","tagName":"figure","properties":{"className":["kg-card","kg-image-card"]},"children":[{"type":"element","tagName":"img","properties":{"src":"https://backend.shahednasser.com/content/images/2021/12/IMG_1551.PNG","className":["kg-image"],"alt":"","loading":"lazy","width":1170,"height":2532,"srcSet":["https://backend.shahednasser.com/content/images/size/w600/2021/12/IMG_1551.PNG 600w","https://backend.shahednasser.com/content/images/size/w1000/2021/12/IMG_1551.PNG 1000w","https://backend.shahednasser.com/content/images/2021/12/IMG_1551.PNG 1170w"],"sizes":"(min-width: 720px) 720px"},"children":[]}]},{"type":"element","tagName":"h2","properties":{"id":"navigation-events"},"children":[{"type":"text","value":"Navigation Events"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In React Navigation, you can subscribe to two events: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"focus"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"blur"}]},{"type":"text","value":". The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"focus"}]},{"type":"text","value":" event will occur every time the screen is opened and is currently the main screen. The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"blur"}]},{"type":"text","value":" event will occur whenever a screen was in focus but navigation occurs that makes the screen not the current main screen. Simply put, the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"focus"}]},{"type":"text","value":" event is when the screen is visible to the user, and the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"blur"}]},{"type":"text","value":" event occurs when a screen was visible to a user but isn't anymore. "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In this section, you'll add a counter that will count how many times you navigated to other screens from the home screen. So, this counter will increment in the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"blur"}]},{"type":"text","value":" event."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"To subscribe to events, in a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"useEffect"}]},{"type":"text","value":" callback inside a component, you can use "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"navigation.addListener"}]},{"type":"text","value":" passing it the name of the event as a first parameter, and the event handler as a second parameter. The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"addListener"}]},{"type":"text","value":" method returns an unsubscribe function which you should return in the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"useEffect"}]},{"type":"text","value":" callback."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"HomeScreen.js"}]},{"type":"text","value":", add an import for "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"useEffect"}]},{"type":"text","value":" at the beginning of the file:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"js"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" React"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":" useEffect"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" useState "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'react'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Then, add a new state variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"counter"}]},{"type":"text","value":" inside the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"HomeScreen"}]},{"type":"text","value":" component:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"js"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"const"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"counter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" setCounter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"useState"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"After that add the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"useEffect"}]},{"type":"text","value":" callback that will register the event handle for the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"blur"}]},{"type":"text","value":" event:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"js"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"useEffect"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=>"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" navigation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"addListener"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'blur'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=>"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n      "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"setCounter"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"counter "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n  "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"As you can see, you use "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"navigation.addListener"}]},{"type":"text","value":" to add an event listener to the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"blur"}]},{"type":"text","value":" event. The function passed as a second parameter is the handler of the event. It will be executed whenever the screen is out of focus (not the screen visible to the user). Inside the event handler, you increment the counter by 1."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Finally, you'll show the counter at the top of the screen. In "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ScrollView"}]},{"type":"text","value":", add a new card before the previously added cards:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"js"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"Card style"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"styles"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"card"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n\t"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"Card"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"Title title"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","template-string"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","template-punctuation","string"]},"children":[{"type":"text","value":"`"}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"Navigation Count: "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","interpolation-punctuation","punctuation"]},"children":[{"type":"text","value":"${"}]},{"type":"text","value":"counter"},{"type":"element","tagName":"span","properties":{"className":["token","interpolation-punctuation","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","template-punctuation","string"]},"children":[{"type":"text","value":"`"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"Card"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's test it out. Run the app if it's not already running. You should see a card with a count of navigation at the top of the screen."}]},{"type":"element","tagName":"h2","properties":{"id":"go-back"},"children":[{"type":"text","value":"Go Back"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The last thing you'll learn in this tutorial is how to go back to the previous screen. As you've seen throughout the tutorial, when you navigate to a screen you'll see a back button in the header which you can use to go back to the previous screen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Although this is sufficient in most cases, sometimes you want to go back to the previous screen programmatically. You'll create in this section a new screen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"BackScreen"}]},{"type":"text","value":" which you can go to from the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"HomeScreen"}]},{"type":"text","value":". This screen will have a back button in its content and when you click on it you can go back to the previous screen, which is the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"HomeScreen"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Create the file "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"screens/BackScreen.js"}]},{"type":"text","value":" with the following content:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"js"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" React "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'react'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":" StyleSheet"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" View "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'react-native'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":" Button"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" Card "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'react-native-paper'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":" DefaultTheme "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'react-native-paper'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"function"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"BackScreen"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","parameter"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":" navigation "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n  "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"View style"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"styles"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"container"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n      "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"Card style"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"styles"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"card"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n        "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"Card"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"Title title"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"This is Back Screen\""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n        "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"Card"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"Content"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n          "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"Button mode"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"contained\""}]},{"type":"text","value":" onPress"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=>"}]},{"type":"text","value":" navigation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"goBack"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n            Go Back\n          "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"Button"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n        "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"Card"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"Content"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n      "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"Card"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"View"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n  "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"const"}]},{"type":"text","value":" styles "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" StyleSheet"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"create"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n  "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"container"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"flex"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"flexDirection"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'column'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"backgroundColor"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" DefaultTheme"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"colors"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"background"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"alignItems"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'center'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"paddingTop"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"text","value":"\n  "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n  "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"card"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"width"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'90%'"}]},{"type":"text","value":"\n  "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"export"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"default"}]},{"type":"text","value":" BackScreen"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This screen is pretty much the same as other screens. The things you should note is that, first, the screen has uses the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"navigation"}]},{"type":"text","value":" prop that is passed to components inside the navigation stack. Second, the screen has a button inside a card which, on press, will use the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"navigation"}]},{"type":"text","value":" prop to go back using the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"goBack"}]},{"type":"text","value":" method:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"js"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"Button mode"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"contained\""}]},{"type":"text","value":" onPress"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=>"}]},{"type":"text","value":" navigation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"goBack"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Now, go to the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"HomeScreen"}]},{"type":"text","value":" and add a new card at the end of the ScrollView:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"js"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"Card style"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"styles"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"card"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"Card"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"Title title"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Navigate to 'Back' Screen\""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"Card"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"Content"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n        "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"Button mode"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"contained\""}]},{"type":"text","value":" onPress"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=>"}]},{"type":"text","value":" navigation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"navigate"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'Back'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n            Navigate\n    \t"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"Button"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"Card"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"Content"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"Card"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This card just shows a button to navigate to the route \"Back\". Let's add that route now."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"App.js"}]},{"type":"text","value":" add a new "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"<Stack.Screen>"}]},{"type":"text","value":" component below the previously added ones:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"js"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"Stack"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"Screen name"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Back\""}]},{"type":"text","value":" component"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"BackScreen"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"It's ready now. Run the app if it isn't already running. You'll see a new card at the end of the list."}]},{"type":"element","tagName":"figure","properties":{"className":["kg-card","kg-image-card"]},"children":[{"type":"element","tagName":"img","properties":{"src":"https://backend.shahednasser.com/content/images/2021/12/IMG_1554.PNG","className":["kg-image"],"alt":"","loading":"lazy","width":1170,"height":2532,"srcSet":["https://backend.shahednasser.com/content/images/size/w600/2021/12/IMG_1554.PNG 600w","https://backend.shahednasser.com/content/images/size/w1000/2021/12/IMG_1554.PNG 1000w","https://backend.shahednasser.com/content/images/2021/12/IMG_1554.PNG 1170w"],"sizes":"(min-width: 720px) 720px"},"children":[]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Click on \"Navigate\" on the last card. You'll be navigated to the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"BackScreen"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"figure","properties":{"className":["kg-card","kg-image-card"]},"children":[{"type":"element","tagName":"img","properties":{"src":"https://backend.shahednasser.com/content/images/2021/12/IMG_1555.PNG","className":["kg-image"],"alt":"","loading":"lazy","width":1170,"height":2532,"srcSet":["https://backend.shahednasser.com/content/images/size/w600/2021/12/IMG_1555.PNG 600w","https://backend.shahednasser.com/content/images/size/w1000/2021/12/IMG_1555.PNG 1000w","https://backend.shahednasser.com/content/images/2021/12/IMG_1555.PNG 1170w"],"sizes":"(min-width: 720px) 720px"},"children":[]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Click on the \"Go Back\" button in the card. You'll be taken back to the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"HomeScreen"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"h2","properties":{"id":"conclusion"},"children":[{"type":"text","value":"Conclusion"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In this tutorial, you learned how to use React Navigation to navigate between screens, pass a parameter from one screen to another, configure the header of a screen, add navigation event listeners, and go back programmatically to the previous screen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Although this covers most use cases for using React Navigation, be sure to check out "},{"type":"element","tagName":"a","properties":{"href":"https://reactnavigation.org/docs/getting-started"},"children":[{"type":"text","value":"their documentation"}]},{"type":"text","value":" for more details on how to use it. "}]}],"data":{"quirksMode":false}},"tableOfContents":[{"id":"prerequisites","heading":"Prerequisites "},{"id":"project-setup","heading":"Project Setup"},{"id":"set-up-react-navigation","heading":"Set Up React Navigation"},{"id":"navigate-to-another-screen","heading":"Navigate to Another Screen"},{"id":"navigate-with-parameters","heading":"Navigate with Parameters"},{"id":"configure-the-header","heading":"Configure the Header","items":[{"id":"change-the-title","heading":"Change the title"},{"id":"add-header-button","heading":"Add Header Button"}]},{"id":"navigation-events","heading":"Navigation Events"},{"id":"go-back","heading":"Go Back"},{"id":"conclusion","heading":"Conclusion"}]},"featureImageSharp":{"base":"nathan-da-silva-FO7kUmBYVi0-unsplash-2.jpg","publicURL":"/static/de081a39f1737345436f7c509de3f888/nathan-da-silva-FO7kUmBYVi0-unsplash-2.jpg","imageMeta":{"width":1920,"height":1920},"childImageSharp":{"fluid":{"base64":"data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAUABQDASIAAhEBAxEB/8QAGAABAQEBAQAAAAAAAAAAAAAAAAQDAQX/xAAWAQEBAQAAAAAAAAAAAAAAAAABAAL/2gAMAwEAAhADEAAAAa/MqiqloGXpjeoH/8QAHBAAAwABBQAAAAAAAAAAAAAAAQIDBAAREhNB/9oACAEBAAEFAsp2SUbGtaYoL3r2pORDkjck8vdf/8QAFREBAQAAAAAAAAAAAAAAAAAAEAL/2gAIAQMBAT8BJP/EABURAQEAAAAAAAAAAAAAAAAAABAC/9oACAECAQE/ASj/xAAdEAACAgIDAQAAAAAAAAAAAAABAgARITEQImFx/9oACAEBAAY/AjjF1ERqqE2IwC7gZgFHpnXI+Sr1z//EABsQAQADAQADAAAAAAAAAAAAAAEAESFBUWGR/9oACAEBAAE/IcQNHtKWxcztQcb63ctHcd3IYFtrA/0rRe4NFRMLa+XYs//aAAwDAQACAAMAAAAQcxD/AP/EABoRAAICAwAAAAAAAAAAAAAAAAABEBEhQVH/2gAIAQMBAT8Q2Z4KquP/xAAZEQACAwEAAAAAAAAAAAAAAAAAARARQYH/2gAIAQIBAT8Qw6O7j//EAB4QAQACAgIDAQAAAAAAAAAAAAEAESExUWFBcZGh/9oACAEBAAE/EMGIlt7DxBWSKotFV36jFK1KVVy4YwKlYoFbCs+JvJCExYLifzwlwUG3QYK/b22/Ytz/2Q==","aspectRatio":1,"src":"/static/de081a39f1737345436f7c509de3f888/ea4ab/nathan-da-silva-FO7kUmBYVi0-unsplash-2.jpg","srcSet":"/static/de081a39f1737345436f7c509de3f888/477ba/nathan-da-silva-FO7kUmBYVi0-unsplash-2.jpg 175w,\n/static/de081a39f1737345436f7c509de3f888/06776/nathan-da-silva-FO7kUmBYVi0-unsplash-2.jpg 350w,\n/static/de081a39f1737345436f7c509de3f888/ea4ab/nathan-da-silva-FO7kUmBYVi0-unsplash-2.jpg 700w,\n/static/de081a39f1737345436f7c509de3f888/3055e/nathan-da-silva-FO7kUmBYVi0-unsplash-2.jpg 1050w,\n/static/de081a39f1737345436f7c509de3f888/eff08/nathan-da-silva-FO7kUmBYVi0-unsplash-2.jpg 1400w,\n/static/de081a39f1737345436f7c509de3f888/74fd5/nathan-da-silva-FO7kUmBYVi0-unsplash-2.jpg 1920w","srcWebp":"/static/de081a39f1737345436f7c509de3f888/89afa/nathan-da-silva-FO7kUmBYVi0-unsplash-2.webp","srcSetWebp":"/static/de081a39f1737345436f7c509de3f888/9fca7/nathan-da-silva-FO7kUmBYVi0-unsplash-2.webp 175w,\n/static/de081a39f1737345436f7c509de3f888/37a4e/nathan-da-silva-FO7kUmBYVi0-unsplash-2.webp 350w,\n/static/de081a39f1737345436f7c509de3f888/89afa/nathan-da-silva-FO7kUmBYVi0-unsplash-2.webp 700w,\n/static/de081a39f1737345436f7c509de3f888/78e7a/nathan-da-silva-FO7kUmBYVi0-unsplash-2.webp 1050w,\n/static/de081a39f1737345436f7c509de3f888/03d34/nathan-da-silva-FO7kUmBYVi0-unsplash-2.webp 1400w,\n/static/de081a39f1737345436f7c509de3f888/6833b/nathan-da-silva-FO7kUmBYVi0-unsplash-2.webp 1920w","sizes":"(max-width: 700px) 100vw, 700px"}}}},"allGhostPost":{"edges":[{"node":{"id":"Ghost__Post__62dd14674e918d05f353777d","title":"React (TanStack) Query Tutorial for Beginners","slug":"react-query-tutorial-for-beginners","featured":true,"feature_image":"https://backend.shahednasser.com/content/images/2022/07/React--TanStack--Query-Tutorial-for-Beginners.jpg","excerpt":"In this tutorial, you'll build a simple Node.js server and then learn how to interact with it on a React website using React Query.","custom_excerpt":"In this tutorial, you'll build a simple Node.js server and then learn how to interact with it on a React website using React Query.","visibility":"public","created_at_pretty":"24 Jul 2022","published_at_pretty":"24 Jul 2022","updated_at_pretty":"24 Jul 2022","created_at":"2022-07-24T09:44:07.000+00:00","published_at":"2022-07-24T15:50:35.000+00:00","updated_at":"2022-07-24T15:50:35.000+00:00","meta_title":null,"meta_description":null,"og_description":null,"og_image":null,"og_title":null,"twitter_description":null,"twitter_image":null,"twitter_title":null,"authors":[{"slug":"shahed","url":"https://backend.shahednasser.com/author/shahed/","name":"Shahed Nasser","bio":null,"cover_image":null,"profile_image":"https://backend.shahednasser.com/content/images/2022/03/IMG_0591.jpg","location":null,"website":null,"twitter":null,"facebook":null,"meta_title":null,"meta_description":null,"coverImageSharp":null,"profileImageSharp":null}],"primary_author":{"slug":"shahed","url":"https://backend.shahednasser.com/author/shahed/","name":"Shahed Nasser","bio":null,"cover_image":null,"profile_image":"https://backend.shahednasser.com/content/images/2022/03/IMG_0591.jpg","location":null,"website":null,"twitter":null,"facebook":null,"meta_title":null,"meta_description":null,"coverImageSharp":null,"profileImageSharp":{"base":"IMG_0591.jpg","publicURL":"/static/ceb49c3c631485453e71e00d7f84b069/IMG_0591.jpg","imageMeta":{"width":1182,"height":1179},"childImageSharp":{"fluid":{"base64":"data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAUABQDASIAAhEBAxEB/8QAGAABAQEBAQAAAAAAAAAAAAAAAAMEAQL/xAAWAQEBAQAAAAAAAAAAAAAAAAADBAL/2gAMAwEAAhADEAAAAdXiFM6i0CohUWXoKn//xAAcEAACAgIDAAAAAAAAAAAAAAACAwESBBEhM0H/2gAIAQEAAQUCWySE3WEr7SzbXjAj4iKty+sOQ//EABYRAQEBAAAAAAAAAAAAAAAAAAERIP/aAAgBAwEBPwEhj//EABQRAQAAAAAAAAAAAAAAAAAAACD/2gAIAQIBAT8BH//EAB4QAAIBBAMBAAAAAAAAAAAAAAABIRESMUECECJx/9oACAEBAAY/ApVGWvOjzgtUwLlTZA0sdL4f/8QAHBAAAwACAwEAAAAAAAAAAAAAAAERITFBkbHB/9oACAEBAAE/IahkCy+N2GwZpjQiJHJCspUFY0QrSi+HqiW2rgf/2gAMAwEAAgADAAAAEPw3/wD/xAAYEQEBAAMAAAAAAAAAAAAAAAAAARExQf/aAAgBAwEBPxCtjDqP/8QAFxEBAQEBAAAAAAAAAAAAAAAAAQAxEf/aAAgBAgEBPxBFus6Tt//EAB8QAQEAAgIBBQAAAAAAAAAAAAERACExQWFRcYGR0f/aAAgBAQABPxAuaBPPzkO1wyX7F4wkwXanfZrFQgeqE9JgS14vVOvrERIJomVBKwt2jebAeP0yVa8h1n//2Q==","aspectRatio":1,"src":"/static/ceb49c3c631485453e71e00d7f84b069/31709/IMG_0591.jpg","srcSet":"/static/ceb49c3c631485453e71e00d7f84b069/f340b/IMG_0591.jpg 28w,\n/static/ceb49c3c631485453e71e00d7f84b069/22d64/IMG_0591.jpg 55w,\n/static/ceb49c3c631485453e71e00d7f84b069/31709/IMG_0591.jpg 110w,\n/static/ceb49c3c631485453e71e00d7f84b069/aa249/IMG_0591.jpg 165w,\n/static/ceb49c3c631485453e71e00d7f84b069/0dc33/IMG_0591.jpg 220w,\n/static/ceb49c3c631485453e71e00d7f84b069/d8257/IMG_0591.jpg 1182w","srcWebp":"/static/ceb49c3c631485453e71e00d7f84b069/8678c/IMG_0591.webp","srcSetWebp":"/static/ceb49c3c631485453e71e00d7f84b069/59cda/IMG_0591.webp 28w,\n/static/ceb49c3c631485453e71e00d7f84b069/7da75/IMG_0591.webp 55w,\n/static/ceb49c3c631485453e71e00d7f84b069/8678c/IMG_0591.webp 110w,\n/static/ceb49c3c631485453e71e00d7f84b069/f282e/IMG_0591.webp 165w,\n/static/ceb49c3c631485453e71e00d7f84b069/a7b21/IMG_0591.webp 220w,\n/static/ceb49c3c631485453e71e00d7f84b069/63099/IMG_0591.webp 1182w","sizes":"(max-width: 110px) 100vw, 110px"}}}},"primary_tag":{"slug":"react","url":"https://backend.shahednasser.com/tag/react/","name":"React","visibility":"public","feature_image":"https://images.unsplash.com/photo-1581276879432-15e50529f34b?crop=entropy&cs=tinysrgb&fit=max&fm=jpg&ixid=MnwxMTc3M3wwfDF8c2VhcmNofDN8fHJlYWN0fGVufDB8fHx8MTYyMjYzMzI0MA&ixlib=rb-1.2.1&q=80&w=2000","description":"Learn more about React through tutorials, articles, and tips.","meta_title":null,"meta_description":null,"featureImageSharp":{"base":"photo-1581276879432-15e50529f34b.jpg","publicURL":"/static/7140ea32e1157ba61402d5d67ab846d4/photo-1581276879432-15e50529f34b.jpg","imageMeta":{"width":2000,"height":1333},"childImageSharp":{"fluid":{"base64":"data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAANABQDASIAAhEBAxEB/8QAFwAAAwEAAAAAAAAAAAAAAAAAAAIDBf/EABUBAQEAAAAAAAAAAAAAAAAAAAAB/9oADAMBAAIQAxAAAAHJai1MBP/EABcQAAMBAAAAAAAAAAAAAAAAAAABEBH/2gAIAQEAAQUCuDU//8QAFBEBAAAAAAAAAAAAAAAAAAAAEP/aAAgBAwEBPwE//8QAFREBAQAAAAAAAAAAAAAAAAAAEBH/2gAIAQIBAT8Bh//EABQQAQAAAAAAAAAAAAAAAAAAACD/2gAIAQEABj8CX//EABgQAQEBAQEAAAAAAAAAAAAAAAABESFR/9oACAEBAAE/IYy+rHKDH//aAAwDAQACAAMAAAAQC9//xAAVEQEBAAAAAAAAAAAAAAAAAAAQEf/aAAgBAwEBPxCn/8QAFREBAQAAAAAAAAAAAAAAAAAAARD/2gAIAQIBAT8QUT//xAAaEAADAQADAAAAAAAAAAAAAAAAAREhQWFx/9oACAEBAAE/EMY0neijzwMrrVEm7auUQPNGlP/Z","aspectRatio":1.5028901734104045,"src":"/static/7140ea32e1157ba61402d5d67ab846d4/d5c54/photo-1581276879432-15e50529f34b.jpg","srcSet":"/static/7140ea32e1157ba61402d5d67ab846d4/65d8c/photo-1581276879432-15e50529f34b.jpg 260w,\n/static/7140ea32e1157ba61402d5d67ab846d4/c5f21/photo-1581276879432-15e50529f34b.jpg 520w,\n/static/7140ea32e1157ba61402d5d67ab846d4/d5c54/photo-1581276879432-15e50529f34b.jpg 1040w,\n/static/7140ea32e1157ba61402d5d67ab846d4/81a53/photo-1581276879432-15e50529f34b.jpg 1560w,\n/static/7140ea32e1157ba61402d5d67ab846d4/4e5f3/photo-1581276879432-15e50529f34b.jpg 2000w","srcWebp":"/static/7140ea32e1157ba61402d5d67ab846d4/e4875/photo-1581276879432-15e50529f34b.webp","srcSetWebp":"/static/7140ea32e1157ba61402d5d67ab846d4/dc8f3/photo-1581276879432-15e50529f34b.webp 260w,\n/static/7140ea32e1157ba61402d5d67ab846d4/2db4b/photo-1581276879432-15e50529f34b.webp 520w,\n/static/7140ea32e1157ba61402d5d67ab846d4/e4875/photo-1581276879432-15e50529f34b.webp 1040w,\n/static/7140ea32e1157ba61402d5d67ab846d4/f5845/photo-1581276879432-15e50529f34b.webp 1560w,\n/static/7140ea32e1157ba61402d5d67ab846d4/49d6b/photo-1581276879432-15e50529f34b.webp 2000w","sizes":"(max-width: 1040px) 100vw, 1040px"}}}},"tags":[{"slug":"react","url":"https://backend.shahednasser.com/tag/react/","name":"React","visibility":"public","feature_image":"https://images.unsplash.com/photo-1581276879432-15e50529f34b?crop=entropy&cs=tinysrgb&fit=max&fm=jpg&ixid=MnwxMTc3M3wwfDF8c2VhcmNofDN8fHJlYWN0fGVufDB8fHx8MTYyMjYzMzI0MA&ixlib=rb-1.2.1&q=80&w=2000","description":"Learn more about React through tutorials, articles, and tips.","meta_title":null,"meta_description":null,"featureImageSharp":null},{"slug":"js","url":"https://backend.shahednasser.com/tag/js/","name":"Javascript","visibility":"public","feature_image":"https://backend.shahednasser.com/content/images/2022/01/photo-1592609931095-54a2168ae893-2.jpeg","description":"Learn more about Javascript through tutorials, articles, and tips.","meta_title":null,"meta_description":"Learn more about Javascript through tutorials, articles and tips.","featureImageSharp":null}],"plaintext":"React Query (now rebranded to TanStack Query) is a React library used to make fetching and manipulating server-side data easier. Using React Query, you can implement, along with data fetching, caching, and synchronization of your data with the server.\n\nIn this tutorial, you'll build a simple Node.js server and then learn how to interact with it on a React website using React Query.\n\nPlease note that this version uses v4 of React Query which is now named TanStack Query.\n\nYou can find the code for this tutorial in this GitHub repository.\n\n\nPrerequisites\n\nBefore starting with this tutorial make sure you have Node.js installed. You need at least version 14.\n\n\nServer Setup\n\nIn this section, you'll set up a simple Node.js server with an SQLite database. The server has 3 endpoints to fetch, add, and delete notes.\n\nIf you already have a server you can skip this section and go to the Website Setup section.\n\n\nCreate Server Project\n\nCreate a new directory called server then initialize a new project using NPM:\n\nmkdir server\ncd server\nnpm init -y\n\n\nInstall Dependencies\n\nThen, install the packages you'll need for the development of the server:\n\nnpm i express cors body-parser sqlite3 nodemon\n\nHere's what each of the packages is for:\n\n 1. express to create a server using Express.\n 2. cors is an Express middleware used to handle CORS on your server.\n 3. body-parser is an Express middleware used to parse the body of a request.\n 4. sqlite3 is an SQLite database adapter for Node.js.\n 5. nodemon is a library used to restart the server whenever new changes occur to the files.\n\n\nCreate Server\n\nCreate the file index.js with the following content:\n\nconst express = require('express');\n\nconst app = express();\nconst port = 3001;\nconst cors = require('cors');\nconst sqlite3 = require('sqlite3').verbose();\nconst bodyParser = require('body-parser');\n\napp.use(bodyParser.json());\napp.use(cors());\n\napp.listen(port, () => {\n  console.log(`Notes app listening on port ${port}`);\n});\n\nThis initializes the server using Express on port 3001. It also uses the cors and body-parser middleware.\n\nThen, in package.json add a new script start to run the server:\n\n  \"scripts\": {\n    \"start\": \"nodemon index.js\"\n  },\n\n\nInitialize the Database\n\nIn index.js before app.listen add the following code:\n\nconst db = new sqlite3.Database('data.db', (err) => {\n  if (err) {\n    throw err;\n  }\n\n  // create tables if they don't exist\n  db.serialize(() => {\n    db.run(`CREATE TABLE IF NOT EXISTS notes (id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT, content TEXT, \n      created_at datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, updated_at datetime NOT NULL DEFAULT CURRENT_TIMESTAMP)`);\n  });\n});\n\nThis creates a new database if it doesn't exist in the file data.db. Then, if the notes table doesn't exist on the database it creates it as well.\n\n\nAdd Endpoints\n\nFollowing the database code, add the following code to add the endpoints:\n\napp.get('/notes', (req, res) => {\n  db.all('SELECT * FROM notes', (err, rows) => {\n    if (err) {\n      console.error(err);\n      return res.status(500).json({ success: false, message: 'An error occurred, please try again later' });\n    }\n\n    return res.json({ success: true, data: rows });\n  });\n});\n\napp.get('/notes/:id', (req, res) => {\n  db.get('SELECT * FROM notes WHERE id = ?', req.params.id, (err, row) => {\n    if (err) {\n      console.error(err);\n      return res.status(500).json({ success: false, message: 'An error occurred, please try again later' });\n    }\n\n    if (!row) {\n      return res.status(404).json({ success: false, message: 'Note does not exist' });\n    }\n\n    return res.json({ success: true, data: row });\n  });\n});\n\napp.post('/notes', (req, res) => {\n  const { title, content } = req.body;\n\n  if (!title || !content) {\n    return res.status(400).json({ success: false, message: 'title and content are required' });\n  }\n\n  db.run('INSERT INTO notes (title, content) VALUES (?, ?)', [title, content], function (err) {\n    if (err) {\n      console.error(err);\n      return res.status(500).json({ success: false, message: 'An error occurred, please try again later' });\n    }\n\n    return res.json({\n      success: true,\n      data: {\n        id: this.lastID,\n        title,\n        content,\n      },\n    });\n  });\n});\n\napp.delete('/notes/:id', (req, res) => {\n  const { id } = req.params;\n\n  db.get('SELECT * FROM notes WHERE id = ?', [id], (err, row) => {\n    if (err) {\n      console.error(err);\n      return res.status(500).json({ success: false, message: 'An error occurred, please try again later' });\n    }\n\n    if (!row) {\n      return res.status(404).json({ success: false, message: 'Note does not exist' });\n    }\n\n    db.run('DELETE FROM notes WHERE id = ?', [id], (error) => {\n      if (error) {\n        console.error(error);\n        return res.status(500).json({ success: false, message: 'An error occurred, please try again later' });\n      }\n\n      return res.json({ success: true, message: 'Note deleted successfully' });\n    });\n  });\n});\n\nBriefly, this creates 4 endpoints:\n\n 1. /notes endpoint of the method GET to fetch all notes.\n 2. /notes/:id endpoint of the method GET to fetch a note by an ID.\n 3. /notes endpoint of the method POST to add a note.\n 4. /notes/:id endpoint of the method DELETE to delete a note.\n\n\nTest Server\n\nRun the following command to start the server:\n\nnpm start\n\nThis starts the server on port 3001. You can test it out by sending a request to localhost:3001/notes.\n\n\nWebsite Setup\n\nIn this section, you'll create the website with Create React App (CRA). This is where you'll make use of React Query.\n\n\nCreate Website Project\n\nTo create a new React app, run the following command in a different directory:\n\nnpx create-react-app website\n\nThis creates a new React app in the directory website.\n\n\nInstall Dependencies\n\nRun the following command to change to the website directory and install the necessary dependencies for the website:\n\ncd website\nnpm i @tanstack/react-query tailwindcss postcss autoprefixer @tailwindcss/typography @heroicons/react @windmill/react-ui\n\nThe @tanstack/react-query library is the React Query library which is now named TanStack Query. The other libraries are Tailwind CSS related libraries to add styling to the website.\n\n\nTailwind CSS Setup\n\nThis section is optional and is only used to set up Tailwind CSS.\n\nCreate the file postcss.config.js with the following content:\n\nmodule.exports = {\n  plugins: {\n    tailwindcss: {},\n    autoprefixer: {},\n  },\n}\n\n\nAlso, create the file tailwind.config.js with the following content:\n\n/** @type {import('tailwindcss').Config} */\nmodule.exports = {\n  content: [\n    \"./src/**/*.{js,jsx,ts,tsx}\",\n  ],\n  theme: {\n    extend: {},\n  },\n  plugins: [\n    require('@tailwindcss/typography')\n  ],\n}\n\n\nThen, create the file src/index.css with the following content:\n\n@tailwind base;\n@tailwind components;\n@tailwind utilities;\n\nFinally, in index.js import src/index.css at the beginning of the file:\n\nimport './index.css';\n\n\nUse QueryClientProvider\n\nTo use the React Query client in all of your components, you must use it at a high level in your website's components hierarchy. The best place to put it is in src/index.js which wraps up your entire website's components.\n\nIn src/index.js add the following imports at the beginning of the file:\n\nimport {\n  QueryClient,\n  QueryClientProvider,\n} from '@tanstack/react-query'\n\nThen, initialize a new Query client:\n\nconst queryClient = new QueryClient()\n\nFinally, change the parameter passed to root.render:\n\nroot.render(\n  <QueryClientProvider client={queryClient}>\n    <App />\n  </QueryClientProvider>\n);\n\nThis wraps the App component which holds the rest of the website's components with QueryClientProvider. This provider accepts the prop client which is an instance of QueryClient.\n\nNow, all components within the website will have access to the Query Client which is used to fetch, cache, and manipulate the server data.\n\n\nImplement Display Notes\n\nFetching data from the server is an act of performing a query. Therefore, you'll use useQuery in this section.\n\nYou'll display notes in the App component. These notes are fetched from the server using the /notes endpoint.\n\nReplace the content of app.js with the following content:\n\nimport { PlusIcon, RefreshIcon } from '@heroicons/react/solid'\nimport { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'\n\nfunction App() {\n  const { isLoading, isError, data, error } = useQuery(['notes'], fetchNotes)\n\n  function fetchNotes () {\n    return fetch('http://localhost:3001/notes')\n    .then((response) => response.json())\n    .then(({ success, data }) => {\n      if (!success) {\n        throw new Error ('An error occurred while fetching notes');\n      }\n      return data;\n    })\n  }\n\n  return (\n    <div className=\"w-screen h-screen overflow-x-hidden bg-red-400 flex flex-col justify-center items-center\">\n      <div className='bg-white w-full md:w-1/2 p-5 text-center rounded shadow-md text-gray-800 prose'>\n        <h1>Notes</h1>\n        {isLoading && <RefreshIcon className=\"w-10 h-10 animate-spin mx-auto\"></RefreshIcon>}\n        {isError && <span className='text-red'>{error.message ? error.message : error}</span>}\n        {!isLoading && !isError && data && !data.length && <span className='text-red-400'>You have no notes</span>}\n        {data && data.length > 0 && data.map((note, index) => (\n          <div key={note.id} className={`text-left ${index !== data.length - 1 ? 'border-b pb-2' : ''}`}>\n            <h2>{note.title}</h2>\n            <p>{note.content}</p>\n            <span>\n              <button className='link text-gray-400'>Delete</button>\n            </span>\n          </div>\n        ))}\n      </div>\n      <button className=\"mt-2 bg-gray-700 hover:bg-gray-600 rounded-full text-white p-3\">\n        <PlusIcon className='w-5 h-5'></PlusIcon>\n      </button>\n    </div>\n  );\n}\n\nexport default App;\n\nHere's briefly what's going on in this code snippet:\n\n 1. You use useQuery to fetch the notes. The first parameter it accepts is a unique key used for caching. The second parameter is the function used to fetch the data. You pass it the fetchNotes function.\n 2. useQuery returns an object that holds many variables. Here, you use 4 of them: isLoading is a boolean value that determines whether the data is currently being fetched; isError is a boolean value that determines if an error occurred. data is the data that is fetched from the server; and error is the error message if isError is true.\n 3. The fetchNotes function must return a promise that either resolves data or throws an error. In the function, you send a GET request to localhost:3001/notes to fetch the notes. If the data is fetched successfully it is returned in the then fulfillment function.\n 4. In the returned JSX, if isLoading is true, a loading icon is shown. If isError is true, an error message is shown. If data is fetched successfully and has any data in it, the notes are rendered.\n 5. You also show a button with a plus icon to add new notes. You'll implement this later.\n\n\nTest Displaying Notes\n\nTo test out what you've implemented so far, make sure your server is still running, then start your React app server with the following command:\n\nnpm start\n\nThis runs your React app on localhost:3000 by default. If you open it in your browser, you'll see a loading icon at first then you'll see no notes as you haven't added any yet.\n\n\nImplement Add Notes Functionality\n\nAdding a note is an act of mutation on the server data. Therefore, you'll be using the useMutation hook in this section.\n\nYou'll create a separate component that shows the form used to add a note.\n\nCreate the file src/form.js with the following content:\n\nimport { useMutation, useQueryClient } from '@tanstack/react-query'\n\nimport { useState } from 'react'\n\nexport default function Form ({ isOpen, setIsOpen }) {\n  const [title, setTitle] = useState(\"\")\n  const [content, setContent] = useState(\"\")\n  const queryClient = useQueryClient()\n\n  const mutation = useMutation(insertNote, {\n    onSuccess: () => {\n      setTitle(\"\")\n      setContent(\"\")\n    }\n  })\n\n  function closeForm (e) {\n    e.preventDefault()\n    setIsOpen(false)\n  }\n\n  function insertNote () {\n    return fetch(`http://localhost:3001/notes`, {\n      method: 'POST',\n      headers: {\n        'Content-Type': 'application/json'\n      },\n      body: JSON.stringify({\n        title,\n        content\n      })\n    })\n    .then((response) => response.json())\n    .then(({ success, data }) => {\n      if (!success) {\n        throw new Error(\"An error occured\")\n      }\n      \n      setIsOpen(false)\n      queryClient.setQueriesData('notes', (old) => [...old, data])\n    })\n  }\n\n  function handleSubmit (e) {\n    e.preventDefault()\n    mutation.mutate()\n  }\n\n  return (\n    <div className={`absolute w-full h-full top-0 left-0 z-50 flex justify-center items-center ${!isOpen ? 'hidden' : ''}`}>\n      <div className='bg-black opacity-50 absolute w-full h-full top-0 left-0'></div>\n      <form className='bg-white w-full md:w-1/2 p-5 rounded shadow-md text-gray-800 prose relative' \n        onSubmit={handleSubmit}>\n        <h2 className='text-center'>Add Note</h2>\n        {mutation.isError && <span className='block mb-2 text-red-400'>{mutation.error.message ? mutation.error.message : mutation.error}</span>}\n        <input type=\"text\" placeholder='Title' className='rounded-sm w-full border px-2' \n          value={title} onChange={(e) => setTitle(e.target.value)} />\n        <textarea onChange={(e) => setContent(e.target.value)} \n          className=\"rounded-sm w-full border px-2 mt-2\" placeholder='Content' value={content}></textarea>\n        <div>\n          <button type=\"submit\" className='mt-2 bg-red-400 hover:bg-red-600 text-white p-3 rounded mr-2 disabled:pointer-events-none' \n            disabled={mutation.isLoading}>\n            Add</button>\n          <button className='mt-2 bg-gray-700 hover:bg-gray-600 text-white p-3 rounded'\n            onClick={closeForm}>Cancel</button>\n        </div>\n      </form>\n    </div>\n  )\n}\n\nHere's a brief explanation of this form\n\n 1. This form acts as a pop-up. It accepts isOpen and setIsOpen props to determine when the form is opened and handle closing it.\n 2. You use useQueryClient to get access to the Query Client. This is necessary to perform a mutation.\n 3. To handle adding a note on your server and keep all data in your query client synced, you must the useMutation hook.\n 4. The useMutation hook accepts 2 parameters. Thie first one is the function that will handle the mutation, which in this case is insertNote. The second parameter is an object of options. You pass it one option onSuccess which is a function that runs if the mutation is performed successfully. You use this to reset the title and content fields of the form.\n 5. In insertNote, you send a POST request to localhost:3001/notes and pass in the body the title and content of the note to be created. If the success body parameter returned from the server is false, an error is thrown to signal that the mutation failed.\n 6. If the note is added successfully, you change the cached value of the notes key using the queryClient.setQueriesData method. This method accepts the key as a first parameter and the new data associated with that key as a second parameter. This updates the data everywhere it's used on your website.\n 7. In this component you display a form with 2 fields: title and content. In the form, you check if an error occurs using mutation.isError and get access to the error using mutation.error.\n 8. You handle form submission in the handleSubmit function. Here, you trigger the mutation using mutation.mutate. This is where the insertNote function is triggered to add a new note.\n\nThen, in src/app.js add the following imports at the beginning of the file:\n\nimport Form from './form'\nimport { useState } from 'react'\n\nThen, at the beginning of the component add a new state variable to manage wheter the form is opened or not:\n\nconst [isOpen, setIsOpen] = useState(false)\n\nNext, add a new function addNote that just uses setIsOpen to open the form:\n\nfunction addNote () {\n    setIsOpen(true)\n}\n\nFinally, in the returned JSX, replace the button with the plus icon with the following:\n\n<button className=\"mt-2 bg-gray-700 hover:bg-gray-600 rounded-full text-white p-3\" onClick={addNote}>\n    <PlusIcon className='w-5 h-5'></PlusIcon>\n</button>\n<Form isOpen={isOpen} setIsOpen={setIsOpen} />\n\nThis sets the onClick handler of the button to addNote. It also adds the Form component you created earlier as a child component of App.\n\n\nTest Adding a Note\n\nRerun your server and React app if they're not running. Then, open the website again at localhost:3000. Click on the plus button and a pop up will open with the form to add a new note.\n\nEnter a random title and content then click Add. The pop up form will then close and you can see the new note added.\n\n\nImplement Delete Note Functionality\n\nThe last functionality you'll add is deleting notes. Deleting a note is another act of mutation as it manipulates the server's data.\n\nAt the beginning of the App component in src/app.js add the following code:\n\nconst queryClient = useQueryClient()\nconst mutation = useMutation(deleteNote, {\n    onSuccess: () => queryClient.invalidateQueries('notes')\n})\n\nHere, you get access to the query client using useQueryClient. Then, you create a new mutation using useMutation. You pass it the function deleteNote (which you'll create next) as a first parameter and an object of options.\n\nTo the onSuccess option you pass a function that does one thing. It executes the method queryClient.invalidateQueries. This method marks the cached data for a specific key as outdated, which triggers retrieving the data again.\n\nSo, once a note is deleted, the query you created earlier that executes the function fetchNotes will be triggered and the notes will be fetched again. If you had created other queries on your website that use the same key notes, they'll also be triggered to update their data.\n\nNext, add the function deleteNote in the App component in the same file:\n\nfunction deleteNote (note) {\n    return fetch(`http://localhost:3001/notes/${note.id}`, {\n      method: 'DELETE'\n    })\n    .then((response) => response.json())\n    .then(({ success, message }) => {\n      if (!success) {\n        throw new Error(message);\n      }\n\n      alert(message);\n    })\n  }\n\nThis function receives the note to be deleted as a parameter. It sends a DELETE request to localhost:3001/notes/:id. If the success body parameter of the response is false, an error is thrown. Otherwise, only an alert is shown.\n\nThen, in the returned JSX of the App component, change how the loading icon and error where shown previously to the following:\n\n{(isLoading || mutation.isLoading) && <RefreshIcon className=\"w-10 h-10 animate-spin mx-auto\"></RefreshIcon>}\n{(isError || mutation.isError) && <span className='text-red'>{error ? (error.message ? error.message : error) : mutation.error.message}</span>}\n\nThis shows the loading icon or the error message for both the query that fetches the notes and the mutation that handles deleting a note.\n\nFinally, find the delete button of a note and add an onClick handler:\n\n<button className='link text-gray-400' onClick={() => mutation.mutate(note)}>Delete</button>\n\nOn click, the mutation responsible for deleting the note is triggered using mutation.mutate. You pass it the note to delete which is the current note in a map loop.\n\n\nTest Deleting a Note\n\nRerun your server and React app if they're not running. Then, open the website again at localhost:3000. Click the Delete link for any of your notes. If the note is deleted successfully, an alert will be shown.\n\nAfter closing the alert, the notes will be fetched again and displayed, if there are any other notes.\n\n\nConclusion\n\nUsing React (TanStack) Query, you can easily handle server data fetching and manipulation on your website with advanced features such as caching and synchronization across your React app.\n\nMake sure to check out the official documentation to learn more about what you can do with React Query.","html":"<p>React Query (now rebranded to TanStack Query) is a React library used to make fetching and manipulating server-side data easier. Using React Query, you can implement, along with data fetching, caching, and synchronization of your data with the server.</p><p>In this tutorial, you'll build a simple Node.js server and then learn how to interact with it on a React website using React Query.</p><p>Please note that this version uses v4 of React Query which is now named TanStack Query.</p><p>You can find the code for this tutorial in <a href=\"https://github.com/shahednasser/react-query-tutorial\">this GitHub repository</a>.</p><h2 id=\"prerequisites\">Prerequisites</h2><p> Before starting with this tutorial make sure you have <a href=\"https://nodejs.org/en/\">Node.js installed</a>. You need at least version 14.</p><h2 id=\"server-setup\">Server Setup</h2><p>In this section, you'll set up a simple Node.js server with an SQLite database. The server has 3 endpoints to fetch, add, and delete notes.</p><p>If you already have a server you can skip this section and go to the Website Setup section.</p><h3 id=\"create-server-project\">Create Server Project</h3><p>Create a new directory called <code>server</code> then initialize a new project using NPM:</p><pre><code class=\"language-bash\">mkdir server\ncd server\nnpm init -y</code></pre><h3 id=\"install-dependencies\">Install Dependencies</h3><p>Then, install the packages you'll need for the development of the server:</p><pre><code class=\"language-bash\">npm i express cors body-parser sqlite3 nodemon</code></pre><p>Here's what each of the packages is for:</p><ol><li><code>express</code> to create a server using <a href=\"https://expressjs.com/\">Express</a>.</li><li><code>cors</code> is an Express middleware used to handle <a href=\"https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS\">CORS</a> on your server.</li><li><code>body-parser</code> is an Express middleware used to parse the body of a request.</li><li><code>sqlite3</code> is an SQLite database adapter for Node.js.</li><li><code>nodemon</code> is a library used to restart the server whenever new changes occur to the files.</li></ol><h3 id=\"create-server\">Create Server</h3><p>Create the file <code>index.js</code> with the following content:</p><pre><code class=\"language-js\">const express = require('express');\n\nconst app = express();\nconst port = 3001;\nconst cors = require('cors');\nconst sqlite3 = require('sqlite3').verbose();\nconst bodyParser = require('body-parser');\n\napp.use(bodyParser.json());\napp.use(cors());\n\napp.listen(port, () =&gt; {\n  console.log(`Notes app listening on port ${port}`);\n});</code></pre><p>This initializes the server using Express on port <code>3001</code>. It also uses the <code>cors</code> and <code>body-parser</code> middleware.</p><p>Then, in <code>package.json</code> add a new script <code>start</code> to run the server:</p><pre><code class=\"language-json\">  \"scripts\": {\n    \"start\": \"nodemon index.js\"\n  },</code></pre><h3 id=\"initialize-the-database\">Initialize the Database</h3><p>In <code>index.js</code> before <code>app.listen</code> add the following code:</p><pre><code class=\"language-js\">const db = new sqlite3.Database('data.db', (err) =&gt; {\n  if (err) {\n    throw err;\n  }\n\n  // create tables if they don't exist\n  db.serialize(() =&gt; {\n    db.run(`CREATE TABLE IF NOT EXISTS notes (id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT, content TEXT, \n      created_at datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, updated_at datetime NOT NULL DEFAULT CURRENT_TIMESTAMP)`);\n  });\n});</code></pre><p>This creates a new database if it doesn't exist in the file <code>data.db</code>. Then, if the <code>notes</code> table doesn't exist on the database it creates it as well.</p><h3 id=\"add-endpoints\">Add Endpoints</h3><p>Following the database code, add the following code to add the endpoints:</p><pre><code class=\"language-js\">app.get('/notes', (req, res) =&gt; {\n  db.all('SELECT * FROM notes', (err, rows) =&gt; {\n    if (err) {\n      console.error(err);\n      return res.status(500).json({ success: false, message: 'An error occurred, please try again later' });\n    }\n\n    return res.json({ success: true, data: rows });\n  });\n});\n\napp.get('/notes/:id', (req, res) =&gt; {\n  db.get('SELECT * FROM notes WHERE id = ?', req.params.id, (err, row) =&gt; {\n    if (err) {\n      console.error(err);\n      return res.status(500).json({ success: false, message: 'An error occurred, please try again later' });\n    }\n\n    if (!row) {\n      return res.status(404).json({ success: false, message: 'Note does not exist' });\n    }\n\n    return res.json({ success: true, data: row });\n  });\n});\n\napp.post('/notes', (req, res) =&gt; {\n  const { title, content } = req.body;\n\n  if (!title || !content) {\n    return res.status(400).json({ success: false, message: 'title and content are required' });\n  }\n\n  db.run('INSERT INTO notes (title, content) VALUES (?, ?)', [title, content], function (err) {\n    if (err) {\n      console.error(err);\n      return res.status(500).json({ success: false, message: 'An error occurred, please try again later' });\n    }\n\n    return res.json({\n      success: true,\n      data: {\n        id: this.lastID,\n        title,\n        content,\n      },\n    });\n  });\n});\n\napp.delete('/notes/:id', (req, res) =&gt; {\n  const { id } = req.params;\n\n  db.get('SELECT * FROM notes WHERE id = ?', [id], (err, row) =&gt; {\n    if (err) {\n      console.error(err);\n      return res.status(500).json({ success: false, message: 'An error occurred, please try again later' });\n    }\n\n    if (!row) {\n      return res.status(404).json({ success: false, message: 'Note does not exist' });\n    }\n\n    db.run('DELETE FROM notes WHERE id = ?', [id], (error) =&gt; {\n      if (error) {\n        console.error(error);\n        return res.status(500).json({ success: false, message: 'An error occurred, please try again later' });\n      }\n\n      return res.json({ success: true, message: 'Note deleted successfully' });\n    });\n  });\n});</code></pre><p>Briefly, this creates 4 endpoints:</p><ol><li><code>/notes</code> endpoint of the method <code>GET</code> to fetch all notes.</li><li><code>/notes/:id</code> endpoint of the method <code>GET</code> to fetch a note by an ID.</li><li><code>/notes</code> endpoint of the method <code>POST</code> to add a note.</li><li><code>/notes/:id</code> endpoint of the method <code>DELETE</code> to delete a note.</li></ol><h3 id=\"test-server\">Test Server</h3><p>Run the following command to start the server:</p><pre><code class=\"language-bash\">npm start</code></pre><p>This starts the server on port <code>3001</code>. You can test it out by sending a request to <code>localhost:3001/notes</code>.</p><h2 id=\"website-setup\">Website Setup</h2><p>In this section, you'll create the website with Create React App (CRA). This is where you'll make use of React Query.</p><h3 id=\"create-website-project\">Create Website Project</h3><p>To create a new React app, run the following command in a different directory:</p><pre><code class=\"language-bash\">npx create-react-app website</code></pre><p>This creates a new React app in the directory <code>website</code>.</p><h3 id=\"install-dependencies-1\">Install Dependencies</h3><p>Run the following command to change to the <code>website</code> directory and install the necessary dependencies for the website:</p><pre><code class=\"language-bash\">cd website\nnpm i @tanstack/react-query tailwindcss postcss autoprefixer @tailwindcss/typography @heroicons/react @windmill/react-ui</code></pre><p>The <code>@tanstack/react-query</code> library is the React Query library which is now named TanStack Query. The other libraries are <a href=\"https://tailwindcss.com/\">Tailwind CSS</a> related libraries to add styling to the website.</p><h3 id=\"tailwind-css-setup\">Tailwind CSS Setup</h3><p>This section is optional and is only used to set up Tailwind CSS.</p><p>Create the file <code>postcss.config.js</code> with the following content:</p><pre><code class=\"language-js\">module.exports = {\n  plugins: {\n    tailwindcss: {},\n    autoprefixer: {},\n  },\n}\n</code></pre><p>Also, create the file <code>tailwind.config.js</code> with the following content:</p><pre><code class=\"language-js\">/** @type {import('tailwindcss').Config} */\nmodule.exports = {\n  content: [\n    \"./src/**/*.{js,jsx,ts,tsx}\",\n  ],\n  theme: {\n    extend: {},\n  },\n  plugins: [\n    require('@tailwindcss/typography')\n  ],\n}\n</code></pre><p>Then, create the file <code>src/index.css</code> with the following content:</p><pre><code class=\"language-css\">@tailwind base;\n@tailwind components;\n@tailwind utilities;</code></pre><p>Finally, in <code>index.js</code> import <code>src/index.css</code> at the beginning of the file:</p><pre><code class=\"language-js\">import './index.css';</code></pre><h3 id=\"use-queryclientprovider\">Use QueryClientProvider</h3><p>To use the React Query client in all of your components, you must use it at a high level in your website's components hierarchy. The best place to put it is in <code>src/index.js</code> which wraps up your entire website's components.</p><p>In <code>src/index.js</code> add the following imports at the beginning of the file:</p><pre><code class=\"language-js\">import {\n  QueryClient,\n  QueryClientProvider,\n} from '@tanstack/react-query'</code></pre><p>Then, initialize a new Query client:</p><pre><code class=\"language-js\">const queryClient = new QueryClient()</code></pre><p>Finally, change the parameter passed to <code>root.render</code>:</p><pre><code class=\"language-js\">root.render(\n  &lt;QueryClientProvider client={queryClient}&gt;\n    &lt;App /&gt;\n  &lt;/QueryClientProvider&gt;\n);</code></pre><p>This wraps the <code>App</code> component which holds the rest of the website's components with <code>QueryClientProvider</code>. This provider accepts the prop <code>client</code> which is an instance of <code>QueryClient</code>.</p><p>Now, all components within the website will have access to the Query Client which is used to fetch, cache, and manipulate the server data.</p><h3 id=\"implement-display-notes\">Implement Display Notes</h3><p>Fetching data from the server is an act of performing a query. Therefore, you'll use <code>useQuery</code> in this section.</p><p>You'll display notes in the <code>App</code> component. These notes are fetched from the server using the <code>/notes</code> endpoint.</p><p>Replace the content of <code>app.js</code> with the following content:</p><pre><code class=\"language-js\">import { PlusIcon, RefreshIcon } from '@heroicons/react/solid'\nimport { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'\n\nfunction App() {\n  const { isLoading, isError, data, error } = useQuery(['notes'], fetchNotes)\n\n  function fetchNotes () {\n    return fetch('http://localhost:3001/notes')\n    .then((response) =&gt; response.json())\n    .then(({ success, data }) =&gt; {\n      if (!success) {\n        throw new Error ('An error occurred while fetching notes');\n      }\n      return data;\n    })\n  }\n\n  return (\n    &lt;div className=\"w-screen h-screen overflow-x-hidden bg-red-400 flex flex-col justify-center items-center\"&gt;\n      &lt;div className='bg-white w-full md:w-1/2 p-5 text-center rounded shadow-md text-gray-800 prose'&gt;\n        &lt;h1&gt;Notes&lt;/h1&gt;\n        {isLoading &amp;&amp; &lt;RefreshIcon className=\"w-10 h-10 animate-spin mx-auto\"&gt;&lt;/RefreshIcon&gt;}\n        {isError &amp;&amp; &lt;span className='text-red'&gt;{error.message ? error.message : error}&lt;/span&gt;}\n        {!isLoading &amp;&amp; !isError &amp;&amp; data &amp;&amp; !data.length &amp;&amp; &lt;span className='text-red-400'&gt;You have no notes&lt;/span&gt;}\n        {data &amp;&amp; data.length &gt; 0 &amp;&amp; data.map((note, index) =&gt; (\n          &lt;div key={note.id} className={`text-left ${index !== data.length - 1 ? 'border-b pb-2' : ''}`}&gt;\n            &lt;h2&gt;{note.title}&lt;/h2&gt;\n            &lt;p&gt;{note.content}&lt;/p&gt;\n            &lt;span&gt;\n              &lt;button className='link text-gray-400'&gt;Delete&lt;/button&gt;\n            &lt;/span&gt;\n          &lt;/div&gt;\n        ))}\n      &lt;/div&gt;\n      &lt;button className=\"mt-2 bg-gray-700 hover:bg-gray-600 rounded-full text-white p-3\"&gt;\n        &lt;PlusIcon className='w-5 h-5'&gt;&lt;/PlusIcon&gt;\n      &lt;/button&gt;\n    &lt;/div&gt;\n  );\n}\n\nexport default App;</code></pre><p>Here's briefly what's going on in this code snippet:</p><ol><li>You use <code>useQuery</code> to fetch the notes. The first parameter it accepts is a unique key used for caching. The second parameter is the function used to fetch the data. You pass it the <code>fetchNotes</code> function.</li><li><code>useQuery</code> returns an object that holds <a href=\"https://tanstack.com/query/v4/docs/reference/useQuery\">many variables</a>. Here, you use 4 of them: <code>isLoading</code> is a boolean value that determines whether the data is currently being fetched; <code>isError</code> is a boolean value that determines if an error occurred. <code>data</code> is the data that is fetched from the server; and <code>error</code> is the error message if <code>isError</code> is true.</li><li>The <code>fetchNotes</code> function must return a promise that either resolves data or throws an error. In the function, you send a <code>GET</code> request to <code>localhost:3001/notes</code> to fetch the notes. If the data is fetched successfully it is returned in the <code>then</code> fulfillment function. </li><li>In the returned JSX, if <code>isLoading</code> is true, a loading icon is shown. If <code>isError</code> is true, an error message is shown. If <code>data</code> is fetched successfully and has any data in it, the notes are rendered.</li><li>You also show a button with a plus icon to add new notes. You'll implement this later.</li></ol><h3 id=\"test-displaying-notes\">Test Displaying Notes</h3><p>To test out what you've implemented so far, make sure your server is still running, then start your React app server with the following command:</p><pre><code class=\"language-bash\">npm start</code></pre><p>This runs your React app on <code>localhost:3000</code> by default. If you open it in your browser, you'll see a loading icon at first then you'll see no notes as you haven't added any yet.</p><figure class=\"kg-card kg-image-card\"><img src=\"https://backend.shahednasser.com/content/images/2022/07/Screen-Shot-2022-06-28-at-9.10.04-PM.png\" class=\"kg-image\" alt loading=\"lazy\" width=\"1446\" height=\"490\" srcset=\"https://backend.shahednasser.com/content/images/size/w600/2022/07/Screen-Shot-2022-06-28-at-9.10.04-PM.png 600w, https://backend.shahednasser.com/content/images/size/w1000/2022/07/Screen-Shot-2022-06-28-at-9.10.04-PM.png 1000w, https://backend.shahednasser.com/content/images/2022/07/Screen-Shot-2022-06-28-at-9.10.04-PM.png 1446w\" sizes=\"(min-width: 720px) 720px\"></figure><h3 id=\"implement-add-notes-functionality\">Implement Add Notes Functionality</h3><p>Adding a note is an act of mutation on the server data. Therefore, you'll be using the <code>useMutation</code> hook in this section.</p><p>You'll create a separate component that shows the form used to add a note.</p><p>Create the file <code>src/form.js</code> with the following content:</p><pre><code class=\"language-js\">import { useMutation, useQueryClient } from '@tanstack/react-query'\n\nimport { useState } from 'react'\n\nexport default function Form ({ isOpen, setIsOpen }) {\n  const [title, setTitle] = useState(\"\")\n  const [content, setContent] = useState(\"\")\n  const queryClient = useQueryClient()\n\n  const mutation = useMutation(insertNote, {\n    onSuccess: () =&gt; {\n      setTitle(\"\")\n      setContent(\"\")\n    }\n  })\n\n  function closeForm (e) {\n    e.preventDefault()\n    setIsOpen(false)\n  }\n\n  function insertNote () {\n    return fetch(`http://localhost:3001/notes`, {\n      method: 'POST',\n      headers: {\n        'Content-Type': 'application/json'\n      },\n      body: JSON.stringify({\n        title,\n        content\n      })\n    })\n    .then((response) =&gt; response.json())\n    .then(({ success, data }) =&gt; {\n      if (!success) {\n        throw new Error(\"An error occured\")\n      }\n      \n      setIsOpen(false)\n      queryClient.setQueriesData('notes', (old) =&gt; [...old, data])\n    })\n  }\n\n  function handleSubmit (e) {\n    e.preventDefault()\n    mutation.mutate()\n  }\n\n  return (\n    &lt;div className={`absolute w-full h-full top-0 left-0 z-50 flex justify-center items-center ${!isOpen ? 'hidden' : ''}`}&gt;\n      &lt;div className='bg-black opacity-50 absolute w-full h-full top-0 left-0'&gt;&lt;/div&gt;\n      &lt;form className='bg-white w-full md:w-1/2 p-5 rounded shadow-md text-gray-800 prose relative' \n        onSubmit={handleSubmit}&gt;\n        &lt;h2 className='text-center'&gt;Add Note&lt;/h2&gt;\n        {mutation.isError &amp;&amp; &lt;span className='block mb-2 text-red-400'&gt;{mutation.error.message ? mutation.error.message : mutation.error}&lt;/span&gt;}\n        &lt;input type=\"text\" placeholder='Title' className='rounded-sm w-full border px-2' \n          value={title} onChange={(e) =&gt; setTitle(e.target.value)} /&gt;\n        &lt;textarea onChange={(e) =&gt; setContent(e.target.value)} \n          className=\"rounded-sm w-full border px-2 mt-2\" placeholder='Content' value={content}&gt;&lt;/textarea&gt;\n        &lt;div&gt;\n          &lt;button type=\"submit\" className='mt-2 bg-red-400 hover:bg-red-600 text-white p-3 rounded mr-2 disabled:pointer-events-none' \n            disabled={mutation.isLoading}&gt;\n            Add&lt;/button&gt;\n          &lt;button className='mt-2 bg-gray-700 hover:bg-gray-600 text-white p-3 rounded'\n            onClick={closeForm}&gt;Cancel&lt;/button&gt;\n        &lt;/div&gt;\n      &lt;/form&gt;\n    &lt;/div&gt;\n  )\n}</code></pre><p>Here's a brief explanation of this form</p><ol><li>This form acts as a pop-up. It accepts <code>isOpen</code> and <code>setIsOpen</code> props to determine when the form is opened and handle closing it.</li><li>You use <code>useQueryClient</code> to get access to the Query Client. This is necessary to perform a mutation.</li><li>To handle adding a note on your server and keep all data in your query client synced, you must the <code>useMutation</code> hook. </li><li>The <code>useMutation</code> hook accepts 2 parameters. Thie first one is the function that will handle the mutation, which in this case is <code>insertNote</code>. The second parameter is an object of <a href=\"https://tanstack.com/query/v4/docs/reference/useMutation\">options</a>. You pass it one option <code>onSuccess</code> which is a function that runs if the mutation is performed successfully. You use this to reset the <code>title</code> and <code>content</code> fields of the form.</li><li>In <code>insertNote</code>, you send a <code>POST</code> request to <code>localhost:3001/notes</code> and pass in the body the <code>title</code> and <code>content</code> of the note to be created. If the <code>success</code> body parameter returned from the server is <code>false</code>, an error is thrown to signal that the mutation failed. </li><li>If the note is added successfully, you change the cached value of the <code>notes</code> key using the <code>queryClient.setQueriesData</code> method. This method accepts the key as a first parameter and the new data associated with that key as a second parameter. This updates the data everywhere it's used on your website.</li><li>In this component you display a form with 2 fields: <code>title</code> and <code>content</code>. In the form, you check if an error occurs using <code>mutation.isError</code> and get access to the error using <code>mutation.error</code>.</li><li>You handle form submission in the <code>handleSubmit</code> function. Here, you trigger the mutation using <code>mutation.mutate</code>. This is where the <code>insertNote</code> function is triggered to add a new note.</li></ol><p>Then, in <code>src/app.js</code> add the following imports at the beginning of the file:</p><pre><code class=\"language-js\">import Form from './form'\nimport { useState } from 'react'</code></pre><p>Then, at the beginning of the component add a new state variable to manage wheter the form is opened or not:</p><pre><code class=\"language-js\">const [isOpen, setIsOpen] = useState(false)</code></pre><p>Next, add a new function <code>addNote</code> that just uses <code>setIsOpen</code> to open the form:</p><pre><code class=\"language-js\">function addNote () {\n    setIsOpen(true)\n}</code></pre><p>Finally, in the returned JSX, replace the button with the plus icon with the following:</p><pre><code class=\"language-js\">&lt;button className=\"mt-2 bg-gray-700 hover:bg-gray-600 rounded-full text-white p-3\" onClick={addNote}&gt;\n    &lt;PlusIcon className='w-5 h-5'&gt;&lt;/PlusIcon&gt;\n&lt;/button&gt;\n&lt;Form isOpen={isOpen} setIsOpen={setIsOpen} /&gt;</code></pre><p>This sets the <code>onClick</code> handler of the button to <code>addNote</code>. It also adds the <code>Form</code> component you created earlier as a child component of <code>App</code>. </p><h3 id=\"test-adding-a-note\">Test Adding a Note</h3><p>Rerun your server and React app if they're not running. Then, open the website again at <code>localhost:3000</code>. Click on the plus button and a pop up will open with the form to add a new note.</p><figure class=\"kg-card kg-image-card\"><img src=\"https://backend.shahednasser.com/content/images/2022/07/Screen-Shot-2022-07-24-at-6.31.41-PM.png\" class=\"kg-image\" alt loading=\"lazy\" width=\"1494\" height=\"792\" srcset=\"https://backend.shahednasser.com/content/images/size/w600/2022/07/Screen-Shot-2022-07-24-at-6.31.41-PM.png 600w, https://backend.shahednasser.com/content/images/size/w1000/2022/07/Screen-Shot-2022-07-24-at-6.31.41-PM.png 1000w, https://backend.shahednasser.com/content/images/2022/07/Screen-Shot-2022-07-24-at-6.31.41-PM.png 1494w\" sizes=\"(min-width: 720px) 720px\"></figure><p>Enter a random title and content then click Add. The pop up form will then close and you can see the new note added.</p><figure class=\"kg-card kg-image-card\"><img src=\"https://backend.shahednasser.com/content/images/2022/07/Screen-Shot-2022-07-24-at-6.32.53-PM.png\" class=\"kg-image\" alt loading=\"lazy\" width=\"1418\" height=\"720\" srcset=\"https://backend.shahednasser.com/content/images/size/w600/2022/07/Screen-Shot-2022-07-24-at-6.32.53-PM.png 600w, https://backend.shahednasser.com/content/images/size/w1000/2022/07/Screen-Shot-2022-07-24-at-6.32.53-PM.png 1000w, https://backend.shahednasser.com/content/images/2022/07/Screen-Shot-2022-07-24-at-6.32.53-PM.png 1418w\" sizes=\"(min-width: 720px) 720px\"></figure><h3 id=\"implement-delete-note-functionality\">Implement Delete Note Functionality</h3><p>The last functionality you'll add is deleting notes. Deleting a note is another act of mutation as it manipulates the server's data.</p><p>At the beginning of the <code>App</code> component in <code>src/app.js</code> add the following code:</p><pre><code class=\"language-js\">const queryClient = useQueryClient()\nconst mutation = useMutation(deleteNote, {\n    onSuccess: () =&gt; queryClient.invalidateQueries('notes')\n})</code></pre><p>Here, you get access to the query client using <code>useQueryClient</code>. Then, you create a new mutation using <code>useMutation</code>. You pass it the function <code>deleteNote</code> (which you'll create next) as a first parameter and an object of options. </p><p>To the <code>onSuccess</code> option you pass a function that does one thing. It executes the method <code>queryClient.invalidateQueries</code>. This method marks the cached data for a specific key as outdated, which triggers retrieving the data again. </p><p>So, once a note is deleted, the query you created earlier that executes the function <code>fetchNotes</code> will be triggered and the notes will be fetched again. If you had created other queries on your website that use the same key <code>notes</code>, they'll also be triggered to update their data. </p><p>Next, add the function <code>deleteNote</code> in the <code>App</code> component in the same file:</p><pre><code class=\"language-js\">function deleteNote (note) {\n    return fetch(`http://localhost:3001/notes/${note.id}`, {\n      method: 'DELETE'\n    })\n    .then((response) =&gt; response.json())\n    .then(({ success, message }) =&gt; {\n      if (!success) {\n        throw new Error(message);\n      }\n\n      alert(message);\n    })\n  }</code></pre><p>This function receives the <code>note</code> to be deleted as a parameter. It sends a <code>DELETE</code> request to <code>localhost:3001/notes/:id</code>. If the <code>success</code> body parameter of the response is <code>false</code>, an error is thrown. Otherwise, only an alert is shown.</p><p>Then, in the returned JSX of the <code>App</code> component, change how the loading icon and error where shown previously to the following:</p><pre><code class=\"language-js\">{(isLoading || mutation.isLoading) &amp;&amp; &lt;RefreshIcon className=\"w-10 h-10 animate-spin mx-auto\"&gt;&lt;/RefreshIcon&gt;}\n{(isError || mutation.isError) &amp;&amp; &lt;span className='text-red'&gt;{error ? (error.message ? error.message : error) : mutation.error.message}&lt;/span&gt;}</code></pre><p>This shows the loading icon or the error message for both the query that fetches the notes and the mutation that handles deleting a note.</p><p>Finally, find the delete button of a note and add an <code>onClick</code> handler:</p><pre><code class=\"language-js\">&lt;button className='link text-gray-400' onClick={() =&gt; mutation.mutate(note)}&gt;Delete&lt;/button&gt;</code></pre><p>On click, the mutation responsible for deleting the note is triggered using <code>mutation.mutate</code>. You pass it the note to delete which is the current note in a <code>map</code> loop.</p><h3 id=\"test-deleting-a-note\">Test Deleting a Note</h3><p>Rerun your server and React app if they're not running. Then, open the website again at <code>localhost:3000</code>. Click the Delete link for any of your notes. If the note is deleted successfully, an alert will be shown.</p><figure class=\"kg-card kg-image-card\"><img src=\"https://backend.shahednasser.com/content/images/2022/07/Screen-Shot-2022-07-01-at-2.48.53-PM.png\" class=\"kg-image\" alt loading=\"lazy\" width=\"1006\" height=\"314\" srcset=\"https://backend.shahednasser.com/content/images/size/w600/2022/07/Screen-Shot-2022-07-01-at-2.48.53-PM.png 600w, https://backend.shahednasser.com/content/images/size/w1000/2022/07/Screen-Shot-2022-07-01-at-2.48.53-PM.png 1000w, https://backend.shahednasser.com/content/images/2022/07/Screen-Shot-2022-07-01-at-2.48.53-PM.png 1006w\" sizes=\"(min-width: 720px) 720px\"></figure><p>After closing the alert, the notes will be fetched again and displayed, if there are any other notes.</p><figure class=\"kg-card kg-image-card\"><img src=\"https://backend.shahednasser.com/content/images/2022/07/Screen-Shot-2022-06-28-at-9.10.04-PM-1.png\" class=\"kg-image\" alt loading=\"lazy\" width=\"1446\" height=\"490\" srcset=\"https://backend.shahednasser.com/content/images/size/w600/2022/07/Screen-Shot-2022-06-28-at-9.10.04-PM-1.png 600w, https://backend.shahednasser.com/content/images/size/w1000/2022/07/Screen-Shot-2022-06-28-at-9.10.04-PM-1.png 1000w, https://backend.shahednasser.com/content/images/2022/07/Screen-Shot-2022-06-28-at-9.10.04-PM-1.png 1446w\" sizes=\"(min-width: 720px) 720px\"></figure><h2 id=\"conclusion\">Conclusion</h2><p>Using React (TanStack) Query, you can easily handle server data fetching and manipulation on your website with advanced features such as caching and synchronization across your React app.</p><p>Make sure to check out the <a href=\"https://tanstack.com/query/v4\">official documentation</a> to learn more about what you can do with React Query.</p>","url":"https://backend.shahednasser.com/react-query-tutorial-for-beginners/","canonical_url":null,"uuid":"59ed8cc6-4d4d-4eac-a4c7-5bfd0118df23","codeinjection_foot":null,"codeinjection_head":null,"codeinjection_styles":null,"comment_id":"62dd14674e918d05f353777d","reading_time":13,"send_email_when_published":null,"email_subject":null,"childHtmlRehype":{"html":"<p>React Query (now rebranded to TanStack Query) is a React library used to make fetching and manipulating server-side data easier. Using React Query, you can implement, along with data fetching, caching, and synchronization of your data with the server.</p><p>In this tutorial, you'll build a simple Node.js server and then learn how to interact with it on a React website using React Query.</p><p>Please note that this version uses v4 of React Query which is now named TanStack Query.</p><p>You can find the code for this tutorial in <a href=\"https://github.com/shahednasser/react-query-tutorial\">this GitHub repository</a>.</p><h2 id=\"prerequisites\">Prerequisites</h2><p> Before starting with this tutorial make sure you have <a href=\"https://nodejs.org/en/\">Node.js installed</a>. You need at least version 14.</p><h2 id=\"server-setup\">Server Setup</h2><p>In this section, you'll set up a simple Node.js server with an SQLite database. The server has 3 endpoints to fetch, add, and delete notes.</p><p>If you already have a server you can skip this section and go to the Website Setup section.</p><h3 id=\"create-server-project\">Create Server Project</h3><p>Create a new directory called <code class=\"language-text\">server</code> then initialize a new project using NPM:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\"><span class=\"token function\">mkdir</span> server\n<span class=\"token builtin class-name\">cd</span> server\n<span class=\"token function\">npm</span> init -y</code></pre></div><h3 id=\"install-dependencies\">Install Dependencies</h3><p>Then, install the packages you'll need for the development of the server:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\"><span class=\"token function\">npm</span> i express cors body-parser sqlite3 nodemon</code></pre></div><p>Here's what each of the packages is for:</p><ol><li><code class=\"language-text\">express</code> to create a server using <a href=\"https://expressjs.com/\">Express</a>.</li><li><code class=\"language-text\">cors</code> is an Express middleware used to handle <a href=\"https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS\">CORS</a> on your server.</li><li><code class=\"language-text\">body-parser</code> is an Express middleware used to parse the body of a request.</li><li><code class=\"language-text\">sqlite3</code> is an SQLite database adapter for Node.js.</li><li><code class=\"language-text\">nodemon</code> is a library used to restart the server whenever new changes occur to the files.</li></ol><h3 id=\"create-server\">Create Server</h3><p>Create the file <code class=\"language-text\">index.js</code> with the following content:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token keyword\">const</span> express <span class=\"token operator\">=</span> <span class=\"token function\">require</span><span class=\"token punctuation\">(</span><span class=\"token string\">'express'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n\n<span class=\"token keyword\">const</span> app <span class=\"token operator\">=</span> <span class=\"token function\">express</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token keyword\">const</span> port <span class=\"token operator\">=</span> <span class=\"token number\">3001</span><span class=\"token punctuation\">;</span>\n<span class=\"token keyword\">const</span> cors <span class=\"token operator\">=</span> <span class=\"token function\">require</span><span class=\"token punctuation\">(</span><span class=\"token string\">'cors'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token keyword\">const</span> sqlite3 <span class=\"token operator\">=</span> <span class=\"token function\">require</span><span class=\"token punctuation\">(</span><span class=\"token string\">'sqlite3'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token function\">verbose</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token keyword\">const</span> bodyParser <span class=\"token operator\">=</span> <span class=\"token function\">require</span><span class=\"token punctuation\">(</span><span class=\"token string\">'body-parser'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n\napp<span class=\"token punctuation\">.</span><span class=\"token function\">use</span><span class=\"token punctuation\">(</span>bodyParser<span class=\"token punctuation\">.</span><span class=\"token function\">json</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\napp<span class=\"token punctuation\">.</span><span class=\"token function\">use</span><span class=\"token punctuation\">(</span><span class=\"token function\">cors</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n\napp<span class=\"token punctuation\">.</span><span class=\"token function\">listen</span><span class=\"token punctuation\">(</span>port<span class=\"token punctuation\">,</span> <span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\n  console<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token template-string\"><span class=\"token template-punctuation string\">`</span><span class=\"token string\">Notes app listening on port </span><span class=\"token interpolation\"><span class=\"token interpolation-punctuation punctuation\">${</span>port<span class=\"token interpolation-punctuation punctuation\">}</span></span><span class=\"token template-punctuation string\">`</span></span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code></pre></div><p>This initializes the server using Express on port <code class=\"language-text\">3001</code>. It also uses the <code class=\"language-text\">cors</code> and <code class=\"language-text\">body-parser</code> middleware.</p><p>Then, in <code class=\"language-text\">package.json</code> add a new script <code class=\"language-text\">start</code> to run the server:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"json\"><pre class=\"language-json\"><code class=\"language-json\">  <span class=\"token property\">\"scripts\"</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token property\">\"start\"</span><span class=\"token operator\">:</span> <span class=\"token string\">\"nodemon index.js\"</span>\n  <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span></code></pre></div><h3 id=\"initialize-the-database\">Initialize the Database</h3><p>In <code class=\"language-text\">index.js</code> before <code class=\"language-text\">app.listen</code> add the following code:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token keyword\">const</span> db <span class=\"token operator\">=</span> <span class=\"token keyword\">new</span> <span class=\"token class-name\">sqlite3<span class=\"token punctuation\">.</span>Database</span><span class=\"token punctuation\">(</span><span class=\"token string\">'data.db'</span><span class=\"token punctuation\">,</span> <span class=\"token punctuation\">(</span><span class=\"token parameter\">err</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\n  <span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span>err<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token keyword\">throw</span> err<span class=\"token punctuation\">;</span>\n  <span class=\"token punctuation\">}</span>\n\n  <span class=\"token comment\">// create tables if they don't exist</span>\n  db<span class=\"token punctuation\">.</span><span class=\"token function\">serialize</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\n    db<span class=\"token punctuation\">.</span><span class=\"token function\">run</span><span class=\"token punctuation\">(</span><span class=\"token template-string\"><span class=\"token template-punctuation string\">`</span><span class=\"token string\">CREATE TABLE IF NOT EXISTS notes (id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT, content TEXT, \n      created_at datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, updated_at datetime NOT NULL DEFAULT CURRENT_TIMESTAMP)</span><span class=\"token template-punctuation string\">`</span></span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n  <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code></pre></div><p>This creates a new database if it doesn't exist in the file <code class=\"language-text\">data.db</code>. Then, if the <code class=\"language-text\">notes</code> table doesn't exist on the database it creates it as well.</p><h3 id=\"add-endpoints\">Add Endpoints</h3><p>Following the database code, add the following code to add the endpoints:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\">app<span class=\"token punctuation\">.</span><span class=\"token function\">get</span><span class=\"token punctuation\">(</span><span class=\"token string\">'/notes'</span><span class=\"token punctuation\">,</span> <span class=\"token punctuation\">(</span><span class=\"token parameter\">req<span class=\"token punctuation\">,</span> res</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\n  db<span class=\"token punctuation\">.</span><span class=\"token function\">all</span><span class=\"token punctuation\">(</span><span class=\"token string\">'SELECT * FROM notes'</span><span class=\"token punctuation\">,</span> <span class=\"token punctuation\">(</span><span class=\"token parameter\">err<span class=\"token punctuation\">,</span> rows</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\n    <span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span>err<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n      console<span class=\"token punctuation\">.</span><span class=\"token function\">error</span><span class=\"token punctuation\">(</span>err<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n      <span class=\"token keyword\">return</span> res<span class=\"token punctuation\">.</span><span class=\"token function\">status</span><span class=\"token punctuation\">(</span><span class=\"token number\">500</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token function\">json</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span> <span class=\"token literal-property property\">success</span><span class=\"token operator\">:</span> <span class=\"token boolean\">false</span><span class=\"token punctuation\">,</span> <span class=\"token literal-property property\">message</span><span class=\"token operator\">:</span> <span class=\"token string\">'An error occurred, please try again later'</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n    <span class=\"token punctuation\">}</span>\n\n    <span class=\"token keyword\">return</span> res<span class=\"token punctuation\">.</span><span class=\"token function\">json</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span> <span class=\"token literal-property property\">success</span><span class=\"token operator\">:</span> <span class=\"token boolean\">true</span><span class=\"token punctuation\">,</span> <span class=\"token literal-property property\">data</span><span class=\"token operator\">:</span> rows <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n  <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n\napp<span class=\"token punctuation\">.</span><span class=\"token function\">get</span><span class=\"token punctuation\">(</span><span class=\"token string\">'/notes/:id'</span><span class=\"token punctuation\">,</span> <span class=\"token punctuation\">(</span><span class=\"token parameter\">req<span class=\"token punctuation\">,</span> res</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\n  db<span class=\"token punctuation\">.</span><span class=\"token function\">get</span><span class=\"token punctuation\">(</span><span class=\"token string\">'SELECT * FROM notes WHERE id = ?'</span><span class=\"token punctuation\">,</span> req<span class=\"token punctuation\">.</span>params<span class=\"token punctuation\">.</span>id<span class=\"token punctuation\">,</span> <span class=\"token punctuation\">(</span><span class=\"token parameter\">err<span class=\"token punctuation\">,</span> row</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\n    <span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span>err<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n      console<span class=\"token punctuation\">.</span><span class=\"token function\">error</span><span class=\"token punctuation\">(</span>err<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n      <span class=\"token keyword\">return</span> res<span class=\"token punctuation\">.</span><span class=\"token function\">status</span><span class=\"token punctuation\">(</span><span class=\"token number\">500</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token function\">json</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span> <span class=\"token literal-property property\">success</span><span class=\"token operator\">:</span> <span class=\"token boolean\">false</span><span class=\"token punctuation\">,</span> <span class=\"token literal-property property\">message</span><span class=\"token operator\">:</span> <span class=\"token string\">'An error occurred, please try again later'</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n    <span class=\"token punctuation\">}</span>\n\n    <span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span><span class=\"token operator\">!</span>row<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n      <span class=\"token keyword\">return</span> res<span class=\"token punctuation\">.</span><span class=\"token function\">status</span><span class=\"token punctuation\">(</span><span class=\"token number\">404</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token function\">json</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span> <span class=\"token literal-property property\">success</span><span class=\"token operator\">:</span> <span class=\"token boolean\">false</span><span class=\"token punctuation\">,</span> <span class=\"token literal-property property\">message</span><span class=\"token operator\">:</span> <span class=\"token string\">'Note does not exist'</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n    <span class=\"token punctuation\">}</span>\n\n    <span class=\"token keyword\">return</span> res<span class=\"token punctuation\">.</span><span class=\"token function\">json</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span> <span class=\"token literal-property property\">success</span><span class=\"token operator\">:</span> <span class=\"token boolean\">true</span><span class=\"token punctuation\">,</span> <span class=\"token literal-property property\">data</span><span class=\"token operator\">:</span> row <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n  <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n\napp<span class=\"token punctuation\">.</span><span class=\"token function\">post</span><span class=\"token punctuation\">(</span><span class=\"token string\">'/notes'</span><span class=\"token punctuation\">,</span> <span class=\"token punctuation\">(</span><span class=\"token parameter\">req<span class=\"token punctuation\">,</span> res</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\n  <span class=\"token keyword\">const</span> <span class=\"token punctuation\">{</span> title<span class=\"token punctuation\">,</span> content <span class=\"token punctuation\">}</span> <span class=\"token operator\">=</span> req<span class=\"token punctuation\">.</span>body<span class=\"token punctuation\">;</span>\n\n  <span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span><span class=\"token operator\">!</span>title <span class=\"token operator\">||</span> <span class=\"token operator\">!</span>content<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token keyword\">return</span> res<span class=\"token punctuation\">.</span><span class=\"token function\">status</span><span class=\"token punctuation\">(</span><span class=\"token number\">400</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token function\">json</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span> <span class=\"token literal-property property\">success</span><span class=\"token operator\">:</span> <span class=\"token boolean\">false</span><span class=\"token punctuation\">,</span> <span class=\"token literal-property property\">message</span><span class=\"token operator\">:</span> <span class=\"token string\">'title and content are required'</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n  <span class=\"token punctuation\">}</span>\n\n  db<span class=\"token punctuation\">.</span><span class=\"token function\">run</span><span class=\"token punctuation\">(</span><span class=\"token string\">'INSERT INTO notes (title, content) VALUES (?, ?)'</span><span class=\"token punctuation\">,</span> <span class=\"token punctuation\">[</span>title<span class=\"token punctuation\">,</span> content<span class=\"token punctuation\">]</span><span class=\"token punctuation\">,</span> <span class=\"token keyword\">function</span> <span class=\"token punctuation\">(</span><span class=\"token parameter\">err</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span>err<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n      console<span class=\"token punctuation\">.</span><span class=\"token function\">error</span><span class=\"token punctuation\">(</span>err<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n      <span class=\"token keyword\">return</span> res<span class=\"token punctuation\">.</span><span class=\"token function\">status</span><span class=\"token punctuation\">(</span><span class=\"token number\">500</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token function\">json</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span> <span class=\"token literal-property property\">success</span><span class=\"token operator\">:</span> <span class=\"token boolean\">false</span><span class=\"token punctuation\">,</span> <span class=\"token literal-property property\">message</span><span class=\"token operator\">:</span> <span class=\"token string\">'An error occurred, please try again later'</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n    <span class=\"token punctuation\">}</span>\n\n    <span class=\"token keyword\">return</span> res<span class=\"token punctuation\">.</span><span class=\"token function\">json</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span>\n      <span class=\"token literal-property property\">success</span><span class=\"token operator\">:</span> <span class=\"token boolean\">true</span><span class=\"token punctuation\">,</span>\n      <span class=\"token literal-property property\">data</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span>\n        <span class=\"token literal-property property\">id</span><span class=\"token operator\">:</span> <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>lastID<span class=\"token punctuation\">,</span>\n        title<span class=\"token punctuation\">,</span>\n        content<span class=\"token punctuation\">,</span>\n      <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\n    <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n  <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n\napp<span class=\"token punctuation\">.</span><span class=\"token function\">delete</span><span class=\"token punctuation\">(</span><span class=\"token string\">'/notes/:id'</span><span class=\"token punctuation\">,</span> <span class=\"token punctuation\">(</span><span class=\"token parameter\">req<span class=\"token punctuation\">,</span> res</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\n  <span class=\"token keyword\">const</span> <span class=\"token punctuation\">{</span> id <span class=\"token punctuation\">}</span> <span class=\"token operator\">=</span> req<span class=\"token punctuation\">.</span>params<span class=\"token punctuation\">;</span>\n\n  db<span class=\"token punctuation\">.</span><span class=\"token function\">get</span><span class=\"token punctuation\">(</span><span class=\"token string\">'SELECT * FROM notes WHERE id = ?'</span><span class=\"token punctuation\">,</span> <span class=\"token punctuation\">[</span>id<span class=\"token punctuation\">]</span><span class=\"token punctuation\">,</span> <span class=\"token punctuation\">(</span><span class=\"token parameter\">err<span class=\"token punctuation\">,</span> row</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\n    <span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span>err<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n      console<span class=\"token punctuation\">.</span><span class=\"token function\">error</span><span class=\"token punctuation\">(</span>err<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n      <span class=\"token keyword\">return</span> res<span class=\"token punctuation\">.</span><span class=\"token function\">status</span><span class=\"token punctuation\">(</span><span class=\"token number\">500</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token function\">json</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span> <span class=\"token literal-property property\">success</span><span class=\"token operator\">:</span> <span class=\"token boolean\">false</span><span class=\"token punctuation\">,</span> <span class=\"token literal-property property\">message</span><span class=\"token operator\">:</span> <span class=\"token string\">'An error occurred, please try again later'</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n    <span class=\"token punctuation\">}</span>\n\n    <span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span><span class=\"token operator\">!</span>row<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n      <span class=\"token keyword\">return</span> res<span class=\"token punctuation\">.</span><span class=\"token function\">status</span><span class=\"token punctuation\">(</span><span class=\"token number\">404</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token function\">json</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span> <span class=\"token literal-property property\">success</span><span class=\"token operator\">:</span> <span class=\"token boolean\">false</span><span class=\"token punctuation\">,</span> <span class=\"token literal-property property\">message</span><span class=\"token operator\">:</span> <span class=\"token string\">'Note does not exist'</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n    <span class=\"token punctuation\">}</span>\n\n    db<span class=\"token punctuation\">.</span><span class=\"token function\">run</span><span class=\"token punctuation\">(</span><span class=\"token string\">'DELETE FROM notes WHERE id = ?'</span><span class=\"token punctuation\">,</span> <span class=\"token punctuation\">[</span>id<span class=\"token punctuation\">]</span><span class=\"token punctuation\">,</span> <span class=\"token punctuation\">(</span><span class=\"token parameter\">error</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\n      <span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span>error<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n        console<span class=\"token punctuation\">.</span><span class=\"token function\">error</span><span class=\"token punctuation\">(</span>error<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n        <span class=\"token keyword\">return</span> res<span class=\"token punctuation\">.</span><span class=\"token function\">status</span><span class=\"token punctuation\">(</span><span class=\"token number\">500</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token function\">json</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span> <span class=\"token literal-property property\">success</span><span class=\"token operator\">:</span> <span class=\"token boolean\">false</span><span class=\"token punctuation\">,</span> <span class=\"token literal-property property\">message</span><span class=\"token operator\">:</span> <span class=\"token string\">'An error occurred, please try again later'</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n      <span class=\"token punctuation\">}</span>\n\n      <span class=\"token keyword\">return</span> res<span class=\"token punctuation\">.</span><span class=\"token function\">json</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span> <span class=\"token literal-property property\">success</span><span class=\"token operator\">:</span> <span class=\"token boolean\">true</span><span class=\"token punctuation\">,</span> <span class=\"token literal-property property\">message</span><span class=\"token operator\">:</span> <span class=\"token string\">'Note deleted successfully'</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n    <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n  <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code></pre></div><p>Briefly, this creates 4 endpoints:</p><ol><li><code class=\"language-text\">/notes</code> endpoint of the method <code class=\"language-text\">GET</code> to fetch all notes.</li><li><code class=\"language-text\">/notes/:id</code> endpoint of the method <code class=\"language-text\">GET</code> to fetch a note by an ID.</li><li><code class=\"language-text\">/notes</code> endpoint of the method <code class=\"language-text\">POST</code> to add a note.</li><li><code class=\"language-text\">/notes/:id</code> endpoint of the method <code class=\"language-text\">DELETE</code> to delete a note.</li></ol><h3 id=\"test-server\">Test Server</h3><p>Run the following command to start the server:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\"><span class=\"token function\">npm</span> start</code></pre></div><p>This starts the server on port <code class=\"language-text\">3001</code>. You can test it out by sending a request to <code class=\"language-text\">localhost:3001/notes</code>.</p><h2 id=\"website-setup\">Website Setup</h2><p>In this section, you'll create the website with Create React App (CRA). This is where you'll make use of React Query.</p><h3 id=\"create-website-project\">Create Website Project</h3><p>To create a new React app, run the following command in a different directory:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\">npx create-react-app website</code></pre></div><p>This creates a new React app in the directory <code class=\"language-text\">website</code>.</p><h3 id=\"install-dependencies-1\">Install Dependencies</h3><p>Run the following command to change to the <code class=\"language-text\">website</code> directory and install the necessary dependencies for the website:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\"><span class=\"token builtin class-name\">cd</span> website\n<span class=\"token function\">npm</span> i @tanstack/react-query tailwindcss postcss autoprefixer @tailwindcss/typography @heroicons/react @windmill/react-ui</code></pre></div><p>The <code class=\"language-text\">@tanstack/react-query</code> library is the React Query library which is now named TanStack Query. The other libraries are <a href=\"https://tailwindcss.com/\">Tailwind CSS</a> related libraries to add styling to the website.</p><h3 id=\"tailwind-css-setup\">Tailwind CSS Setup</h3><p>This section is optional and is only used to set up Tailwind CSS.</p><p>Create the file <code class=\"language-text\">postcss.config.js</code> with the following content:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\">module<span class=\"token punctuation\">.</span>exports <span class=\"token operator\">=</span> <span class=\"token punctuation\">{</span>\n  <span class=\"token literal-property property\">plugins</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token literal-property property\">tailwindcss</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span><span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\n    <span class=\"token literal-property property\">autoprefixer</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span><span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\n  <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\n<span class=\"token punctuation\">}</span>\n</code></pre></div><p>Also, create the file <code class=\"language-text\">tailwind.config.js</code> with the following content:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token comment\">/** @type {import('tailwindcss').Config} */</span>\nmodule<span class=\"token punctuation\">.</span>exports <span class=\"token operator\">=</span> <span class=\"token punctuation\">{</span>\n  <span class=\"token literal-property property\">content</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">[</span>\n    <span class=\"token string\">\"./src/**/*.{js,jsx,ts,tsx}\"</span><span class=\"token punctuation\">,</span>\n  <span class=\"token punctuation\">]</span><span class=\"token punctuation\">,</span>\n  <span class=\"token literal-property property\">theme</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token literal-property property\">extend</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span><span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\n  <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\n  <span class=\"token literal-property property\">plugins</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">[</span>\n    <span class=\"token function\">require</span><span class=\"token punctuation\">(</span><span class=\"token string\">'@tailwindcss/typography'</span><span class=\"token punctuation\">)</span>\n  <span class=\"token punctuation\">]</span><span class=\"token punctuation\">,</span>\n<span class=\"token punctuation\">}</span>\n</code></pre></div><p>Then, create the file <code class=\"language-text\">src/index.css</code> with the following content:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"css\"><pre class=\"language-css\"><code class=\"language-css\"><span class=\"token atrule\"><span class=\"token rule\">@tailwind</span> base<span class=\"token punctuation\">;</span></span>\n<span class=\"token atrule\"><span class=\"token rule\">@tailwind</span> components<span class=\"token punctuation\">;</span></span>\n<span class=\"token atrule\"><span class=\"token rule\">@tailwind</span> utilities<span class=\"token punctuation\">;</span></span></code></pre></div><p>Finally, in <code class=\"language-text\">index.js</code> import <code class=\"language-text\">src/index.css</code> at the beginning of the file:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token keyword\">import</span> <span class=\"token string\">'./index.css'</span><span class=\"token punctuation\">;</span></code></pre></div><h3 id=\"use-queryclientprovider\">Use QueryClientProvider</h3><p>To use the React Query client in all of your components, you must use it at a high level in your website's components hierarchy. The best place to put it is in <code class=\"language-text\">src/index.js</code> which wraps up your entire website's components.</p><p>In <code class=\"language-text\">src/index.js</code> add the following imports at the beginning of the file:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span>\n  QueryClient<span class=\"token punctuation\">,</span>\n  QueryClientProvider<span class=\"token punctuation\">,</span>\n<span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'@tanstack/react-query'</span></code></pre></div><p>Then, initialize a new Query client:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token keyword\">const</span> queryClient <span class=\"token operator\">=</span> <span class=\"token keyword\">new</span> <span class=\"token class-name\">QueryClient</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span></code></pre></div><p>Finally, change the parameter passed to <code class=\"language-text\">root.render</code>:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\">root<span class=\"token punctuation\">.</span><span class=\"token function\">render</span><span class=\"token punctuation\">(</span>\n  <span class=\"token operator\">&#x3C;</span>QueryClientProvider client<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span>queryClient<span class=\"token punctuation\">}</span><span class=\"token operator\">></span>\n    <span class=\"token operator\">&#x3C;</span>App <span class=\"token operator\">/</span><span class=\"token operator\">></span>\n  <span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>QueryClientProvider<span class=\"token operator\">></span>\n<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code></pre></div><p>This wraps the <code class=\"language-text\">App</code> component which holds the rest of the website's components with <code class=\"language-text\">QueryClientProvider</code>. This provider accepts the prop <code class=\"language-text\">client</code> which is an instance of <code class=\"language-text\">QueryClient</code>.</p><p>Now, all components within the website will have access to the Query Client which is used to fetch, cache, and manipulate the server data.</p><h3 id=\"implement-display-notes\">Implement Display Notes</h3><p>Fetching data from the server is an act of performing a query. Therefore, you'll use <code class=\"language-text\">useQuery</code> in this section.</p><p>You'll display notes in the <code class=\"language-text\">App</code> component. These notes are fetched from the server using the <code class=\"language-text\">/notes</code> endpoint.</p><p>Replace the content of <code class=\"language-text\">app.js</code> with the following content:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> PlusIcon<span class=\"token punctuation\">,</span> RefreshIcon <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'@heroicons/react/solid'</span>\n<span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> useMutation<span class=\"token punctuation\">,</span> useQuery<span class=\"token punctuation\">,</span> useQueryClient <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'@tanstack/react-query'</span>\n\n<span class=\"token keyword\">function</span> <span class=\"token function\">App</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n  <span class=\"token keyword\">const</span> <span class=\"token punctuation\">{</span> isLoading<span class=\"token punctuation\">,</span> isError<span class=\"token punctuation\">,</span> data<span class=\"token punctuation\">,</span> error <span class=\"token punctuation\">}</span> <span class=\"token operator\">=</span> <span class=\"token function\">useQuery</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">[</span><span class=\"token string\">'notes'</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">,</span> fetchNotes<span class=\"token punctuation\">)</span>\n\n  <span class=\"token keyword\">function</span> <span class=\"token function\">fetchNotes</span> <span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token keyword\">return</span> <span class=\"token function\">fetch</span><span class=\"token punctuation\">(</span><span class=\"token string\">'http://localhost:3001/notes'</span><span class=\"token punctuation\">)</span>\n    <span class=\"token punctuation\">.</span><span class=\"token function\">then</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">response</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> response<span class=\"token punctuation\">.</span><span class=\"token function\">json</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span>\n    <span class=\"token punctuation\">.</span><span class=\"token function\">then</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">(</span><span class=\"token parameter\"><span class=\"token punctuation\">{</span> success<span class=\"token punctuation\">,</span> data <span class=\"token punctuation\">}</span></span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\n      <span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span><span class=\"token operator\">!</span>success<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n        <span class=\"token keyword\">throw</span> <span class=\"token keyword\">new</span> <span class=\"token class-name\">Error</span> <span class=\"token punctuation\">(</span><span class=\"token string\">'An error occurred while fetching notes'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n      <span class=\"token punctuation\">}</span>\n      <span class=\"token keyword\">return</span> data<span class=\"token punctuation\">;</span>\n    <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span>\n  <span class=\"token punctuation\">}</span>\n\n  <span class=\"token keyword\">return</span> <span class=\"token punctuation\">(</span>\n    <span class=\"token operator\">&#x3C;</span>div className<span class=\"token operator\">=</span><span class=\"token string\">\"w-screen h-screen overflow-x-hidden bg-red-400 flex flex-col justify-center items-center\"</span><span class=\"token operator\">></span>\n      <span class=\"token operator\">&#x3C;</span>div className<span class=\"token operator\">=</span><span class=\"token string\">'bg-white w-full md:w-1/2 p-5 text-center rounded shadow-md text-gray-800 prose'</span><span class=\"token operator\">></span>\n        <span class=\"token operator\">&#x3C;</span>h1<span class=\"token operator\">></span>Notes<span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>h1<span class=\"token operator\">></span>\n        <span class=\"token punctuation\">{</span>isLoading <span class=\"token operator\">&#x26;&#x26;</span> <span class=\"token operator\">&#x3C;</span>RefreshIcon className<span class=\"token operator\">=</span><span class=\"token string\">\"w-10 h-10 animate-spin mx-auto\"</span><span class=\"token operator\">></span><span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>RefreshIcon<span class=\"token operator\">></span><span class=\"token punctuation\">}</span>\n        <span class=\"token punctuation\">{</span>isError <span class=\"token operator\">&#x26;&#x26;</span> <span class=\"token operator\">&#x3C;</span>span className<span class=\"token operator\">=</span><span class=\"token string\">'text-red'</span><span class=\"token operator\">></span><span class=\"token punctuation\">{</span>error<span class=\"token punctuation\">.</span>message <span class=\"token operator\">?</span> error<span class=\"token punctuation\">.</span>message <span class=\"token operator\">:</span> error<span class=\"token punctuation\">}</span><span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>span<span class=\"token operator\">></span><span class=\"token punctuation\">}</span>\n        <span class=\"token punctuation\">{</span><span class=\"token operator\">!</span>isLoading <span class=\"token operator\">&#x26;&#x26;</span> <span class=\"token operator\">!</span>isError <span class=\"token operator\">&#x26;&#x26;</span> data <span class=\"token operator\">&#x26;&#x26;</span> <span class=\"token operator\">!</span>data<span class=\"token punctuation\">.</span>length <span class=\"token operator\">&#x26;&#x26;</span> <span class=\"token operator\">&#x3C;</span>span className<span class=\"token operator\">=</span><span class=\"token string\">'text-red-400'</span><span class=\"token operator\">></span>You have no notes<span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>span<span class=\"token operator\">></span><span class=\"token punctuation\">}</span>\n        <span class=\"token punctuation\">{</span>data <span class=\"token operator\">&#x26;&#x26;</span> data<span class=\"token punctuation\">.</span>length <span class=\"token operator\">></span> <span class=\"token number\">0</span> <span class=\"token operator\">&#x26;&#x26;</span> data<span class=\"token punctuation\">.</span><span class=\"token function\">map</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">note<span class=\"token punctuation\">,</span> index</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">(</span>\n          <span class=\"token operator\">&#x3C;</span>div key<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span>note<span class=\"token punctuation\">.</span>id<span class=\"token punctuation\">}</span> className<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span><span class=\"token template-string\"><span class=\"token template-punctuation string\">`</span><span class=\"token string\">text-left </span><span class=\"token interpolation\"><span class=\"token interpolation-punctuation punctuation\">${</span>index <span class=\"token operator\">!==</span> data<span class=\"token punctuation\">.</span>length <span class=\"token operator\">-</span> <span class=\"token number\">1</span> <span class=\"token operator\">?</span> <span class=\"token string\">'border-b pb-2'</span> <span class=\"token operator\">:</span> <span class=\"token string\">''</span><span class=\"token interpolation-punctuation punctuation\">}</span></span><span class=\"token template-punctuation string\">`</span></span><span class=\"token punctuation\">}</span><span class=\"token operator\">></span>\n            <span class=\"token operator\">&#x3C;</span>h2<span class=\"token operator\">></span><span class=\"token punctuation\">{</span>note<span class=\"token punctuation\">.</span>title<span class=\"token punctuation\">}</span><span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>h2<span class=\"token operator\">></span>\n            <span class=\"token operator\">&#x3C;</span>p<span class=\"token operator\">></span><span class=\"token punctuation\">{</span>note<span class=\"token punctuation\">.</span>content<span class=\"token punctuation\">}</span><span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>p<span class=\"token operator\">></span>\n            <span class=\"token operator\">&#x3C;</span>span<span class=\"token operator\">></span>\n              <span class=\"token operator\">&#x3C;</span>button className<span class=\"token operator\">=</span><span class=\"token string\">'link text-gray-400'</span><span class=\"token operator\">></span>Delete<span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>button<span class=\"token operator\">></span>\n            <span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>span<span class=\"token operator\">></span>\n          <span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>div<span class=\"token operator\">></span>\n        <span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">}</span>\n      <span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>div<span class=\"token operator\">></span>\n      <span class=\"token operator\">&#x3C;</span>button className<span class=\"token operator\">=</span><span class=\"token string\">\"mt-2 bg-gray-700 hover:bg-gray-600 rounded-full text-white p-3\"</span><span class=\"token operator\">></span>\n        <span class=\"token operator\">&#x3C;</span>PlusIcon className<span class=\"token operator\">=</span><span class=\"token string\">'w-5 h-5'</span><span class=\"token operator\">></span><span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>PlusIcon<span class=\"token operator\">></span>\n      <span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>button<span class=\"token operator\">></span>\n    <span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>div<span class=\"token operator\">></span>\n  <span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span>\n\n<span class=\"token keyword\">export</span> <span class=\"token keyword\">default</span> App<span class=\"token punctuation\">;</span></code></pre></div><p>Here's briefly what's going on in this code snippet:</p><ol><li>You use <code class=\"language-text\">useQuery</code> to fetch the notes. The first parameter it accepts is a unique key used for caching. The second parameter is the function used to fetch the data. You pass it the <code class=\"language-text\">fetchNotes</code> function.</li><li><code class=\"language-text\">useQuery</code> returns an object that holds <a href=\"https://tanstack.com/query/v4/docs/reference/useQuery\">many variables</a>. Here, you use 4 of them: <code class=\"language-text\">isLoading</code> is a boolean value that determines whether the data is currently being fetched; <code class=\"language-text\">isError</code> is a boolean value that determines if an error occurred. <code class=\"language-text\">data</code> is the data that is fetched from the server; and <code class=\"language-text\">error</code> is the error message if <code class=\"language-text\">isError</code> is true.</li><li>The <code class=\"language-text\">fetchNotes</code> function must return a promise that either resolves data or throws an error. In the function, you send a <code class=\"language-text\">GET</code> request to <code class=\"language-text\">localhost:3001/notes</code> to fetch the notes. If the data is fetched successfully it is returned in the <code class=\"language-text\">then</code> fulfillment function. </li><li>In the returned JSX, if <code class=\"language-text\">isLoading</code> is true, a loading icon is shown. If <code class=\"language-text\">isError</code> is true, an error message is shown. If <code class=\"language-text\">data</code> is fetched successfully and has any data in it, the notes are rendered.</li><li>You also show a button with a plus icon to add new notes. You'll implement this later.</li></ol><h3 id=\"test-displaying-notes\">Test Displaying Notes</h3><p>To test out what you've implemented so far, make sure your server is still running, then start your React app server with the following command:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\"><span class=\"token function\">npm</span> start</code></pre></div><p>This runs your React app on <code class=\"language-text\">localhost:3000</code> by default. If you open it in your browser, you'll see a loading icon at first then you'll see no notes as you haven't added any yet.</p><figure class=\"kg-card kg-image-card\"><img src=\"https://backend.shahednasser.com/content/images/2022/07/Screen-Shot-2022-06-28-at-9.10.04-PM.png\" class=\"kg-image\" alt=\"\" loading=\"lazy\" width=\"1446\" height=\"490\" srcset=\"https://backend.shahednasser.com/content/images/size/w600/2022/07/Screen-Shot-2022-06-28-at-9.10.04-PM.png 600w, https://backend.shahednasser.com/content/images/size/w1000/2022/07/Screen-Shot-2022-06-28-at-9.10.04-PM.png 1000w, https://backend.shahednasser.com/content/images/2022/07/Screen-Shot-2022-06-28-at-9.10.04-PM.png 1446w\" sizes=\"(min-width: 720px) 720px\"></figure><h3 id=\"implement-add-notes-functionality\">Implement Add Notes Functionality</h3><p>Adding a note is an act of mutation on the server data. Therefore, you'll be using the <code class=\"language-text\">useMutation</code> hook in this section.</p><p>You'll create a separate component that shows the form used to add a note.</p><p>Create the file <code class=\"language-text\">src/form.js</code> with the following content:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> useMutation<span class=\"token punctuation\">,</span> useQueryClient <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'@tanstack/react-query'</span>\n\n<span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> useState <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'react'</span>\n\n<span class=\"token keyword\">export</span> <span class=\"token keyword\">default</span> <span class=\"token keyword\">function</span> <span class=\"token function\">Form</span> <span class=\"token punctuation\">(</span><span class=\"token parameter\"><span class=\"token punctuation\">{</span> isOpen<span class=\"token punctuation\">,</span> setIsOpen <span class=\"token punctuation\">}</span></span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n  <span class=\"token keyword\">const</span> <span class=\"token punctuation\">[</span>title<span class=\"token punctuation\">,</span> setTitle<span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token function\">useState</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"\"</span><span class=\"token punctuation\">)</span>\n  <span class=\"token keyword\">const</span> <span class=\"token punctuation\">[</span>content<span class=\"token punctuation\">,</span> setContent<span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token function\">useState</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"\"</span><span class=\"token punctuation\">)</span>\n  <span class=\"token keyword\">const</span> queryClient <span class=\"token operator\">=</span> <span class=\"token function\">useQueryClient</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\n\n  <span class=\"token keyword\">const</span> mutation <span class=\"token operator\">=</span> <span class=\"token function\">useMutation</span><span class=\"token punctuation\">(</span>insertNote<span class=\"token punctuation\">,</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token function-variable function\">onSuccess</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\n      <span class=\"token function\">setTitle</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"\"</span><span class=\"token punctuation\">)</span>\n      <span class=\"token function\">setContent</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"\"</span><span class=\"token punctuation\">)</span>\n    <span class=\"token punctuation\">}</span>\n  <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span>\n\n  <span class=\"token keyword\">function</span> <span class=\"token function\">closeForm</span> <span class=\"token punctuation\">(</span><span class=\"token parameter\">e</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    e<span class=\"token punctuation\">.</span><span class=\"token function\">preventDefault</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\n    <span class=\"token function\">setIsOpen</span><span class=\"token punctuation\">(</span><span class=\"token boolean\">false</span><span class=\"token punctuation\">)</span>\n  <span class=\"token punctuation\">}</span>\n\n  <span class=\"token keyword\">function</span> <span class=\"token function\">insertNote</span> <span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token keyword\">return</span> <span class=\"token function\">fetch</span><span class=\"token punctuation\">(</span><span class=\"token template-string\"><span class=\"token template-punctuation string\">`</span><span class=\"token string\">http://localhost:3001/notes</span><span class=\"token template-punctuation string\">`</span></span><span class=\"token punctuation\">,</span> <span class=\"token punctuation\">{</span>\n      <span class=\"token literal-property property\">method</span><span class=\"token operator\">:</span> <span class=\"token string\">'POST'</span><span class=\"token punctuation\">,</span>\n      <span class=\"token literal-property property\">headers</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span>\n        <span class=\"token string-property property\">'Content-Type'</span><span class=\"token operator\">:</span> <span class=\"token string\">'application/json'</span>\n      <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\n      <span class=\"token literal-property property\">body</span><span class=\"token operator\">:</span> <span class=\"token constant\">JSON</span><span class=\"token punctuation\">.</span><span class=\"token function\">stringify</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span>\n        title<span class=\"token punctuation\">,</span>\n        content\n      <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span>\n    <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span>\n    <span class=\"token punctuation\">.</span><span class=\"token function\">then</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">response</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> response<span class=\"token punctuation\">.</span><span class=\"token function\">json</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span>\n    <span class=\"token punctuation\">.</span><span class=\"token function\">then</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">(</span><span class=\"token parameter\"><span class=\"token punctuation\">{</span> success<span class=\"token punctuation\">,</span> data <span class=\"token punctuation\">}</span></span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\n      <span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span><span class=\"token operator\">!</span>success<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n        <span class=\"token keyword\">throw</span> <span class=\"token keyword\">new</span> <span class=\"token class-name\">Error</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"An error occured\"</span><span class=\"token punctuation\">)</span>\n      <span class=\"token punctuation\">}</span>\n      \n      <span class=\"token function\">setIsOpen</span><span class=\"token punctuation\">(</span><span class=\"token boolean\">false</span><span class=\"token punctuation\">)</span>\n      queryClient<span class=\"token punctuation\">.</span><span class=\"token function\">setQueriesData</span><span class=\"token punctuation\">(</span><span class=\"token string\">'notes'</span><span class=\"token punctuation\">,</span> <span class=\"token punctuation\">(</span><span class=\"token parameter\">old</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">[</span><span class=\"token operator\">...</span>old<span class=\"token punctuation\">,</span> data<span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span>\n    <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span>\n  <span class=\"token punctuation\">}</span>\n\n  <span class=\"token keyword\">function</span> <span class=\"token function\">handleSubmit</span> <span class=\"token punctuation\">(</span><span class=\"token parameter\">e</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    e<span class=\"token punctuation\">.</span><span class=\"token function\">preventDefault</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\n    mutation<span class=\"token punctuation\">.</span><span class=\"token function\">mutate</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\n  <span class=\"token punctuation\">}</span>\n\n  <span class=\"token keyword\">return</span> <span class=\"token punctuation\">(</span>\n    <span class=\"token operator\">&#x3C;</span>div className<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span><span class=\"token template-string\"><span class=\"token template-punctuation string\">`</span><span class=\"token string\">absolute w-full h-full top-0 left-0 z-50 flex justify-center items-center </span><span class=\"token interpolation\"><span class=\"token interpolation-punctuation punctuation\">${</span><span class=\"token operator\">!</span>isOpen <span class=\"token operator\">?</span> <span class=\"token string\">'hidden'</span> <span class=\"token operator\">:</span> <span class=\"token string\">''</span><span class=\"token interpolation-punctuation punctuation\">}</span></span><span class=\"token template-punctuation string\">`</span></span><span class=\"token punctuation\">}</span><span class=\"token operator\">></span>\n      <span class=\"token operator\">&#x3C;</span>div className<span class=\"token operator\">=</span><span class=\"token string\">'bg-black opacity-50 absolute w-full h-full top-0 left-0'</span><span class=\"token operator\">></span><span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>div<span class=\"token operator\">></span>\n      <span class=\"token operator\">&#x3C;</span>form className<span class=\"token operator\">=</span><span class=\"token string\">'bg-white w-full md:w-1/2 p-5 rounded shadow-md text-gray-800 prose relative'</span> \n        onSubmit<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span>handleSubmit<span class=\"token punctuation\">}</span><span class=\"token operator\">></span>\n        <span class=\"token operator\">&#x3C;</span>h2 className<span class=\"token operator\">=</span><span class=\"token string\">'text-center'</span><span class=\"token operator\">></span>Add Note<span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>h2<span class=\"token operator\">></span>\n        <span class=\"token punctuation\">{</span>mutation<span class=\"token punctuation\">.</span>isError <span class=\"token operator\">&#x26;&#x26;</span> <span class=\"token operator\">&#x3C;</span>span className<span class=\"token operator\">=</span><span class=\"token string\">'block mb-2 text-red-400'</span><span class=\"token operator\">></span><span class=\"token punctuation\">{</span>mutation<span class=\"token punctuation\">.</span>error<span class=\"token punctuation\">.</span>message <span class=\"token operator\">?</span> mutation<span class=\"token punctuation\">.</span>error<span class=\"token punctuation\">.</span>message <span class=\"token operator\">:</span> mutation<span class=\"token punctuation\">.</span>error<span class=\"token punctuation\">}</span><span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>span<span class=\"token operator\">></span><span class=\"token punctuation\">}</span>\n        <span class=\"token operator\">&#x3C;</span>input type<span class=\"token operator\">=</span><span class=\"token string\">\"text\"</span> placeholder<span class=\"token operator\">=</span><span class=\"token string\">'Title'</span> className<span class=\"token operator\">=</span><span class=\"token string\">'rounded-sm w-full border px-2'</span> \n          value<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span>title<span class=\"token punctuation\">}</span> onChange<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">e</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token function\">setTitle</span><span class=\"token punctuation\">(</span>e<span class=\"token punctuation\">.</span>target<span class=\"token punctuation\">.</span>value<span class=\"token punctuation\">)</span><span class=\"token punctuation\">}</span> <span class=\"token operator\">/</span><span class=\"token operator\">></span>\n        <span class=\"token operator\">&#x3C;</span>textarea onChange<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">e</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token function\">setContent</span><span class=\"token punctuation\">(</span>e<span class=\"token punctuation\">.</span>target<span class=\"token punctuation\">.</span>value<span class=\"token punctuation\">)</span><span class=\"token punctuation\">}</span> \n          className<span class=\"token operator\">=</span><span class=\"token string\">\"rounded-sm w-full border px-2 mt-2\"</span> placeholder<span class=\"token operator\">=</span><span class=\"token string\">'Content'</span> value<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span>content<span class=\"token punctuation\">}</span><span class=\"token operator\">></span><span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>textarea<span class=\"token operator\">></span>\n        <span class=\"token operator\">&#x3C;</span>div<span class=\"token operator\">></span>\n          <span class=\"token operator\">&#x3C;</span>button type<span class=\"token operator\">=</span><span class=\"token string\">\"submit\"</span> className<span class=\"token operator\">=</span><span class=\"token string\">'mt-2 bg-red-400 hover:bg-red-600 text-white p-3 rounded mr-2 disabled:pointer-events-none'</span> \n            disabled<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span>mutation<span class=\"token punctuation\">.</span>isLoading<span class=\"token punctuation\">}</span><span class=\"token operator\">></span>\n            Add<span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>button<span class=\"token operator\">></span>\n          <span class=\"token operator\">&#x3C;</span>button className<span class=\"token operator\">=</span><span class=\"token string\">'mt-2 bg-gray-700 hover:bg-gray-600 text-white p-3 rounded'</span>\n            onClick<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span>closeForm<span class=\"token punctuation\">}</span><span class=\"token operator\">></span>Cancel<span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>button<span class=\"token operator\">></span>\n        <span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>div<span class=\"token operator\">></span>\n      <span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>form<span class=\"token operator\">></span>\n    <span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>div<span class=\"token operator\">></span>\n  <span class=\"token punctuation\">)</span>\n<span class=\"token punctuation\">}</span></code></pre></div><p>Here's a brief explanation of this form</p><ol><li>This form acts as a pop-up. It accepts <code class=\"language-text\">isOpen</code> and <code class=\"language-text\">setIsOpen</code> props to determine when the form is opened and handle closing it.</li><li>You use <code class=\"language-text\">useQueryClient</code> to get access to the Query Client. This is necessary to perform a mutation.</li><li>To handle adding a note on your server and keep all data in your query client synced, you must the <code class=\"language-text\">useMutation</code> hook. </li><li>The <code class=\"language-text\">useMutation</code> hook accepts 2 parameters. Thie first one is the function that will handle the mutation, which in this case is <code class=\"language-text\">insertNote</code>. The second parameter is an object of <a href=\"https://tanstack.com/query/v4/docs/reference/useMutation\">options</a>. You pass it one option <code class=\"language-text\">onSuccess</code> which is a function that runs if the mutation is performed successfully. You use this to reset the <code class=\"language-text\">title</code> and <code class=\"language-text\">content</code> fields of the form.</li><li>In <code class=\"language-text\">insertNote</code>, you send a <code class=\"language-text\">POST</code> request to <code class=\"language-text\">localhost:3001/notes</code> and pass in the body the <code class=\"language-text\">title</code> and <code class=\"language-text\">content</code> of the note to be created. If the <code class=\"language-text\">success</code> body parameter returned from the server is <code class=\"language-text\">false</code>, an error is thrown to signal that the mutation failed. </li><li>If the note is added successfully, you change the cached value of the <code class=\"language-text\">notes</code> key using the <code class=\"language-text\">queryClient.setQueriesData</code> method. This method accepts the key as a first parameter and the new data associated with that key as a second parameter. This updates the data everywhere it's used on your website.</li><li>In this component you display a form with 2 fields: <code class=\"language-text\">title</code> and <code class=\"language-text\">content</code>. In the form, you check if an error occurs using <code class=\"language-text\">mutation.isError</code> and get access to the error using <code class=\"language-text\">mutation.error</code>.</li><li>You handle form submission in the <code class=\"language-text\">handleSubmit</code> function. Here, you trigger the mutation using <code class=\"language-text\">mutation.mutate</code>. This is where the <code class=\"language-text\">insertNote</code> function is triggered to add a new note.</li></ol><p>Then, in <code class=\"language-text\">src/app.js</code> add the following imports at the beginning of the file:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token keyword\">import</span> Form <span class=\"token keyword\">from</span> <span class=\"token string\">'./form'</span>\n<span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> useState <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'react'</span></code></pre></div><p>Then, at the beginning of the component add a new state variable to manage wheter the form is opened or not:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token keyword\">const</span> <span class=\"token punctuation\">[</span>isOpen<span class=\"token punctuation\">,</span> setIsOpen<span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token function\">useState</span><span class=\"token punctuation\">(</span><span class=\"token boolean\">false</span><span class=\"token punctuation\">)</span></code></pre></div><p>Next, add a new function <code class=\"language-text\">addNote</code> that just uses <code class=\"language-text\">setIsOpen</code> to open the form:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token keyword\">function</span> <span class=\"token function\">addNote</span> <span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token function\">setIsOpen</span><span class=\"token punctuation\">(</span><span class=\"token boolean\">true</span><span class=\"token punctuation\">)</span>\n<span class=\"token punctuation\">}</span></code></pre></div><p>Finally, in the returned JSX, replace the button with the plus icon with the following:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token operator\">&#x3C;</span>button className<span class=\"token operator\">=</span><span class=\"token string\">\"mt-2 bg-gray-700 hover:bg-gray-600 rounded-full text-white p-3\"</span> onClick<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span>addNote<span class=\"token punctuation\">}</span><span class=\"token operator\">></span>\n    <span class=\"token operator\">&#x3C;</span>PlusIcon className<span class=\"token operator\">=</span><span class=\"token string\">'w-5 h-5'</span><span class=\"token operator\">></span><span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>PlusIcon<span class=\"token operator\">></span>\n<span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>button<span class=\"token operator\">></span>\n<span class=\"token operator\">&#x3C;</span>Form isOpen<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span>isOpen<span class=\"token punctuation\">}</span> setIsOpen<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span>setIsOpen<span class=\"token punctuation\">}</span> <span class=\"token operator\">/</span><span class=\"token operator\">></span></code></pre></div><p>This sets the <code class=\"language-text\">onClick</code> handler of the button to <code class=\"language-text\">addNote</code>. It also adds the <code class=\"language-text\">Form</code> component you created earlier as a child component of <code class=\"language-text\">App</code>. </p><h3 id=\"test-adding-a-note\">Test Adding a Note</h3><p>Rerun your server and React app if they're not running. Then, open the website again at <code class=\"language-text\">localhost:3000</code>. Click on the plus button and a pop up will open with the form to add a new note.</p><figure class=\"kg-card kg-image-card\"><img src=\"https://backend.shahednasser.com/content/images/2022/07/Screen-Shot-2022-07-24-at-6.31.41-PM.png\" class=\"kg-image\" alt=\"\" loading=\"lazy\" width=\"1494\" height=\"792\" srcset=\"https://backend.shahednasser.com/content/images/size/w600/2022/07/Screen-Shot-2022-07-24-at-6.31.41-PM.png 600w, https://backend.shahednasser.com/content/images/size/w1000/2022/07/Screen-Shot-2022-07-24-at-6.31.41-PM.png 1000w, https://backend.shahednasser.com/content/images/2022/07/Screen-Shot-2022-07-24-at-6.31.41-PM.png 1494w\" sizes=\"(min-width: 720px) 720px\"></figure><p>Enter a random title and content then click Add. The pop up form will then close and you can see the new note added.</p><figure class=\"kg-card kg-image-card\"><img src=\"https://backend.shahednasser.com/content/images/2022/07/Screen-Shot-2022-07-24-at-6.32.53-PM.png\" class=\"kg-image\" alt=\"\" loading=\"lazy\" width=\"1418\" height=\"720\" srcset=\"https://backend.shahednasser.com/content/images/size/w600/2022/07/Screen-Shot-2022-07-24-at-6.32.53-PM.png 600w, https://backend.shahednasser.com/content/images/size/w1000/2022/07/Screen-Shot-2022-07-24-at-6.32.53-PM.png 1000w, https://backend.shahednasser.com/content/images/2022/07/Screen-Shot-2022-07-24-at-6.32.53-PM.png 1418w\" sizes=\"(min-width: 720px) 720px\"></figure><h3 id=\"implement-delete-note-functionality\">Implement Delete Note Functionality</h3><p>The last functionality you'll add is deleting notes. Deleting a note is another act of mutation as it manipulates the server's data.</p><p>At the beginning of the <code class=\"language-text\">App</code> component in <code class=\"language-text\">src/app.js</code> add the following code:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token keyword\">const</span> queryClient <span class=\"token operator\">=</span> <span class=\"token function\">useQueryClient</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\n<span class=\"token keyword\">const</span> mutation <span class=\"token operator\">=</span> <span class=\"token function\">useMutation</span><span class=\"token punctuation\">(</span>deleteNote<span class=\"token punctuation\">,</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token function-variable function\">onSuccess</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> queryClient<span class=\"token punctuation\">.</span><span class=\"token function\">invalidateQueries</span><span class=\"token punctuation\">(</span><span class=\"token string\">'notes'</span><span class=\"token punctuation\">)</span>\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span></code></pre></div><p>Here, you get access to the query client using <code class=\"language-text\">useQueryClient</code>. Then, you create a new mutation using <code class=\"language-text\">useMutation</code>. You pass it the function <code class=\"language-text\">deleteNote</code> (which you'll create next) as a first parameter and an object of options. </p><p>To the <code class=\"language-text\">onSuccess</code> option you pass a function that does one thing. It executes the method <code class=\"language-text\">queryClient.invalidateQueries</code>. This method marks the cached data for a specific key as outdated, which triggers retrieving the data again. </p><p>So, once a note is deleted, the query you created earlier that executes the function <code class=\"language-text\">fetchNotes</code> will be triggered and the notes will be fetched again. If you had created other queries on your website that use the same key <code class=\"language-text\">notes</code>, they'll also be triggered to update their data. </p><p>Next, add the function <code class=\"language-text\">deleteNote</code> in the <code class=\"language-text\">App</code> component in the same file:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token keyword\">function</span> <span class=\"token function\">deleteNote</span> <span class=\"token punctuation\">(</span><span class=\"token parameter\">note</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token keyword\">return</span> <span class=\"token function\">fetch</span><span class=\"token punctuation\">(</span><span class=\"token template-string\"><span class=\"token template-punctuation string\">`</span><span class=\"token string\">http://localhost:3001/notes/</span><span class=\"token interpolation\"><span class=\"token interpolation-punctuation punctuation\">${</span>note<span class=\"token punctuation\">.</span>id<span class=\"token interpolation-punctuation punctuation\">}</span></span><span class=\"token template-punctuation string\">`</span></span><span class=\"token punctuation\">,</span> <span class=\"token punctuation\">{</span>\n      <span class=\"token literal-property property\">method</span><span class=\"token operator\">:</span> <span class=\"token string\">'DELETE'</span>\n    <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span>\n    <span class=\"token punctuation\">.</span><span class=\"token function\">then</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">response</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> response<span class=\"token punctuation\">.</span><span class=\"token function\">json</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span>\n    <span class=\"token punctuation\">.</span><span class=\"token function\">then</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">(</span><span class=\"token parameter\"><span class=\"token punctuation\">{</span> success<span class=\"token punctuation\">,</span> message <span class=\"token punctuation\">}</span></span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\n      <span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span><span class=\"token operator\">!</span>success<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n        <span class=\"token keyword\">throw</span> <span class=\"token keyword\">new</span> <span class=\"token class-name\">Error</span><span class=\"token punctuation\">(</span>message<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n      <span class=\"token punctuation\">}</span>\n\n      <span class=\"token function\">alert</span><span class=\"token punctuation\">(</span>message<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n    <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span>\n  <span class=\"token punctuation\">}</span></code></pre></div><p>This function receives the <code class=\"language-text\">note</code> to be deleted as a parameter. It sends a <code class=\"language-text\">DELETE</code> request to <code class=\"language-text\">localhost:3001/notes/:id</code>. If the <code class=\"language-text\">success</code> body parameter of the response is <code class=\"language-text\">false</code>, an error is thrown. Otherwise, only an alert is shown.</p><p>Then, in the returned JSX of the <code class=\"language-text\">App</code> component, change how the loading icon and error where shown previously to the following:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token punctuation\">{</span><span class=\"token punctuation\">(</span>isLoading <span class=\"token operator\">||</span> mutation<span class=\"token punctuation\">.</span>isLoading<span class=\"token punctuation\">)</span> <span class=\"token operator\">&#x26;&#x26;</span> <span class=\"token operator\">&#x3C;</span>RefreshIcon className<span class=\"token operator\">=</span><span class=\"token string\">\"w-10 h-10 animate-spin mx-auto\"</span><span class=\"token operator\">></span><span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>RefreshIcon<span class=\"token operator\">></span><span class=\"token punctuation\">}</span>\n<span class=\"token punctuation\">{</span><span class=\"token punctuation\">(</span>isError <span class=\"token operator\">||</span> mutation<span class=\"token punctuation\">.</span>isError<span class=\"token punctuation\">)</span> <span class=\"token operator\">&#x26;&#x26;</span> <span class=\"token operator\">&#x3C;</span>span className<span class=\"token operator\">=</span><span class=\"token string\">'text-red'</span><span class=\"token operator\">></span><span class=\"token punctuation\">{</span>error <span class=\"token operator\">?</span> <span class=\"token punctuation\">(</span>error<span class=\"token punctuation\">.</span>message <span class=\"token operator\">?</span> error<span class=\"token punctuation\">.</span>message <span class=\"token operator\">:</span> error<span class=\"token punctuation\">)</span> <span class=\"token operator\">:</span> mutation<span class=\"token punctuation\">.</span>error<span class=\"token punctuation\">.</span>message<span class=\"token punctuation\">}</span><span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>span<span class=\"token operator\">></span><span class=\"token punctuation\">}</span></code></pre></div><p>This shows the loading icon or the error message for both the query that fetches the notes and the mutation that handles deleting a note.</p><p>Finally, find the delete button of a note and add an <code class=\"language-text\">onClick</code> handler:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token operator\">&#x3C;</span>button className<span class=\"token operator\">=</span><span class=\"token string\">'link text-gray-400'</span> onClick<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> mutation<span class=\"token punctuation\">.</span><span class=\"token function\">mutate</span><span class=\"token punctuation\">(</span>note<span class=\"token punctuation\">)</span><span class=\"token punctuation\">}</span><span class=\"token operator\">></span>Delete<span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>button<span class=\"token operator\">></span></code></pre></div><p>On click, the mutation responsible for deleting the note is triggered using <code class=\"language-text\">mutation.mutate</code>. You pass it the note to delete which is the current note in a <code class=\"language-text\">map</code> loop.</p><h3 id=\"test-deleting-a-note\">Test Deleting a Note</h3><p>Rerun your server and React app if they're not running. Then, open the website again at <code class=\"language-text\">localhost:3000</code>. Click the Delete link for any of your notes. If the note is deleted successfully, an alert will be shown.</p><figure class=\"kg-card kg-image-card\"><img src=\"https://backend.shahednasser.com/content/images/2022/07/Screen-Shot-2022-07-01-at-2.48.53-PM.png\" class=\"kg-image\" alt=\"\" loading=\"lazy\" width=\"1006\" height=\"314\" srcset=\"https://backend.shahednasser.com/content/images/size/w600/2022/07/Screen-Shot-2022-07-01-at-2.48.53-PM.png 600w, https://backend.shahednasser.com/content/images/size/w1000/2022/07/Screen-Shot-2022-07-01-at-2.48.53-PM.png 1000w, https://backend.shahednasser.com/content/images/2022/07/Screen-Shot-2022-07-01-at-2.48.53-PM.png 1006w\" sizes=\"(min-width: 720px) 720px\"></figure><p>After closing the alert, the notes will be fetched again and displayed, if there are any other notes.</p><figure class=\"kg-card kg-image-card\"><img src=\"https://backend.shahednasser.com/content/images/2022/07/Screen-Shot-2022-06-28-at-9.10.04-PM-1.png\" class=\"kg-image\" alt=\"\" loading=\"lazy\" width=\"1446\" height=\"490\" srcset=\"https://backend.shahednasser.com/content/images/size/w600/2022/07/Screen-Shot-2022-06-28-at-9.10.04-PM-1.png 600w, https://backend.shahednasser.com/content/images/size/w1000/2022/07/Screen-Shot-2022-06-28-at-9.10.04-PM-1.png 1000w, https://backend.shahednasser.com/content/images/2022/07/Screen-Shot-2022-06-28-at-9.10.04-PM-1.png 1446w\" sizes=\"(min-width: 720px) 720px\"></figure><h2 id=\"conclusion\">Conclusion</h2><p>Using React (TanStack) Query, you can easily handle server data fetching and manipulation on your website with advanced features such as caching and synchronization across your React app.</p><p>Make sure to check out the <a href=\"https://tanstack.com/query/v4\">official documentation</a> to learn more about what you can do with React Query.</p>","htmlAst":{"type":"root","children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"React Query (now rebranded to TanStack Query) is a React library used to make fetching and manipulating server-side data easier. Using React Query, you can implement, along with data fetching, caching, and synchronization of your data with the server."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In this tutorial, you'll build a simple Node.js server and then learn how to interact with it on a React website using React Query."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please note that this version uses v4 of React Query which is now named TanStack Query."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You can find the code for this tutorial in "},{"type":"element","tagName":"a","properties":{"href":"https://github.com/shahednasser/react-query-tutorial"},"children":[{"type":"text","value":"this GitHub repository"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"h2","properties":{"id":"prerequisites"},"children":[{"type":"text","value":"Prerequisites"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":" Before starting with this tutorial make sure you have "},{"type":"element","tagName":"a","properties":{"href":"https://nodejs.org/en/"},"children":[{"type":"text","value":"Node.js installed"}]},{"type":"text","value":". You need at least version 14."}]},{"type":"element","tagName":"h2","properties":{"id":"server-setup"},"children":[{"type":"text","value":"Server Setup"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In this section, you'll set up a simple Node.js server with an SQLite database. The server has 3 endpoints to fetch, add, and delete notes."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If you already have a server you can skip this section and go to the Website Setup section."}]},{"type":"element","tagName":"h3","properties":{"id":"create-server-project"},"children":[{"type":"text","value":"Create Server Project"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Create a new directory called "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"server"}]},{"type":"text","value":" then initialize a new project using NPM:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"bash"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-bash"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-bash"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"mkdir"}]},{"type":"text","value":" server\n"},{"type":"element","tagName":"span","properties":{"className":["token","builtin","class-name"]},"children":[{"type":"text","value":"cd"}]},{"type":"text","value":" server\n"},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"npm"}]},{"type":"text","value":" init -y"}]}]}]},{"type":"element","tagName":"h3","properties":{"id":"install-dependencies"},"children":[{"type":"text","value":"Install Dependencies"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Then, install the packages you'll need for the development of the server:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"bash"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-bash"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-bash"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"npm"}]},{"type":"text","value":" i express cors body-parser sqlite3 nodemon"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Here's what each of the packages is for:"}]},{"type":"element","tagName":"ol","properties":{},"children":[{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"express"}]},{"type":"text","value":" to create a server using "},{"type":"element","tagName":"a","properties":{"href":"https://expressjs.com/"},"children":[{"type":"text","value":"Express"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"cors"}]},{"type":"text","value":" is an Express middleware used to handle "},{"type":"element","tagName":"a","properties":{"href":"https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS"},"children":[{"type":"text","value":"CORS"}]},{"type":"text","value":" on your server."}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"body-parser"}]},{"type":"text","value":" is an Express middleware used to parse the body of a request."}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sqlite3"}]},{"type":"text","value":" is an SQLite database adapter for Node.js."}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"nodemon"}]},{"type":"text","value":" is a library used to restart the server whenever new changes occur to the files."}]}]},{"type":"element","tagName":"h3","properties":{"id":"create-server"},"children":[{"type":"text","value":"Create Server"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Create the file "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"index.js"}]},{"type":"text","value":" with the following content:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"js"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"const"}]},{"type":"text","value":" express "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"require"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'express'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"const"}]},{"type":"text","value":" app "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"express"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"const"}]},{"type":"text","value":" port "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3001"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"const"}]},{"type":"text","value":" cors "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"require"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'cors'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"const"}]},{"type":"text","value":" sqlite3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"require"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'sqlite3'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"verbose"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"const"}]},{"type":"text","value":" bodyParser "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"require"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'body-parser'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n\napp"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"use"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"bodyParser"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"json"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\napp"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"use"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"cors"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n\napp"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"listen"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"port"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=>"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n  console"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"log"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","template-string"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","template-punctuation","string"]},"children":[{"type":"text","value":"`"}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"Notes app listening on port "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","interpolation-punctuation","punctuation"]},"children":[{"type":"text","value":"${"}]},{"type":"text","value":"port"},{"type":"element","tagName":"span","properties":{"className":["token","interpolation-punctuation","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","template-punctuation","string"]},"children":[{"type":"text","value":"`"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This initializes the server using Express on port "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"3001"}]},{"type":"text","value":". It also uses the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"cors"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"body-parser"}]},{"type":"text","value":" middleware."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Then, in "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"package.json"}]},{"type":"text","value":" add a new script "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"start"}]},{"type":"text","value":" to run the server:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"json"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-json"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-json"]},"children":[{"type":"text","value":"  "},{"type":"element","tagName":"span","properties":{"className":["token","property"]},"children":[{"type":"text","value":"\"scripts\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","property"]},"children":[{"type":"text","value":"\"start\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"nodemon index.js\""}]},{"type":"text","value":"\n  "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]}]}]}]},{"type":"element","tagName":"h3","properties":{"id":"initialize-the-database"},"children":[{"type":"text","value":"Initialize the Database"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"index.js"}]},{"type":"text","value":" before "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"app.listen"}]},{"type":"text","value":" add the following code:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"js"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"const"}]},{"type":"text","value":" db "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"new"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"sqlite3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"Database"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'data.db'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","parameter"]},"children":[{"type":"text","value":"err"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=>"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n  "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"err"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"throw"}]},{"type":"text","value":" err"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n  "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n\n  "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"// create tables if they don't exist"}]},{"type":"text","value":"\n  db"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"serialize"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=>"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n    db"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"run"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","template-string"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","template-punctuation","string"]},"children":[{"type":"text","value":"`"}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"CREATE TABLE IF NOT EXISTS notes (id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT, content TEXT, \n      created_at datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, updated_at datetime NOT NULL DEFAULT CURRENT_TIMESTAMP)"}]},{"type":"element","tagName":"span","properties":{"className":["token","template-punctuation","string"]},"children":[{"type":"text","value":"`"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n  "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This creates a new database if it doesn't exist in the file "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"data.db"}]},{"type":"text","value":". Then, if the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"notes"}]},{"type":"text","value":" table doesn't exist on the database it creates it as well."}]},{"type":"element","tagName":"h3","properties":{"id":"add-endpoints"},"children":[{"type":"text","value":"Add Endpoints"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Following the database code, add the following code to add the endpoints:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"js"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-js"]},"children":[{"type":"text","value":"app"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"get"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'/notes'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","parameter"]},"children":[{"type":"text","value":"req"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" res"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=>"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n  db"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"all"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'SELECT * FROM notes'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","parameter"]},"children":[{"type":"text","value":"err"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" rows"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=>"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"err"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n      console"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"error"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"err"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n      "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" res"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"status"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"500"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"json"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"success"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"false"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"message"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'An error occurred, please try again later'"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n\n    "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" res"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"json"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"success"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"true"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"data"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" rows "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n  "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n\napp"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"get"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'/notes/:id'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","parameter"]},"children":[{"type":"text","value":"req"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" res"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=>"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n  db"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"get"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'SELECT * FROM notes WHERE id = ?'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" req"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"params"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"id"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","parameter"]},"children":[{"type":"text","value":"err"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" row"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=>"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"err"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n      console"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"error"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"err"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n      "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" res"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"status"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"500"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"json"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"success"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"false"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"message"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'An error occurred, please try again later'"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n\n    "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"!"}]},{"type":"text","value":"row"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n      "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" res"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"status"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"404"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"json"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"success"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"false"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"message"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'Note does not exist'"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n\n    "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" res"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"json"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"success"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"true"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"data"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" row "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n  "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n\napp"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"post"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'/notes'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","parameter"]},"children":[{"type":"text","value":"req"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" res"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=>"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n  "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"const"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":" title"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" content "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" req"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"body"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n\n  "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"!"}]},{"type":"text","value":"title "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"||"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"!"}]},{"type":"text","value":"content"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" res"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"status"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"400"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"json"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"success"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"false"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"message"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'title and content are required'"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n  "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n\n  db"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"run"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'INSERT INTO notes (title, content) VALUES (?, ?)'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"title"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" content"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"function"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","parameter"]},"children":[{"type":"text","value":"err"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"err"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n      console"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"error"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"err"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n      "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" res"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"status"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"500"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"json"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"success"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"false"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"message"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'An error occurred, please try again later'"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n\n    "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" res"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"json"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n      "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"success"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"true"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n      "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"data"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n        "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"id"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"this"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"lastID"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n        title"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n        content"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n      "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n  "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n\napp"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"delete"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'/notes/:id'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","parameter"]},"children":[{"type":"text","value":"req"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" res"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=>"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n  "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"const"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":" id "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" req"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"params"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n\n  db"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"get"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'SELECT * FROM notes WHERE id = ?'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"id"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","parameter"]},"children":[{"type":"text","value":"err"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" row"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=>"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"err"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n      console"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"error"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"err"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n      "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" res"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"status"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"500"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"json"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"success"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"false"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"message"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'An error occurred, please try again later'"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n\n    "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"!"}]},{"type":"text","value":"row"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n      "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" res"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"status"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"404"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"json"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"success"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"false"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"message"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'Note does not exist'"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n\n    db"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"run"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'DELETE FROM notes WHERE id = ?'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"id"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","parameter"]},"children":[{"type":"text","value":"error"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=>"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n      "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"error"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n        console"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"error"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"error"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n        "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" res"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"status"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"500"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"json"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"success"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"false"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"message"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'An error occurred, please try again later'"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n      "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n\n      "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" res"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"json"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"success"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"true"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"message"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'Note deleted successfully'"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n  "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Briefly, this creates 4 endpoints:"}]},{"type":"element","tagName":"ol","properties":{},"children":[{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"/notes"}]},{"type":"text","value":" endpoint of the method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"GET"}]},{"type":"text","value":" to fetch all notes."}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"/notes/:id"}]},{"type":"text","value":" endpoint of the method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"GET"}]},{"type":"text","value":" to fetch a note by an ID."}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"/notes"}]},{"type":"text","value":" endpoint of the method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"POST"}]},{"type":"text","value":" to add a note."}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"/notes/:id"}]},{"type":"text","value":" endpoint of the method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"DELETE"}]},{"type":"text","value":" to delete a note."}]}]},{"type":"element","tagName":"h3","properties":{"id":"test-server"},"children":[{"type":"text","value":"Test Server"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Run the following command to start the server:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"bash"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-bash"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-bash"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"npm"}]},{"type":"text","value":" start"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This starts the server on port "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"3001"}]},{"type":"text","value":". You can test it out by sending a request to "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"localhost:3001/notes"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"h2","properties":{"id":"website-setup"},"children":[{"type":"text","value":"Website Setup"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In this section, you'll create the website with Create React App (CRA). This is where you'll make use of React Query."}]},{"type":"element","tagName":"h3","properties":{"id":"create-website-project"},"children":[{"type":"text","value":"Create Website Project"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"To create a new React app, run the following command in a different directory:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"bash"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-bash"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-bash"]},"children":[{"type":"text","value":"npx create-react-app website"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This creates a new React app in the directory "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"website"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"h3","properties":{"id":"install-dependencies-1"},"children":[{"type":"text","value":"Install Dependencies"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Run the following command to change to the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"website"}]},{"type":"text","value":" directory and install the necessary dependencies for the website:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"bash"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-bash"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-bash"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","builtin","class-name"]},"children":[{"type":"text","value":"cd"}]},{"type":"text","value":" website\n"},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"npm"}]},{"type":"text","value":" i @tanstack/react-query tailwindcss postcss autoprefixer @tailwindcss/typography @heroicons/react @windmill/react-ui"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"@tanstack/react-query"}]},{"type":"text","value":" library is the React Query library which is now named TanStack Query. The other libraries are "},{"type":"element","tagName":"a","properties":{"href":"https://tailwindcss.com/"},"children":[{"type":"text","value":"Tailwind CSS"}]},{"type":"text","value":" related libraries to add styling to the website."}]},{"type":"element","tagName":"h3","properties":{"id":"tailwind-css-setup"},"children":[{"type":"text","value":"Tailwind CSS Setup"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This section is optional and is only used to set up Tailwind CSS."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Create the file "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"postcss.config.js"}]},{"type":"text","value":" with the following content:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"js"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-js"]},"children":[{"type":"text","value":"module"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"exports "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n  "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"plugins"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"tailwindcss"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"autoprefixer"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n  "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Also, create the file "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"tailwind.config.js"}]},{"type":"text","value":" with the following content:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"js"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"/** @type {import('tailwindcss').Config} */"}]},{"type":"text","value":"\nmodule"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"exports "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n  "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"content"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"./src/**/*.{js,jsx,ts,tsx}\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n  "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n  "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"theme"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"extend"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n  "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n  "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"plugins"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"require"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'@tailwindcss/typography'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n  "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Then, create the file "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"src/index.css"}]},{"type":"text","value":" with the following content:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"css"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-css"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-css"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","atrule"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","rule"]},"children":[{"type":"text","value":"@tailwind"}]},{"type":"text","value":" base"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","atrule"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","rule"]},"children":[{"type":"text","value":"@tailwind"}]},{"type":"text","value":" components"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","atrule"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","rule"]},"children":[{"type":"text","value":"@tailwind"}]},{"type":"text","value":" utilities"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Finally, in "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"index.js"}]},{"type":"text","value":" import "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"src/index.css"}]},{"type":"text","value":" at the beginning of the file:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"js"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'./index.css'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]}]}]}]},{"type":"element","tagName":"h3","properties":{"id":"use-queryclientprovider"},"children":[{"type":"text","value":"Use QueryClientProvider"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"To use the React Query client in all of your components, you must use it at a high level in your website's components hierarchy. The best place to put it is in "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"src/index.js"}]},{"type":"text","value":" which wraps up your entire website's components."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"src/index.js"}]},{"type":"text","value":" add the following imports at the beginning of the file:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"js"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n  QueryClient"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n  QueryClientProvider"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'@tanstack/react-query'"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Then, initialize a new Query client:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"js"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"const"}]},{"type":"text","value":" queryClient "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"new"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"QueryClient"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Finally, change the parameter passed to "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"root.render"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"js"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-js"]},"children":[{"type":"text","value":"root"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"render"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"\n  "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"QueryClientProvider client"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"queryClient"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"App "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n  "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"QueryClientProvider"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This wraps the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"App"}]},{"type":"text","value":" component which holds the rest of the website's components with "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"QueryClientProvider"}]},{"type":"text","value":". This provider accepts the prop "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"client"}]},{"type":"text","value":" which is an instance of "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"QueryClient"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Now, all components within the website will have access to the Query Client which is used to fetch, cache, and manipulate the server data."}]},{"type":"element","tagName":"h3","properties":{"id":"implement-display-notes"},"children":[{"type":"text","value":"Implement Display Notes"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Fetching data from the server is an act of performing a query. Therefore, you'll use "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"useQuery"}]},{"type":"text","value":" in this section."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You'll display notes in the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"App"}]},{"type":"text","value":" component. These notes are fetched from the server using the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"/notes"}]},{"type":"text","value":" endpoint."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Replace the content of "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"app.js"}]},{"type":"text","value":" with the following content:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"js"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":" PlusIcon"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" RefreshIcon "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'@heroicons/react/solid'"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":" useMutation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" useQuery"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" useQueryClient "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'@tanstack/react-query'"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"function"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"App"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n  "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"const"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":" isLoading"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" isError"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" data"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" error "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"useQuery"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'notes'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" fetchNotes"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n  "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"function"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"fetchNotes"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"fetch"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'http://localhost:3001/notes'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"then"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","parameter"]},"children":[{"type":"text","value":"response"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=>"}]},{"type":"text","value":" response"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"json"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"then"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","parameter"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":" success"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" data "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=>"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n      "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"!"}]},{"type":"text","value":"success"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n        "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"throw"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"new"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Error"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'An error occurred while fetching notes'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n      "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n      "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" data"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n  "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n\n  "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"div className"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"w-screen h-screen overflow-x-hidden bg-red-400 flex flex-col justify-center items-center\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n      "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"div className"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'bg-white w-full md:w-1/2 p-5 text-center rounded shadow-md text-gray-800 prose'"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n        "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"h1"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"Notes"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"h1"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n        "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"isLoading "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"&&"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"RefreshIcon className"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"w-10 h-10 animate-spin mx-auto\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"RefreshIcon"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n        "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"isError "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"&&"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"span className"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'text-red'"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"error"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"message "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"?"}]},{"type":"text","value":" error"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"message "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" error"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"span"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n        "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"!"}]},{"type":"text","value":"isLoading "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"&&"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"!"}]},{"type":"text","value":"isError "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"&&"}]},{"type":"text","value":" data "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"&&"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"!"}]},{"type":"text","value":"data"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"length "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"&&"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"span className"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'text-red-400'"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"You have no notes"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"span"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n        "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"data "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"&&"}]},{"type":"text","value":" data"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"length "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"&&"}]},{"type":"text","value":" data"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"map"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","parameter"]},"children":[{"type":"text","value":"note"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" index"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=>"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"\n          "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"div key"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"note"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"id"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":" className"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","template-string"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","template-punctuation","string"]},"children":[{"type":"text","value":"`"}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"text-left "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","interpolation-punctuation","punctuation"]},"children":[{"type":"text","value":"${"}]},{"type":"text","value":"index "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"!=="}]},{"type":"text","value":" data"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"length "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"?"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'border-b pb-2'"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"''"}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation-punctuation","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","template-punctuation","string"]},"children":[{"type":"text","value":"`"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n            "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"h2"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"note"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"title"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"h2"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n            "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"p"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"note"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"content"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"p"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n            "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"span"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n              "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"button className"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'link text-gray-400'"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"Delete"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"button"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n            "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"span"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n          "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"div"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n        "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n      "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"div"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n      "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"button className"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"mt-2 bg-gray-700 hover:bg-gray-600 rounded-full text-white p-3\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n        "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"PlusIcon className"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'w-5 h-5'"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"PlusIcon"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n      "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"button"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"div"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n  "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"export"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"default"}]},{"type":"text","value":" App"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Here's briefly what's going on in this code snippet:"}]},{"type":"element","tagName":"ol","properties":{},"children":[{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You use "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"useQuery"}]},{"type":"text","value":" to fetch the notes. The first parameter it accepts is a unique key used for caching. The second parameter is the function used to fetch the data. You pass it the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"fetchNotes"}]},{"type":"text","value":" function."}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"useQuery"}]},{"type":"text","value":" returns an object that holds "},{"type":"element","tagName":"a","properties":{"href":"https://tanstack.com/query/v4/docs/reference/useQuery"},"children":[{"type":"text","value":"many variables"}]},{"type":"text","value":". Here, you use 4 of them: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"isLoading"}]},{"type":"text","value":" is a boolean value that determines whether the data is currently being fetched; "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"isError"}]},{"type":"text","value":" is a boolean value that determines if an error occurred. "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"data"}]},{"type":"text","value":" is the data that is fetched from the server; and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"error"}]},{"type":"text","value":" is the error message if "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"isError"}]},{"type":"text","value":" is true."}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"fetchNotes"}]},{"type":"text","value":" function must return a promise that either resolves data or throws an error. In the function, you send a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"GET"}]},{"type":"text","value":" request to "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"localhost:3001/notes"}]},{"type":"text","value":" to fetch the notes. If the data is fetched successfully it is returned in the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"then"}]},{"type":"text","value":" fulfillment function. "}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"In the returned JSX, if "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"isLoading"}]},{"type":"text","value":" is true, a loading icon is shown. If "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"isError"}]},{"type":"text","value":" is true, an error message is shown. If "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"data"}]},{"type":"text","value":" is fetched successfully and has any data in it, the notes are rendered."}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You also show a button with a plus icon to add new notes. You'll implement this later."}]}]},{"type":"element","tagName":"h3","properties":{"id":"test-displaying-notes"},"children":[{"type":"text","value":"Test Displaying Notes"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"To test out what you've implemented so far, make sure your server is still running, then start your React app server with the following command:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"bash"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-bash"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-bash"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"npm"}]},{"type":"text","value":" start"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This runs your React app on "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"localhost:3000"}]},{"type":"text","value":" by default. If you open it in your browser, you'll see a loading icon at first then you'll see no notes as you haven't added any yet."}]},{"type":"element","tagName":"figure","properties":{"className":["kg-card","kg-image-card"]},"children":[{"type":"element","tagName":"img","properties":{"src":"https://backend.shahednasser.com/content/images/2022/07/Screen-Shot-2022-06-28-at-9.10.04-PM.png","className":["kg-image"],"alt":"","loading":"lazy","width":1446,"height":490,"srcSet":["https://backend.shahednasser.com/content/images/size/w600/2022/07/Screen-Shot-2022-06-28-at-9.10.04-PM.png 600w","https://backend.shahednasser.com/content/images/size/w1000/2022/07/Screen-Shot-2022-06-28-at-9.10.04-PM.png 1000w","https://backend.shahednasser.com/content/images/2022/07/Screen-Shot-2022-06-28-at-9.10.04-PM.png 1446w"],"sizes":"(min-width: 720px) 720px"},"children":[]}]},{"type":"element","tagName":"h3","properties":{"id":"implement-add-notes-functionality"},"children":[{"type":"text","value":"Implement Add Notes Functionality"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Adding a note is an act of mutation on the server data. Therefore, you'll be using the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"useMutation"}]},{"type":"text","value":" hook in this section."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You'll create a separate component that shows the form used to add a note."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Create the file "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"src/form.js"}]},{"type":"text","value":" with the following content:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"js"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":" useMutation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" useQueryClient "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'@tanstack/react-query'"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":" useState "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'react'"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"export"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"default"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"function"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"Form"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","parameter"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":" isOpen"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" setIsOpen "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n  "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"const"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"title"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" setTitle"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"useState"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n  "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"const"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"content"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" setContent"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"useState"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n  "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"const"}]},{"type":"text","value":" queryClient "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"useQueryClient"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n  "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"const"}]},{"type":"text","value":" mutation "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"useMutation"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"insertNote"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","function-variable","function"]},"children":[{"type":"text","value":"onSuccess"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=>"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n      "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"setTitle"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n      "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"setContent"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n  "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n  "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"function"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"closeForm"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","parameter"]},"children":[{"type":"text","value":"e"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n    e"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"preventDefault"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"setIsOpen"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"false"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n  "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n\n  "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"function"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"insertNote"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"fetch"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","template-string"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","template-punctuation","string"]},"children":[{"type":"text","value":"`"}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"http://localhost:3001/notes"}]},{"type":"element","tagName":"span","properties":{"className":["token","template-punctuation","string"]},"children":[{"type":"text","value":"`"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n      "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"method"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'POST'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n      "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"headers"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n        "},{"type":"element","tagName":"span","properties":{"className":["token","string-property","property"]},"children":[{"type":"text","value":"'Content-Type'"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'application/json'"}]},{"type":"text","value":"\n      "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n      "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"body"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","constant"]},"children":[{"type":"text","value":"JSON"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"stringify"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n        title"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n        content\n      "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"then"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","parameter"]},"children":[{"type":"text","value":"response"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=>"}]},{"type":"text","value":" response"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"json"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"then"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","parameter"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":" success"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" data "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=>"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n      "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"!"}]},{"type":"text","value":"success"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n        "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"throw"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"new"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Error"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"An error occured\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n      "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n      \n      "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"setIsOpen"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"false"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n      queryClient"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"setQueriesData"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'notes'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","parameter"]},"children":[{"type":"text","value":"old"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=>"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"..."}]},{"type":"text","value":"old"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" data"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n  "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n\n  "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"function"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"handleSubmit"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","parameter"]},"children":[{"type":"text","value":"e"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n    e"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"preventDefault"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n    mutation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"mutate"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n  "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n\n  "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"div className"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","template-string"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","template-punctuation","string"]},"children":[{"type":"text","value":"`"}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"absolute w-full h-full top-0 left-0 z-50 flex justify-center items-center "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","interpolation-punctuation","punctuation"]},"children":[{"type":"text","value":"${"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"!"}]},{"type":"text","value":"isOpen "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"?"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'hidden'"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"''"}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation-punctuation","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","template-punctuation","string"]},"children":[{"type":"text","value":"`"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n      "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"div className"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'bg-black opacity-50 absolute w-full h-full top-0 left-0'"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"div"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n      "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"form className"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'bg-white w-full md:w-1/2 p-5 rounded shadow-md text-gray-800 prose relative'"}]},{"type":"text","value":" \n        onSubmit"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"handleSubmit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n        "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"h2 className"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'text-center'"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"Add Note"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"h2"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n        "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"mutation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"isError "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"&&"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"span className"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'block mb-2 text-red-400'"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"mutation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"error"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"message "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"?"}]},{"type":"text","value":" mutation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"error"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"message "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" mutation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"error"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"span"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n        "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"input type"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"text\""}]},{"type":"text","value":" placeholder"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'Title'"}]},{"type":"text","value":" className"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'rounded-sm w-full border px-2'"}]},{"type":"text","value":" \n          value"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"title"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":" onChange"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","parameter"]},"children":[{"type":"text","value":"e"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=>"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"setTitle"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"e"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"target"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"value"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n        "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"textarea onChange"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","parameter"]},"children":[{"type":"text","value":"e"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=>"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"setContent"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"e"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"target"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"value"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":" \n          className"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"rounded-sm w-full border px-2 mt-2\""}]},{"type":"text","value":" placeholder"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'Content'"}]},{"type":"text","value":" value"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"content"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"textarea"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n        "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"div"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n          "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"button type"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"submit\""}]},{"type":"text","value":" className"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'mt-2 bg-red-400 hover:bg-red-600 text-white p-3 rounded mr-2 disabled:pointer-events-none'"}]},{"type":"text","value":" \n            disabled"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"mutation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"isLoading"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n            Add"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"button"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n          "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"button className"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'mt-2 bg-gray-700 hover:bg-gray-600 text-white p-3 rounded'"}]},{"type":"text","value":"\n            onClick"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"closeForm"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"Cancel"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"button"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n        "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"div"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n      "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"form"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"div"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n  "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Here's a brief explanation of this form"}]},{"type":"element","tagName":"ol","properties":{},"children":[{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"This form acts as a pop-up. It accepts "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"isOpen"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"setIsOpen"}]},{"type":"text","value":" props to determine when the form is opened and handle closing it."}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You use "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"useQueryClient"}]},{"type":"text","value":" to get access to the Query Client. This is necessary to perform a mutation."}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"To handle adding a note on your server and keep all data in your query client synced, you must the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"useMutation"}]},{"type":"text","value":" hook. "}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"useMutation"}]},{"type":"text","value":" hook accepts 2 parameters. Thie first one is the function that will handle the mutation, which in this case is "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"insertNote"}]},{"type":"text","value":". The second parameter is an object of "},{"type":"element","tagName":"a","properties":{"href":"https://tanstack.com/query/v4/docs/reference/useMutation"},"children":[{"type":"text","value":"options"}]},{"type":"text","value":". You pass it one option "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"onSuccess"}]},{"type":"text","value":" which is a function that runs if the mutation is performed successfully. You use this to reset the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"title"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"content"}]},{"type":"text","value":" fields of the form."}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"In "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"insertNote"}]},{"type":"text","value":", you send a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"POST"}]},{"type":"text","value":" request to "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"localhost:3001/notes"}]},{"type":"text","value":" and pass in the body the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"title"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"content"}]},{"type":"text","value":" of the note to be created. If the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"success"}]},{"type":"text","value":" body parameter returned from the server is "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"false"}]},{"type":"text","value":", an error is thrown to signal that the mutation failed. "}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"If the note is added successfully, you change the cached value of the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"notes"}]},{"type":"text","value":" key using the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"queryClient.setQueriesData"}]},{"type":"text","value":" method. This method accepts the key as a first parameter and the new data associated with that key as a second parameter. This updates the data everywhere it's used on your website."}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"In this component you display a form with 2 fields: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"title"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"content"}]},{"type":"text","value":". In the form, you check if an error occurs using "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"mutation.isError"}]},{"type":"text","value":" and get access to the error using "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"mutation.error"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You handle form submission in the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"handleSubmit"}]},{"type":"text","value":" function. Here, you trigger the mutation using "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"mutation.mutate"}]},{"type":"text","value":". This is where the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"insertNote"}]},{"type":"text","value":" function is triggered to add a new note."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Then, in "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"src/app.js"}]},{"type":"text","value":" add the following imports at the beginning of the file:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"js"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" Form "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'./form'"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":" useState "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'react'"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Then, at the beginning of the component add a new state variable to manage wheter the form is opened or not:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"js"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"const"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"isOpen"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" setIsOpen"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"useState"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"false"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Next, add a new function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"addNote"}]},{"type":"text","value":" that just uses "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"setIsOpen"}]},{"type":"text","value":" to open the form:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"js"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"function"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"addNote"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"setIsOpen"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"true"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Finally, in the returned JSX, replace the button with the plus icon with the following:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"js"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"button className"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"mt-2 bg-gray-700 hover:bg-gray-600 rounded-full text-white p-3\""}]},{"type":"text","value":" onClick"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"addNote"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"PlusIcon className"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'w-5 h-5'"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"PlusIcon"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"button"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"Form isOpen"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"isOpen"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":" setIsOpen"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"setIsOpen"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This sets the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"onClick"}]},{"type":"text","value":" handler of the button to "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"addNote"}]},{"type":"text","value":". It also adds the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Form"}]},{"type":"text","value":" component you created earlier as a child component of "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"App"}]},{"type":"text","value":". "}]},{"type":"element","tagName":"h3","properties":{"id":"test-adding-a-note"},"children":[{"type":"text","value":"Test Adding a Note"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Rerun your server and React app if they're not running. Then, open the website again at "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"localhost:3000"}]},{"type":"text","value":". Click on the plus button and a pop up will open with the form to add a new note."}]},{"type":"element","tagName":"figure","properties":{"className":["kg-card","kg-image-card"]},"children":[{"type":"element","tagName":"img","properties":{"src":"https://backend.shahednasser.com/content/images/2022/07/Screen-Shot-2022-07-24-at-6.31.41-PM.png","className":["kg-image"],"alt":"","loading":"lazy","width":1494,"height":792,"srcSet":["https://backend.shahednasser.com/content/images/size/w600/2022/07/Screen-Shot-2022-07-24-at-6.31.41-PM.png 600w","https://backend.shahednasser.com/content/images/size/w1000/2022/07/Screen-Shot-2022-07-24-at-6.31.41-PM.png 1000w","https://backend.shahednasser.com/content/images/2022/07/Screen-Shot-2022-07-24-at-6.31.41-PM.png 1494w"],"sizes":"(min-width: 720px) 720px"},"children":[]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Enter a random title and content then click Add. The pop up form will then close and you can see the new note added."}]},{"type":"element","tagName":"figure","properties":{"className":["kg-card","kg-image-card"]},"children":[{"type":"element","tagName":"img","properties":{"src":"https://backend.shahednasser.com/content/images/2022/07/Screen-Shot-2022-07-24-at-6.32.53-PM.png","className":["kg-image"],"alt":"","loading":"lazy","width":1418,"height":720,"srcSet":["https://backend.shahednasser.com/content/images/size/w600/2022/07/Screen-Shot-2022-07-24-at-6.32.53-PM.png 600w","https://backend.shahednasser.com/content/images/size/w1000/2022/07/Screen-Shot-2022-07-24-at-6.32.53-PM.png 1000w","https://backend.shahednasser.com/content/images/2022/07/Screen-Shot-2022-07-24-at-6.32.53-PM.png 1418w"],"sizes":"(min-width: 720px) 720px"},"children":[]}]},{"type":"element","tagName":"h3","properties":{"id":"implement-delete-note-functionality"},"children":[{"type":"text","value":"Implement Delete Note Functionality"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The last functionality you'll add is deleting notes. Deleting a note is another act of mutation as it manipulates the server's data."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"At the beginning of the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"App"}]},{"type":"text","value":" component in "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"src/app.js"}]},{"type":"text","value":" add the following code:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"js"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"const"}]},{"type":"text","value":" queryClient "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"useQueryClient"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"const"}]},{"type":"text","value":" mutation "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"useMutation"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"deleteNote"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","function-variable","function"]},"children":[{"type":"text","value":"onSuccess"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=>"}]},{"type":"text","value":" queryClient"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"invalidateQueries"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'notes'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Here, you get access to the query client using "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"useQueryClient"}]},{"type":"text","value":". Then, you create a new mutation using "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"useMutation"}]},{"type":"text","value":". You pass it the function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"deleteNote"}]},{"type":"text","value":" (which you'll create next) as a first parameter and an object of options. "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"To the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"onSuccess"}]},{"type":"text","value":" option you pass a function that does one thing. It executes the method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"queryClient.invalidateQueries"}]},{"type":"text","value":". This method marks the cached data for a specific key as outdated, which triggers retrieving the data again. "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"So, once a note is deleted, the query you created earlier that executes the function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"fetchNotes"}]},{"type":"text","value":" will be triggered and the notes will be fetched again. If you had created other queries on your website that use the same key "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"notes"}]},{"type":"text","value":", they'll also be triggered to update their data. "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Next, add the function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"deleteNote"}]},{"type":"text","value":" in the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"App"}]},{"type":"text","value":" component in the same file:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"js"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"function"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"deleteNote"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","parameter"]},"children":[{"type":"text","value":"note"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"fetch"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","template-string"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","template-punctuation","string"]},"children":[{"type":"text","value":"`"}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"http://localhost:3001/notes/"}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","interpolation-punctuation","punctuation"]},"children":[{"type":"text","value":"${"}]},{"type":"text","value":"note"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"id"},{"type":"element","tagName":"span","properties":{"className":["token","interpolation-punctuation","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","template-punctuation","string"]},"children":[{"type":"text","value":"`"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n      "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"method"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'DELETE'"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"then"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","parameter"]},"children":[{"type":"text","value":"response"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=>"}]},{"type":"text","value":" response"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"json"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"then"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","parameter"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":" success"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" message "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=>"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n      "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"!"}]},{"type":"text","value":"success"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n        "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"throw"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"new"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Error"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"message"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n      "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n\n      "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"alert"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"message"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n  "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This function receives the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"note"}]},{"type":"text","value":" to be deleted as a parameter. It sends a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"DELETE"}]},{"type":"text","value":" request to "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"localhost:3001/notes/:id"}]},{"type":"text","value":". If the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"success"}]},{"type":"text","value":" body parameter of the response is "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"false"}]},{"type":"text","value":", an error is thrown. Otherwise, only an alert is shown."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Then, in the returned JSX of the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"App"}]},{"type":"text","value":" component, change how the loading icon and error where shown previously to the following:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"js"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"isLoading "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"||"}]},{"type":"text","value":" mutation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"isLoading"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"&&"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"RefreshIcon className"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"w-10 h-10 animate-spin mx-auto\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"RefreshIcon"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"isError "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"||"}]},{"type":"text","value":" mutation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"isError"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"&&"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"span className"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'text-red'"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"error "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"?"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"error"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"message "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"?"}]},{"type":"text","value":" error"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"message "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" error"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" mutation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"error"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"message"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"span"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This shows the loading icon or the error message for both the query that fetches the notes and the mutation that handles deleting a note."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Finally, find the delete button of a note and add an "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"onClick"}]},{"type":"text","value":" handler:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"js"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"button className"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'link text-gray-400'"}]},{"type":"text","value":" onClick"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=>"}]},{"type":"text","value":" mutation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"mutate"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"note"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"Delete"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"button"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"On click, the mutation responsible for deleting the note is triggered using "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"mutation.mutate"}]},{"type":"text","value":". You pass it the note to delete which is the current note in a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"map"}]},{"type":"text","value":" loop."}]},{"type":"element","tagName":"h3","properties":{"id":"test-deleting-a-note"},"children":[{"type":"text","value":"Test Deleting a Note"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Rerun your server and React app if they're not running. Then, open the website again at "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"localhost:3000"}]},{"type":"text","value":". Click the Delete link for any of your notes. If the note is deleted successfully, an alert will be shown."}]},{"type":"element","tagName":"figure","properties":{"className":["kg-card","kg-image-card"]},"children":[{"type":"element","tagName":"img","properties":{"src":"https://backend.shahednasser.com/content/images/2022/07/Screen-Shot-2022-07-01-at-2.48.53-PM.png","className":["kg-image"],"alt":"","loading":"lazy","width":1006,"height":314,"srcSet":["https://backend.shahednasser.com/content/images/size/w600/2022/07/Screen-Shot-2022-07-01-at-2.48.53-PM.png 600w","https://backend.shahednasser.com/content/images/size/w1000/2022/07/Screen-Shot-2022-07-01-at-2.48.53-PM.png 1000w","https://backend.shahednasser.com/content/images/2022/07/Screen-Shot-2022-07-01-at-2.48.53-PM.png 1006w"],"sizes":"(min-width: 720px) 720px"},"children":[]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"After closing the alert, the notes will be fetched again and displayed, if there are any other notes."}]},{"type":"element","tagName":"figure","properties":{"className":["kg-card","kg-image-card"]},"children":[{"type":"element","tagName":"img","properties":{"src":"https://backend.shahednasser.com/content/images/2022/07/Screen-Shot-2022-06-28-at-9.10.04-PM-1.png","className":["kg-image"],"alt":"","loading":"lazy","width":1446,"height":490,"srcSet":["https://backend.shahednasser.com/content/images/size/w600/2022/07/Screen-Shot-2022-06-28-at-9.10.04-PM-1.png 600w","https://backend.shahednasser.com/content/images/size/w1000/2022/07/Screen-Shot-2022-06-28-at-9.10.04-PM-1.png 1000w","https://backend.shahednasser.com/content/images/2022/07/Screen-Shot-2022-06-28-at-9.10.04-PM-1.png 1446w"],"sizes":"(min-width: 720px) 720px"},"children":[]}]},{"type":"element","tagName":"h2","properties":{"id":"conclusion"},"children":[{"type":"text","value":"Conclusion"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Using React (TanStack) Query, you can easily handle server data fetching and manipulation on your website with advanced features such as caching and synchronization across your React app."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Make sure to check out the "},{"type":"element","tagName":"a","properties":{"href":"https://tanstack.com/query/v4"},"children":[{"type":"text","value":"official documentation"}]},{"type":"text","value":" to learn more about what you can do with React Query."}]}],"data":{"quirksMode":false}},"tableOfContents":[{"id":"prerequisites","heading":"Prerequisites"},{"id":"server-setup","heading":"Server Setup","items":[{"id":"create-server-project","heading":"Create Server Project"},{"id":"install-dependencies","heading":"Install Dependencies"},{"id":"create-server","heading":"Create Server"},{"id":"initialize-the-database","heading":"Initialize the Database"},{"id":"add-endpoints","heading":"Add Endpoints"},{"id":"test-server","heading":"Test Server"}]},{"id":"website-setup","heading":"Website Setup","items":[{"id":"create-website-project","heading":"Create Website Project"},{"id":"install-dependencies-1","heading":"Install Dependencies"},{"id":"tailwind-css-setup","heading":"Tailwind CSS Setup"},{"id":"use-queryclientprovider","heading":"Use QueryClientProvider"},{"id":"implement-display-notes","heading":"Implement Display Notes"},{"id":"test-displaying-notes","heading":"Test Displaying Notes"},{"id":"implement-add-notes-functionality","heading":"Implement Add Notes Functionality"},{"id":"test-adding-a-note","heading":"Test Adding a Note"},{"id":"implement-delete-note-functionality","heading":"Implement Delete Note Functionality"},{"id":"test-deleting-a-note","heading":"Test Deleting a Note"}]},{"id":"conclusion","heading":"Conclusion"}]},"featureImageSharp":{"base":"React--TanStack--Query-Tutorial-for-Beginners.jpg","publicURL":"/static/49810f1cc6a34556c1ce36e3061364ce/React--TanStack--Query-Tutorial-for-Beginners.jpg","imageMeta":{"width":1560,"height":876},"childImageSharp":{"fluid":{"base64":"data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAFwAAAwEAAAAAAAAAAAAAAAAAAAECBf/EABQBAQAAAAAAAAAAAAAAAAAAAAD/2gAMAwEAAhADEAAAAduqBDD/xAAYEAACAwAAAAAAAAAAAAAAAAAAARESIP/aAAgBAQABBQKxLz//xAAUEQEAAAAAAAAAAAAAAAAAAAAQ/9oACAEDAQE/AT//xAAUEQEAAAAAAAAAAAAAAAAAAAAQ/9oACAECAQE/AT//xAAXEAADAQAAAAAAAAAAAAAAAAAAATEg/9oACAEBAAY/AoyZ/8QAGhABAAIDAQAAAAAAAAAAAAAAAQARIDFhkf/aAAgBAQABPyFQ1Lp9hbsrD//aAAwDAQACAAMAAAAQow//xAAUEQEAAAAAAAAAAAAAAAAAAAAQ/9oACAEDAQE/ED//xAAUEQEAAAAAAAAAAAAAAAAAAAAQ/9oACAECAQE/ED//xAAcEAEAAgEFAAAAAAAAAAAAAAABABEhIDFBUWH/2gAIAQEAAT8QUh65DDBDYx0jC08Lej//2Q==","aspectRatio":1.7857142857142858,"src":"/static/49810f1cc6a34556c1ce36e3061364ce/ea4ab/React--TanStack--Query-Tutorial-for-Beginners.jpg","srcSet":"/static/49810f1cc6a34556c1ce36e3061364ce/477ba/React--TanStack--Query-Tutorial-for-Beginners.jpg 175w,\n/static/49810f1cc6a34556c1ce36e3061364ce/06776/React--TanStack--Query-Tutorial-for-Beginners.jpg 350w,\n/static/49810f1cc6a34556c1ce36e3061364ce/ea4ab/React--TanStack--Query-Tutorial-for-Beginners.jpg 700w,\n/static/49810f1cc6a34556c1ce36e3061364ce/3055e/React--TanStack--Query-Tutorial-for-Beginners.jpg 1050w,\n/static/49810f1cc6a34556c1ce36e3061364ce/eff08/React--TanStack--Query-Tutorial-for-Beginners.jpg 1400w,\n/static/49810f1cc6a34556c1ce36e3061364ce/81a53/React--TanStack--Query-Tutorial-for-Beginners.jpg 1560w","srcWebp":"/static/49810f1cc6a34556c1ce36e3061364ce/89afa/React--TanStack--Query-Tutorial-for-Beginners.webp","srcSetWebp":"/static/49810f1cc6a34556c1ce36e3061364ce/9fca7/React--TanStack--Query-Tutorial-for-Beginners.webp 175w,\n/static/49810f1cc6a34556c1ce36e3061364ce/37a4e/React--TanStack--Query-Tutorial-for-Beginners.webp 350w,\n/static/49810f1cc6a34556c1ce36e3061364ce/89afa/React--TanStack--Query-Tutorial-for-Beginners.webp 700w,\n/static/49810f1cc6a34556c1ce36e3061364ce/78e7a/React--TanStack--Query-Tutorial-for-Beginners.webp 1050w,\n/static/49810f1cc6a34556c1ce36e3061364ce/03d34/React--TanStack--Query-Tutorial-for-Beginners.webp 1400w,\n/static/49810f1cc6a34556c1ce36e3061364ce/f5845/React--TanStack--Query-Tutorial-for-Beginners.webp 1560w","sizes":"(max-width: 700px) 100vw, 700px"}}}}},{"node":{"id":"Ghost__Post__6263f18539840e1ac287510b","title":"How to use Transitions in React 18","slug":"how-to-use-transitions-in-react-18","featured":true,"feature_image":"https://backend.shahednasser.com/content/images/2022/04/How-to-use-Transitions-in-React-18-2.jpg","excerpt":"In this tutorial, you'll learn more about Transitions in React 18 and see them in action.","custom_excerpt":"In this tutorial, you'll learn more about Transitions in React 18 and see them in action.","visibility":"public","created_at_pretty":"23 Apr 2022","published_at_pretty":"25 Apr 2022","updated_at_pretty":"25 Apr 2022","created_at":"2022-04-23T12:31:01.000+00:00","published_at":"2022-04-25T09:13:48.000+00:00","updated_at":"2022-04-25T09:13:48.000+00:00","meta_title":null,"meta_description":null,"og_description":null,"og_image":null,"og_title":null,"twitter_description":null,"twitter_image":null,"twitter_title":null,"authors":[{"slug":"shahed","url":"https://backend.shahednasser.com/author/shahed/","name":"Shahed Nasser","bio":null,"cover_image":null,"profile_image":"https://backend.shahednasser.com/content/images/2022/03/IMG_0591.jpg","location":null,"website":null,"twitter":null,"facebook":null,"meta_title":null,"meta_description":null,"coverImageSharp":null,"profileImageSharp":null}],"primary_author":{"slug":"shahed","url":"https://backend.shahednasser.com/author/shahed/","name":"Shahed Nasser","bio":null,"cover_image":null,"profile_image":"https://backend.shahednasser.com/content/images/2022/03/IMG_0591.jpg","location":null,"website":null,"twitter":null,"facebook":null,"meta_title":null,"meta_description":null,"coverImageSharp":null,"profileImageSharp":{"base":"IMG_0591.jpg","publicURL":"/static/ceb49c3c631485453e71e00d7f84b069/IMG_0591.jpg","imageMeta":{"width":1182,"height":1179},"childImageSharp":{"fluid":{"base64":"data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAUABQDASIAAhEBAxEB/8QAGAABAQEBAQAAAAAAAAAAAAAAAAMEAQL/xAAWAQEBAQAAAAAAAAAAAAAAAAADBAL/2gAMAwEAAhADEAAAAdXiFM6i0CohUWXoKn//xAAcEAACAgIDAAAAAAAAAAAAAAACAwESBBEhM0H/2gAIAQEAAQUCWySE3WEr7SzbXjAj4iKty+sOQ//EABYRAQEBAAAAAAAAAAAAAAAAAAERIP/aAAgBAwEBPwEhj//EABQRAQAAAAAAAAAAAAAAAAAAACD/2gAIAQIBAT8BH//EAB4QAAIBBAMBAAAAAAAAAAAAAAABIRESMUECECJx/9oACAEBAAY/ApVGWvOjzgtUwLlTZA0sdL4f/8QAHBAAAwACAwEAAAAAAAAAAAAAAAERITFBkbHB/9oACAEBAAE/IahkCy+N2GwZpjQiJHJCspUFY0QrSi+HqiW2rgf/2gAMAwEAAgADAAAAEPw3/wD/xAAYEQEBAAMAAAAAAAAAAAAAAAAAARExQf/aAAgBAwEBPxCtjDqP/8QAFxEBAQEBAAAAAAAAAAAAAAAAAQAxEf/aAAgBAgEBPxBFus6Tt//EAB8QAQEAAgIBBQAAAAAAAAAAAAERACExQWFRcYGR0f/aAAgBAQABPxAuaBPPzkO1wyX7F4wkwXanfZrFQgeqE9JgS14vVOvrERIJomVBKwt2jebAeP0yVa8h1n//2Q==","aspectRatio":1,"src":"/static/ceb49c3c631485453e71e00d7f84b069/31709/IMG_0591.jpg","srcSet":"/static/ceb49c3c631485453e71e00d7f84b069/f340b/IMG_0591.jpg 28w,\n/static/ceb49c3c631485453e71e00d7f84b069/22d64/IMG_0591.jpg 55w,\n/static/ceb49c3c631485453e71e00d7f84b069/31709/IMG_0591.jpg 110w,\n/static/ceb49c3c631485453e71e00d7f84b069/aa249/IMG_0591.jpg 165w,\n/static/ceb49c3c631485453e71e00d7f84b069/0dc33/IMG_0591.jpg 220w,\n/static/ceb49c3c631485453e71e00d7f84b069/d8257/IMG_0591.jpg 1182w","srcWebp":"/static/ceb49c3c631485453e71e00d7f84b069/8678c/IMG_0591.webp","srcSetWebp":"/static/ceb49c3c631485453e71e00d7f84b069/59cda/IMG_0591.webp 28w,\n/static/ceb49c3c631485453e71e00d7f84b069/7da75/IMG_0591.webp 55w,\n/static/ceb49c3c631485453e71e00d7f84b069/8678c/IMG_0591.webp 110w,\n/static/ceb49c3c631485453e71e00d7f84b069/f282e/IMG_0591.webp 165w,\n/static/ceb49c3c631485453e71e00d7f84b069/a7b21/IMG_0591.webp 220w,\n/static/ceb49c3c631485453e71e00d7f84b069/63099/IMG_0591.webp 1182w","sizes":"(max-width: 110px) 100vw, 110px"}}}},"primary_tag":{"slug":"react","url":"https://backend.shahednasser.com/tag/react/","name":"React","visibility":"public","feature_image":"https://images.unsplash.com/photo-1581276879432-15e50529f34b?crop=entropy&cs=tinysrgb&fit=max&fm=jpg&ixid=MnwxMTc3M3wwfDF8c2VhcmNofDN8fHJlYWN0fGVufDB8fHx8MTYyMjYzMzI0MA&ixlib=rb-1.2.1&q=80&w=2000","description":"Learn more about React through tutorials, articles, and tips.","meta_title":null,"meta_description":null,"featureImageSharp":{"base":"photo-1581276879432-15e50529f34b.jpg","publicURL":"/static/7140ea32e1157ba61402d5d67ab846d4/photo-1581276879432-15e50529f34b.jpg","imageMeta":{"width":2000,"height":1333},"childImageSharp":{"fluid":{"base64":"data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAANABQDASIAAhEBAxEB/8QAFwAAAwEAAAAAAAAAAAAAAAAAAAIDBf/EABUBAQEAAAAAAAAAAAAAAAAAAAAB/9oADAMBAAIQAxAAAAHJai1MBP/EABcQAAMBAAAAAAAAAAAAAAAAAAABEBH/2gAIAQEAAQUCuDU//8QAFBEBAAAAAAAAAAAAAAAAAAAAEP/aAAgBAwEBPwE//8QAFREBAQAAAAAAAAAAAAAAAAAAEBH/2gAIAQIBAT8Bh//EABQQAQAAAAAAAAAAAAAAAAAAACD/2gAIAQEABj8CX//EABgQAQEBAQEAAAAAAAAAAAAAAAABESFR/9oACAEBAAE/IYy+rHKDH//aAAwDAQACAAMAAAAQC9//xAAVEQEBAAAAAAAAAAAAAAAAAAAQEf/aAAgBAwEBPxCn/8QAFREBAQAAAAAAAAAAAAAAAAAAARD/2gAIAQIBAT8QUT//xAAaEAADAQADAAAAAAAAAAAAAAAAAREhQWFx/9oACAEBAAE/EMY0neijzwMrrVEm7auUQPNGlP/Z","aspectRatio":1.5028901734104045,"src":"/static/7140ea32e1157ba61402d5d67ab846d4/d5c54/photo-1581276879432-15e50529f34b.jpg","srcSet":"/static/7140ea32e1157ba61402d5d67ab846d4/65d8c/photo-1581276879432-15e50529f34b.jpg 260w,\n/static/7140ea32e1157ba61402d5d67ab846d4/c5f21/photo-1581276879432-15e50529f34b.jpg 520w,\n/static/7140ea32e1157ba61402d5d67ab846d4/d5c54/photo-1581276879432-15e50529f34b.jpg 1040w,\n/static/7140ea32e1157ba61402d5d67ab846d4/81a53/photo-1581276879432-15e50529f34b.jpg 1560w,\n/static/7140ea32e1157ba61402d5d67ab846d4/4e5f3/photo-1581276879432-15e50529f34b.jpg 2000w","srcWebp":"/static/7140ea32e1157ba61402d5d67ab846d4/e4875/photo-1581276879432-15e50529f34b.webp","srcSetWebp":"/static/7140ea32e1157ba61402d5d67ab846d4/dc8f3/photo-1581276879432-15e50529f34b.webp 260w,\n/static/7140ea32e1157ba61402d5d67ab846d4/2db4b/photo-1581276879432-15e50529f34b.webp 520w,\n/static/7140ea32e1157ba61402d5d67ab846d4/e4875/photo-1581276879432-15e50529f34b.webp 1040w,\n/static/7140ea32e1157ba61402d5d67ab846d4/f5845/photo-1581276879432-15e50529f34b.webp 1560w,\n/static/7140ea32e1157ba61402d5d67ab846d4/49d6b/photo-1581276879432-15e50529f34b.webp 2000w","sizes":"(max-width: 1040px) 100vw, 1040px"}}}},"tags":[{"slug":"react","url":"https://backend.shahednasser.com/tag/react/","name":"React","visibility":"public","feature_image":"https://images.unsplash.com/photo-1581276879432-15e50529f34b?crop=entropy&cs=tinysrgb&fit=max&fm=jpg&ixid=MnwxMTc3M3wwfDF8c2VhcmNofDN8fHJlYWN0fGVufDB8fHx8MTYyMjYzMzI0MA&ixlib=rb-1.2.1&q=80&w=2000","description":"Learn more about React through tutorials, articles, and tips.","meta_title":null,"meta_description":null,"featureImageSharp":null}],"plaintext":"React 18 came out at the end of March with a bundle of new features. One of these new features is Transitions.\n\nIn this tutorial, you'll learn more about Transitions in React 18 and see them in action.\n\n\nDefinition\n\nYou have 2 types of transitions. Urgent transitions and non-urgent transitions.\n\n\nUrgent Transitions\n\nUrgent transitions are transitions that the user needs to see instantly. For example, if the user changes their profile name and saves it, they should be able to see the change in the displayed profile name in the navigation bar.\n\nUrgent transitions are done the same way you've been setting a state before:\n\nconst [name, setName] = useState('');\n\nfunction handleChange (e) {\n\tsetName(e.target.value); //urgent transition\n}\n\n\nNon-Urgent Transitions\n\nNon-urgent transitions are transitions that are ok to be delayed a little. For example, if the user is performing a search, it's ok for the results to appear not so instantly.\n\nThere are 2 ways to start a non-urgent transition. The first one is using the hook useTransition:\n\nimport {useTransition, useState} from 'react';\n\nexport default function MyApp() {\n    const [results, setResults] = useState([]);\n    const [pending, startTransition] = useTransition();\n    \n    function handleChange(e) {\n        let tempResults = [];\n        ... // set results from APIs\n        startTransition(() => {\n            setResults(tempResults);\n        });\n    }\n}\n\nThe hook returns the boolean variable pending which indicates whether the transition is active or not. It can be used to show a loading component.\n\nThe hook also returns a function startTransition that accepts a callback function in which you set the state. The state will not be set immediately.\n\nThe second way to start a non-urgent transition is using the function startTransition imported directly from React:\n\nimport {startTransition} from 'react';\n\nexport default function MyApp() {\n    const [results, setResults] = useState([]);\n    \n    function handleChange(e) {\n        let tempResults = [];\n        ... // set results from APIs\n        startTransition(() => {\n            setResults(tempResults);\n        });\n    }\n}\n\nThis approach does not give you access to a variable like isPending to check whether the transition is active or not.\n\nThis approach is mainly available for places in your code when you can't use hooks like useTransition.\n\n\nUsage Example\n\nIn this example, you'll be creating a number input that accepts a large number of images to be shown. Then, random images will be retrieved using Falso.\n\nStart by creating a new React app if you don't have one available:\n\nnpx create-react-app my-app\n\nNext, change into the directory my-app:\n\ncd my-app\n\nThen, install the Falso library:\n\nnpm i @ngneat/falso\n\nNow, open src/App.js and change it to the following:\n\nimport './App.css';\n\nimport { useState, useTransition } from 'react';\n\nimport { randImg } from '@ngneat/falso';\n\nfunction App() {\n  const [number, setNumber] = useState(5000);\n  const [images, setImages] = useState([])\n  const [isPending, startTransition] = useTransition();\n\n  function showImages() {\n    //TODO add transition\n  }\n\n  return (\n    <div style={{\n      padding: '10px'\n    }}>\n      <h1>Images</h1>\n      <div>\n        <label>Number of images</label>\n        <input type=\"number\" min=\"1\" max=\"10000\" value={number} onChange={(e) => setNumber(e.target.value)} style={{\n          display: 'block',\n          marginTop: '10px',\n          width: '3rem'\n        }} />\n        <button type=\"button\" onClick={showImages} style={{marginTop: '10px'}}>Show Images</button>\n      </div>\n      <div>\n        <span>Number selected: {number}</span><br/>\n        <span>Results:</span>\n        {isPending && <span>Loading...</span>}\n        {!isPending && images.length > 0 && images}\n      </div>\n    </div>\n  );\n}\n\nexport default App;\n\n\nYou first create 2 state variables number and images. You also use useTransition which gives you access to isPending and startTransition.\n\nIn the returned JSX, you show a number input and a button that will later retrieve the images on click.\n\nBelow the input and button, the number entered by the user in the input will be shown. Notice that in the onChange event handler for the input the name is updated urgently. This will show the number instantly as it is updated by the user.\n\nLet's test it out now. Run the website server:\n\nnpm start\n\nThis will open the website in your browser. If you try to update the input, you'll notice that the number displayed below it will update instantly.\n\n0:00/1×\n\nNow, let's test the non-urgent transition. In showImages replace the TODO with the following code:\n\nconst imgSources = randImg({length: number}).map((url, index) => <img src={`${url}?v=${index}`} key={index} />);\nstartTransition(() => {\n\tsetImages(imgSources);\n})\n\nThis will get the images using the falso library and inside startTransition with set the images state variable.\n\nNotice that in the returned JSX of the function we use isPending to indicate whether to show \"Loading...\" or not.\n\nIf you try clicking on the button now, the \"Loading...\" text will show first, and then the images will be shown gradually.\n\n0:00/1×\n\n\nConclusion\n\nTransitions in React 18 allow you to optimize your user experience, especially for tasks or features that require some time to load. You can now use Transitions in React 18 to differentiate between instate updates and updates that can be delayed, and show in the UI any necessary loading more efficiently for those that require more time.","html":"<p><a href=\"https://reactjs.org/blog/2022/03/29/react-v18.html\">React 18 came out at the end of March</a> with a bundle of new features. One of these new features is Transitions.</p><p>In this tutorial, you'll learn more about Transitions in React 18 and see them in action.</p><h2 id=\"definition\">Definition</h2><p>You have 2 types of transitions. Urgent transitions and non-urgent transitions.</p><h3 id=\"urgent-transitions\">Urgent Transitions</h3><p>Urgent transitions are transitions that the user needs to see instantly. For example, if the user changes their profile name and saves it, they should be able to see the change in the displayed profile name in the navigation bar.</p><p>Urgent transitions are done the same way you've been setting a state before:</p><pre><code class=\"language-js\">const [name, setName] = useState('');\n\nfunction handleChange (e) {\n\tsetName(e.target.value); //urgent transition\n}</code></pre><h3 id=\"non-urgent-transitions\">Non-Urgent Transitions</h3><p>Non-urgent transitions are transitions that are ok to be delayed a little. For example, if the user is performing a search, it's ok for the results to appear not so instantly.</p><p>There are 2 ways to start a non-urgent transition. The first one is using the hook <code>useTransition</code>: </p><pre><code class=\"language-js\">import {useTransition, useState} from 'react';\n\nexport default function MyApp() {\n    const [results, setResults] = useState([]);\n    const [pending, startTransition] = useTransition();\n    \n    function handleChange(e) {\n        let tempResults = [];\n        ... // set results from APIs\n        startTransition(() =&gt; {\n            setResults(tempResults);\n        });\n    }\n}</code></pre><p>The hook returns the boolean variable <code>pending</code> which indicates whether the transition is active or not. It can be used to show a loading component.</p><p>The hook also returns a function <code>startTransition</code> that accepts a callback function in which you set the state. The state will not be set immediately.</p><p>The second way to start a non-urgent transition is using the function <code>startTransition</code> imported directly from React:</p><pre><code class=\"language-js\">import {startTransition} from 'react';\n\nexport default function MyApp() {\n    const [results, setResults] = useState([]);\n    \n    function handleChange(e) {\n        let tempResults = [];\n        ... // set results from APIs\n        startTransition(() =&gt; {\n            setResults(tempResults);\n        });\n    }\n}</code></pre><p>This approach does not give you access to a variable like <code>isPending</code> to check whether the transition is active or not.</p><p>This approach is mainly available for places in your code when you can't use hooks like <code>useTransition</code>.</p><h2 id=\"usage-example\">Usage Example</h2><p>In this example, you'll be creating a number input that accepts a large number of images to be shown. Then, random images will be retrieved using <a href=\"https://ngneat.github.io/falso/\">Falso</a>.</p><p>Start by creating a new React app if you don't have one available:</p><pre><code class=\"language-bash\">npx create-react-app my-app</code></pre><p>Next, change into the directory <code>my-app</code>:</p><pre><code class=\"language-bash\">cd my-app</code></pre><p>Then, install the Falso library:</p><pre><code class=\"language-bash\">npm i @ngneat/falso</code></pre><p>Now, open <code>src/App.js</code> and change it to the following:</p><pre><code class=\"language-js\">import './App.css';\n\nimport { useState, useTransition } from 'react';\n\nimport { randImg } from '@ngneat/falso';\n\nfunction App() {\n  const [number, setNumber] = useState(5000);\n  const [images, setImages] = useState([])\n  const [isPending, startTransition] = useTransition();\n\n  function showImages() {\n    //TODO add transition\n  }\n\n  return (\n    &lt;div style={{\n      padding: '10px'\n    }}&gt;\n      &lt;h1&gt;Images&lt;/h1&gt;\n      &lt;div&gt;\n        &lt;label&gt;Number of images&lt;/label&gt;\n        &lt;input type=\"number\" min=\"1\" max=\"10000\" value={number} onChange={(e) =&gt; setNumber(e.target.value)} style={{\n          display: 'block',\n          marginTop: '10px',\n          width: '3rem'\n        }} /&gt;\n        &lt;button type=\"button\" onClick={showImages} style={{marginTop: '10px'}}&gt;Show Images&lt;/button&gt;\n      &lt;/div&gt;\n      &lt;div&gt;\n        &lt;span&gt;Number selected: {number}&lt;/span&gt;&lt;br/&gt;\n        &lt;span&gt;Results:&lt;/span&gt;\n        {isPending &amp;&amp; &lt;span&gt;Loading...&lt;/span&gt;}\n        {!isPending &amp;&amp; images.length &gt; 0 &amp;&amp; images}\n      &lt;/div&gt;\n    &lt;/div&gt;\n  );\n}\n\nexport default App;\n</code></pre><p>You first create 2 state variables <code>number</code> and <code>images</code>. You also use <code>useTransition</code> which gives you access to <code>isPending</code> and <code>startTransition</code>.</p><p>In the returned JSX, you show a number input and a button that will later retrieve the images on click.</p><p>Below the input and button, the number entered by the user in the input will be shown. Notice that in the <code>onChange</code> event handler for the input the name is updated <strong>urgently. </strong>This will show the number instantly as it is updated by the user.</p><p>Let's test it out now. Run the website server:</p><pre><code class=\"language-bash\">npm start</code></pre><p>This will open the website in your browser. If you try to update the input, you'll notice that the number displayed below it will update instantly.</p><figure class=\"kg-card kg-video-card\"><div class=\"kg-video-container\"><video src=\"https://backend.shahednasser.com/content/media/2022/04/Screen-Recording-2022-04-23-at-3.45.38-PM.mp4\" poster=\"https://img.spacergif.org/v1/798x692/0a/spacer.png\" width=\"798\" height=\"692\" playsinline preload=\"metadata\" style=\"background: transparent url('https://backend.shahednasser.com/content/images/2022/04/media-thumbnail-ember409.jpg') 50% 50% / cover no-repeat;\" /></video><div class=\"kg-video-overlay\"><button class=\"kg-video-large-play-icon\"><svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 24 24\"><path d=\"M23.14 10.608 2.253.164A1.559 1.559 0 0 0 0 1.557v20.887a1.558 1.558 0 0 0 2.253 1.392L23.14 13.393a1.557 1.557 0 0 0 0-2.785Z\"/></svg></button></div><div class=\"kg-video-player-container\"><div class=\"kg-video-player\"><button class=\"kg-video-play-icon\"><svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 24 24\"><path d=\"M23.14 10.608 2.253.164A1.559 1.559 0 0 0 0 1.557v20.887a1.558 1.558 0 0 0 2.253 1.392L23.14 13.393a1.557 1.557 0 0 0 0-2.785Z\"/></svg></button><button class=\"kg-video-pause-icon kg-video-hide\"><svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 24 24\"><rect x=\"3\" y=\"1\" width=\"7\" height=\"22\" rx=\"1.5\" ry=\"1.5\"/><rect x=\"14\" y=\"1\" width=\"7\" height=\"22\" rx=\"1.5\" ry=\"1.5\"/></svg></button><span class=\"kg-video-current-time\">0:00</span><div class=\"kg-video-time\">/<span class=\"kg-video-duration\"></span></div><input type=\"range\" class=\"kg-video-seek-slider\" max=\"100\" value=\"0\"><button class=\"kg-video-playback-rate\">1&#215;</button><button class=\"kg-video-unmute-icon\"><svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 24 24\"><path d=\"M15.189 2.021a9.728 9.728 0 0 0-7.924 4.85.249.249 0 0 1-.221.133H5.25a3 3 0 0 0-3 3v2a3 3 0 0 0 3 3h1.794a.249.249 0 0 1 .221.133 9.73 9.73 0 0 0 7.924 4.85h.06a1 1 0 0 0 1-1V3.02a1 1 0 0 0-1.06-.998Z\"/></svg></button><button class=\"kg-video-mute-icon kg-video-hide\"><svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 24 24\"><path d=\"M16.177 4.3a.248.248 0 0 0 .073-.176v-1.1a1 1 0 0 0-1.061-1 9.728 9.728 0 0 0-7.924 4.85.249.249 0 0 1-.221.133H5.25a3 3 0 0 0-3 3v2a3 3 0 0 0 3 3h.114a.251.251 0 0 0 .177-.073ZM23.707 1.706A1 1 0 0 0 22.293.292l-22 22a1 1 0 0 0 0 1.414l.009.009a1 1 0 0 0 1.405-.009l6.63-6.631A.251.251 0 0 1 8.515 17a.245.245 0 0 1 .177.075 10.081 10.081 0 0 0 6.5 2.92 1 1 0 0 0 1.061-1V9.266a.247.247 0 0 1 .073-.176Z\"/></svg></button><input type=\"range\" class=\"kg-video-volume-slider\" max=\"100\" value=\"100\"></div></div></div></figure><p>Now, let's test the non-urgent transition. In <code>showImages</code> replace the <code>TODO</code> with the following code:</p><pre><code class=\"language-js\">const imgSources = randImg({length: number}).map((url, index) =&gt; &lt;img src={`${url}?v=${index}`} key={index} /&gt;);\nstartTransition(() =&gt; {\n\tsetImages(imgSources);\n})</code></pre><p>This will get the images using the <code>falso</code> library and inside <code>startTransition</code> with set the <code>images</code> state variable.</p><p>Notice that in the returned JSX of the function we use <code>isPending</code> to indicate whether to show \"Loading...\" or not.</p><p>If you try clicking on the button now, the \"Loading...\" text will show first, and then the images will be shown gradually.</p><figure class=\"kg-card kg-video-card\"><div class=\"kg-video-container\"><video src=\"https://backend.shahednasser.com/content/media/2022/04/Screen-Recording-2022-04-23-at-4.01.34-PM.mp4\" poster=\"https://img.spacergif.org/v1/794x896/0a/spacer.png\" width=\"794\" height=\"896\" playsinline preload=\"metadata\" style=\"background: transparent url('https://backend.shahednasser.com/content/images/2022/04/media-thumbnail-ember441.jpg') 50% 50% / cover no-repeat;\" /></video><div class=\"kg-video-overlay\"><button class=\"kg-video-large-play-icon\"><svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 24 24\"><path d=\"M23.14 10.608 2.253.164A1.559 1.559 0 0 0 0 1.557v20.887a1.558 1.558 0 0 0 2.253 1.392L23.14 13.393a1.557 1.557 0 0 0 0-2.785Z\"/></svg></button></div><div class=\"kg-video-player-container\"><div class=\"kg-video-player\"><button class=\"kg-video-play-icon\"><svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 24 24\"><path d=\"M23.14 10.608 2.253.164A1.559 1.559 0 0 0 0 1.557v20.887a1.558 1.558 0 0 0 2.253 1.392L23.14 13.393a1.557 1.557 0 0 0 0-2.785Z\"/></svg></button><button class=\"kg-video-pause-icon kg-video-hide\"><svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 24 24\"><rect x=\"3\" y=\"1\" width=\"7\" height=\"22\" rx=\"1.5\" ry=\"1.5\"/><rect x=\"14\" y=\"1\" width=\"7\" height=\"22\" rx=\"1.5\" ry=\"1.5\"/></svg></button><span class=\"kg-video-current-time\">0:00</span><div class=\"kg-video-time\">/<span class=\"kg-video-duration\"></span></div><input type=\"range\" class=\"kg-video-seek-slider\" max=\"100\" value=\"0\"><button class=\"kg-video-playback-rate\">1&#215;</button><button class=\"kg-video-unmute-icon\"><svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 24 24\"><path d=\"M15.189 2.021a9.728 9.728 0 0 0-7.924 4.85.249.249 0 0 1-.221.133H5.25a3 3 0 0 0-3 3v2a3 3 0 0 0 3 3h1.794a.249.249 0 0 1 .221.133 9.73 9.73 0 0 0 7.924 4.85h.06a1 1 0 0 0 1-1V3.02a1 1 0 0 0-1.06-.998Z\"/></svg></button><button class=\"kg-video-mute-icon kg-video-hide\"><svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 24 24\"><path d=\"M16.177 4.3a.248.248 0 0 0 .073-.176v-1.1a1 1 0 0 0-1.061-1 9.728 9.728 0 0 0-7.924 4.85.249.249 0 0 1-.221.133H5.25a3 3 0 0 0-3 3v2a3 3 0 0 0 3 3h.114a.251.251 0 0 0 .177-.073ZM23.707 1.706A1 1 0 0 0 22.293.292l-22 22a1 1 0 0 0 0 1.414l.009.009a1 1 0 0 0 1.405-.009l6.63-6.631A.251.251 0 0 1 8.515 17a.245.245 0 0 1 .177.075 10.081 10.081 0 0 0 6.5 2.92 1 1 0 0 0 1.061-1V9.266a.247.247 0 0 1 .073-.176Z\"/></svg></button><input type=\"range\" class=\"kg-video-volume-slider\" max=\"100\" value=\"100\"></div></div></div></figure><h2 id=\"conclusion\">Conclusion</h2><p>Transitions in React 18 allow you to optimize your user experience, especially for tasks or features that require some time to load. You can now use Transitions in React 18 to differentiate between instate updates and updates that can be delayed, and show in the UI any necessary loading more efficiently for those that require more time.</p>","url":"https://backend.shahednasser.com/how-to-use-transitions-in-react-18/","canonical_url":null,"uuid":"b77353b0-096c-4619-b2b2-bab7e980f5c4","codeinjection_foot":null,"codeinjection_head":null,"codeinjection_styles":null,"comment_id":"6263f18539840e1ac287510b","reading_time":3,"send_email_when_published":null,"email_subject":null,"childHtmlRehype":{"html":"<p><a href=\"https://reactjs.org/blog/2022/03/29/react-v18.html\">React 18 came out at the end of March</a> with a bundle of new features. One of these new features is Transitions.</p><p>In this tutorial, you'll learn more about Transitions in React 18 and see them in action.</p><h2 id=\"definition\">Definition</h2><p>You have 2 types of transitions. Urgent transitions and non-urgent transitions.</p><h3 id=\"urgent-transitions\">Urgent Transitions</h3><p>Urgent transitions are transitions that the user needs to see instantly. For example, if the user changes their profile name and saves it, they should be able to see the change in the displayed profile name in the navigation bar.</p><p>Urgent transitions are done the same way you've been setting a state before:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token keyword\">const</span> <span class=\"token punctuation\">[</span>name<span class=\"token punctuation\">,</span> setName<span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token function\">useState</span><span class=\"token punctuation\">(</span><span class=\"token string\">''</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n\n<span class=\"token keyword\">function</span> <span class=\"token function\">handleChange</span> <span class=\"token punctuation\">(</span><span class=\"token parameter\">e</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n\t<span class=\"token function\">setName</span><span class=\"token punctuation\">(</span>e<span class=\"token punctuation\">.</span>target<span class=\"token punctuation\">.</span>value<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">//urgent transition</span>\n<span class=\"token punctuation\">}</span></code></pre></div><h3 id=\"non-urgent-transitions\">Non-Urgent Transitions</h3><p>Non-urgent transitions are transitions that are ok to be delayed a little. For example, if the user is performing a search, it's ok for the results to appear not so instantly.</p><p>There are 2 ways to start a non-urgent transition. The first one is using the hook <code class=\"language-text\">useTransition</code>: </p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span>useTransition<span class=\"token punctuation\">,</span> useState<span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'react'</span><span class=\"token punctuation\">;</span>\n\n<span class=\"token keyword\">export</span> <span class=\"token keyword\">default</span> <span class=\"token keyword\">function</span> <span class=\"token function\">MyApp</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token keyword\">const</span> <span class=\"token punctuation\">[</span>results<span class=\"token punctuation\">,</span> setResults<span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token function\">useState</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">[</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n    <span class=\"token keyword\">const</span> <span class=\"token punctuation\">[</span>pending<span class=\"token punctuation\">,</span> startTransition<span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token function\">useTransition</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n    \n    <span class=\"token keyword\">function</span> <span class=\"token function\">handleChange</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">e</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n        <span class=\"token keyword\">let</span> tempResults <span class=\"token operator\">=</span> <span class=\"token punctuation\">[</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span>\n        <span class=\"token operator\">...</span> <span class=\"token comment\">// set results from APIs</span>\n        <span class=\"token function\">startTransition</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\n            <span class=\"token function\">setResults</span><span class=\"token punctuation\">(</span>tempResults<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n        <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n    <span class=\"token punctuation\">}</span>\n<span class=\"token punctuation\">}</span></code></pre></div><p>The hook returns the boolean variable <code class=\"language-text\">pending</code> which indicates whether the transition is active or not. It can be used to show a loading component.</p><p>The hook also returns a function <code class=\"language-text\">startTransition</code> that accepts a callback function in which you set the state. The state will not be set immediately.</p><p>The second way to start a non-urgent transition is using the function <code class=\"language-text\">startTransition</code> imported directly from React:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span>startTransition<span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'react'</span><span class=\"token punctuation\">;</span>\n\n<span class=\"token keyword\">export</span> <span class=\"token keyword\">default</span> <span class=\"token keyword\">function</span> <span class=\"token function\">MyApp</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token keyword\">const</span> <span class=\"token punctuation\">[</span>results<span class=\"token punctuation\">,</span> setResults<span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token function\">useState</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">[</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n    \n    <span class=\"token keyword\">function</span> <span class=\"token function\">handleChange</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">e</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n        <span class=\"token keyword\">let</span> tempResults <span class=\"token operator\">=</span> <span class=\"token punctuation\">[</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span>\n        <span class=\"token operator\">...</span> <span class=\"token comment\">// set results from APIs</span>\n        <span class=\"token function\">startTransition</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\n            <span class=\"token function\">setResults</span><span class=\"token punctuation\">(</span>tempResults<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n        <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n    <span class=\"token punctuation\">}</span>\n<span class=\"token punctuation\">}</span></code></pre></div><p>This approach does not give you access to a variable like <code class=\"language-text\">isPending</code> to check whether the transition is active or not.</p><p>This approach is mainly available for places in your code when you can't use hooks like <code class=\"language-text\">useTransition</code>.</p><h2 id=\"usage-example\">Usage Example</h2><p>In this example, you'll be creating a number input that accepts a large number of images to be shown. Then, random images will be retrieved using <a href=\"https://ngneat.github.io/falso/\">Falso</a>.</p><p>Start by creating a new React app if you don't have one available:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\">npx create-react-app my-app</code></pre></div><p>Next, change into the directory <code class=\"language-text\">my-app</code>:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\"><span class=\"token builtin class-name\">cd</span> my-app</code></pre></div><p>Then, install the Falso library:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\"><span class=\"token function\">npm</span> i @ngneat/falso</code></pre></div><p>Now, open <code class=\"language-text\">src/App.js</code> and change it to the following:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token keyword\">import</span> <span class=\"token string\">'./App.css'</span><span class=\"token punctuation\">;</span>\n\n<span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> useState<span class=\"token punctuation\">,</span> useTransition <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'react'</span><span class=\"token punctuation\">;</span>\n\n<span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> randImg <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'@ngneat/falso'</span><span class=\"token punctuation\">;</span>\n\n<span class=\"token keyword\">function</span> <span class=\"token function\">App</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n  <span class=\"token keyword\">const</span> <span class=\"token punctuation\">[</span>number<span class=\"token punctuation\">,</span> setNumber<span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token function\">useState</span><span class=\"token punctuation\">(</span><span class=\"token number\">5000</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n  <span class=\"token keyword\">const</span> <span class=\"token punctuation\">[</span>images<span class=\"token punctuation\">,</span> setImages<span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token function\">useState</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">[</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span>\n  <span class=\"token keyword\">const</span> <span class=\"token punctuation\">[</span>isPending<span class=\"token punctuation\">,</span> startTransition<span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token function\">useTransition</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n\n  <span class=\"token keyword\">function</span> <span class=\"token function\">showImages</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token comment\">//TODO add transition</span>\n  <span class=\"token punctuation\">}</span>\n\n  <span class=\"token keyword\">return</span> <span class=\"token punctuation\">(</span>\n    <span class=\"token operator\">&#x3C;</span>div style<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span><span class=\"token punctuation\">{</span>\n      <span class=\"token literal-property property\">padding</span><span class=\"token operator\">:</span> <span class=\"token string\">'10px'</span>\n    <span class=\"token punctuation\">}</span><span class=\"token punctuation\">}</span><span class=\"token operator\">></span>\n      <span class=\"token operator\">&#x3C;</span>h1<span class=\"token operator\">></span>Images<span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>h1<span class=\"token operator\">></span>\n      <span class=\"token operator\">&#x3C;</span>div<span class=\"token operator\">></span>\n        <span class=\"token operator\">&#x3C;</span>label<span class=\"token operator\">></span>Number <span class=\"token keyword\">of</span> images<span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>label<span class=\"token operator\">></span>\n        <span class=\"token operator\">&#x3C;</span>input type<span class=\"token operator\">=</span><span class=\"token string\">\"number\"</span> min<span class=\"token operator\">=</span><span class=\"token string\">\"1\"</span> max<span class=\"token operator\">=</span><span class=\"token string\">\"10000\"</span> value<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span>number<span class=\"token punctuation\">}</span> onChange<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">e</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token function\">setNumber</span><span class=\"token punctuation\">(</span>e<span class=\"token punctuation\">.</span>target<span class=\"token punctuation\">.</span>value<span class=\"token punctuation\">)</span><span class=\"token punctuation\">}</span> style<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span><span class=\"token punctuation\">{</span>\n          <span class=\"token literal-property property\">display</span><span class=\"token operator\">:</span> <span class=\"token string\">'block'</span><span class=\"token punctuation\">,</span>\n          <span class=\"token literal-property property\">marginTop</span><span class=\"token operator\">:</span> <span class=\"token string\">'10px'</span><span class=\"token punctuation\">,</span>\n          <span class=\"token literal-property property\">width</span><span class=\"token operator\">:</span> <span class=\"token string\">'3rem'</span>\n        <span class=\"token punctuation\">}</span><span class=\"token punctuation\">}</span> <span class=\"token operator\">/</span><span class=\"token operator\">></span>\n        <span class=\"token operator\">&#x3C;</span>button type<span class=\"token operator\">=</span><span class=\"token string\">\"button\"</span> onClick<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span>showImages<span class=\"token punctuation\">}</span> style<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span><span class=\"token punctuation\">{</span><span class=\"token literal-property property\">marginTop</span><span class=\"token operator\">:</span> <span class=\"token string\">'10px'</span><span class=\"token punctuation\">}</span><span class=\"token punctuation\">}</span><span class=\"token operator\">></span>Show Images<span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>button<span class=\"token operator\">></span>\n      <span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>div<span class=\"token operator\">></span>\n      <span class=\"token operator\">&#x3C;</span>div<span class=\"token operator\">></span>\n        <span class=\"token operator\">&#x3C;</span>span<span class=\"token operator\">></span>Number selected<span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span>number<span class=\"token punctuation\">}</span><span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>span<span class=\"token operator\">></span><span class=\"token operator\">&#x3C;</span>br<span class=\"token operator\">/</span><span class=\"token operator\">></span>\n        <span class=\"token operator\">&#x3C;</span>span<span class=\"token operator\">></span>Results<span class=\"token operator\">:</span><span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>span<span class=\"token operator\">></span>\n        <span class=\"token punctuation\">{</span>isPending <span class=\"token operator\">&#x26;&#x26;</span> <span class=\"token operator\">&#x3C;</span>span<span class=\"token operator\">></span>Loading<span class=\"token operator\">...</span><span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>span<span class=\"token operator\">></span><span class=\"token punctuation\">}</span>\n        <span class=\"token punctuation\">{</span><span class=\"token operator\">!</span>isPending <span class=\"token operator\">&#x26;&#x26;</span> images<span class=\"token punctuation\">.</span>length <span class=\"token operator\">></span> <span class=\"token number\">0</span> <span class=\"token operator\">&#x26;&#x26;</span> images<span class=\"token punctuation\">}</span>\n      <span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>div<span class=\"token operator\">></span>\n    <span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>div<span class=\"token operator\">></span>\n  <span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span>\n\n<span class=\"token keyword\">export</span> <span class=\"token keyword\">default</span> App<span class=\"token punctuation\">;</span>\n</code></pre></div><p>You first create 2 state variables <code class=\"language-text\">number</code> and <code class=\"language-text\">images</code>. You also use <code class=\"language-text\">useTransition</code> which gives you access to <code class=\"language-text\">isPending</code> and <code class=\"language-text\">startTransition</code>.</p><p>In the returned JSX, you show a number input and a button that will later retrieve the images on click.</p><p>Below the input and button, the number entered by the user in the input will be shown. Notice that in the <code class=\"language-text\">onChange</code> event handler for the input the name is updated <strong>urgently. </strong>This will show the number instantly as it is updated by the user.</p><p>Let's test it out now. Run the website server:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\"><span class=\"token function\">npm</span> start</code></pre></div><p>This will open the website in your browser. If you try to update the input, you'll notice that the number displayed below it will update instantly.</p><figure class=\"kg-card kg-video-card\"><div class=\"kg-video-container\"><video src=\"https://backend.shahednasser.com/content/media/2022/04/Screen-Recording-2022-04-23-at-3.45.38-PM.mp4\" poster=\"https://img.spacergif.org/v1/798x692/0a/spacer.png\" width=\"798\" height=\"692\" playsinline preload=\"metadata\" style=\"background: transparent url(&#x27;https://backend.shahednasser.com/content/images/2022/04/media-thumbnail-ember409.jpg&#x27;) 50% 50% / cover no-repeat;\"></video><div class=\"kg-video-overlay\"><button class=\"kg-video-large-play-icon\"><svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 24 24\"><path d=\"M23.14 10.608 2.253.164A1.559 1.559 0 0 0 0 1.557v20.887a1.558 1.558 0 0 0 2.253 1.392L23.14 13.393a1.557 1.557 0 0 0 0-2.785Z\"></path></svg></button></div><div class=\"kg-video-player-container\"><div class=\"kg-video-player\"><button class=\"kg-video-play-icon\"><svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 24 24\"><path d=\"M23.14 10.608 2.253.164A1.559 1.559 0 0 0 0 1.557v20.887a1.558 1.558 0 0 0 2.253 1.392L23.14 13.393a1.557 1.557 0 0 0 0-2.785Z\"></path></svg></button><button class=\"kg-video-pause-icon kg-video-hide\"><svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 24 24\"><rect x=\"3\" y=\"1\" width=\"7\" height=\"22\" rx=\"1.5\" ry=\"1.5\"></rect><rect x=\"14\" y=\"1\" width=\"7\" height=\"22\" rx=\"1.5\" ry=\"1.5\"></rect></svg></button><span class=\"kg-video-current-time\">0:00</span><div class=\"kg-video-time\">/<span class=\"kg-video-duration\"></span></div><input type=\"range\" class=\"kg-video-seek-slider\" max=\"100\" value=\"0\"><button class=\"kg-video-playback-rate\">1×</button><button class=\"kg-video-unmute-icon\"><svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 24 24\"><path d=\"M15.189 2.021a9.728 9.728 0 0 0-7.924 4.85.249.249 0 0 1-.221.133H5.25a3 3 0 0 0-3 3v2a3 3 0 0 0 3 3h1.794a.249.249 0 0 1 .221.133 9.73 9.73 0 0 0 7.924 4.85h.06a1 1 0 0 0 1-1V3.02a1 1 0 0 0-1.06-.998Z\"></path></svg></button><button class=\"kg-video-mute-icon kg-video-hide\"><svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 24 24\"><path d=\"M16.177 4.3a.248.248 0 0 0 .073-.176v-1.1a1 1 0 0 0-1.061-1 9.728 9.728 0 0 0-7.924 4.85.249.249 0 0 1-.221.133H5.25a3 3 0 0 0-3 3v2a3 3 0 0 0 3 3h.114a.251.251 0 0 0 .177-.073ZM23.707 1.706A1 1 0 0 0 22.293.292l-22 22a1 1 0 0 0 0 1.414l.009.009a1 1 0 0 0 1.405-.009l6.63-6.631A.251.251 0 0 1 8.515 17a.245.245 0 0 1 .177.075 10.081 10.081 0 0 0 6.5 2.92 1 1 0 0 0 1.061-1V9.266a.247.247 0 0 1 .073-.176Z\"></path></svg></button><input type=\"range\" class=\"kg-video-volume-slider\" max=\"100\" value=\"100\"></div></div></div></figure><p>Now, let's test the non-urgent transition. In <code class=\"language-text\">showImages</code> replace the <code class=\"language-text\">TODO</code> with the following code:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token keyword\">const</span> imgSources <span class=\"token operator\">=</span> <span class=\"token function\">randImg</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span><span class=\"token literal-property property\">length</span><span class=\"token operator\">:</span> number<span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token function\">map</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">url<span class=\"token punctuation\">,</span> index</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token operator\">&#x3C;</span>img src<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span><span class=\"token template-string\"><span class=\"token template-punctuation string\">`</span><span class=\"token interpolation\"><span class=\"token interpolation-punctuation punctuation\">${</span>url<span class=\"token interpolation-punctuation punctuation\">}</span></span><span class=\"token string\">?v=</span><span class=\"token interpolation\"><span class=\"token interpolation-punctuation punctuation\">${</span>index<span class=\"token interpolation-punctuation punctuation\">}</span></span><span class=\"token template-punctuation string\">`</span></span><span class=\"token punctuation\">}</span> key<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span>index<span class=\"token punctuation\">}</span> <span class=\"token operator\">/</span><span class=\"token operator\">></span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token function\">startTransition</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\n\t<span class=\"token function\">setImages</span><span class=\"token punctuation\">(</span>imgSources<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span></code></pre></div><p>This will get the images using the <code class=\"language-text\">falso</code> library and inside <code class=\"language-text\">startTransition</code> with set the <code class=\"language-text\">images</code> state variable.</p><p>Notice that in the returned JSX of the function we use <code class=\"language-text\">isPending</code> to indicate whether to show \"Loading...\" or not.</p><p>If you try clicking on the button now, the \"Loading...\" text will show first, and then the images will be shown gradually.</p><figure class=\"kg-card kg-video-card\"><div class=\"kg-video-container\"><video src=\"https://backend.shahednasser.com/content/media/2022/04/Screen-Recording-2022-04-23-at-4.01.34-PM.mp4\" poster=\"https://img.spacergif.org/v1/794x896/0a/spacer.png\" width=\"794\" height=\"896\" playsinline preload=\"metadata\" style=\"background: transparent url(&#x27;https://backend.shahednasser.com/content/images/2022/04/media-thumbnail-ember441.jpg&#x27;) 50% 50% / cover no-repeat;\"></video><div class=\"kg-video-overlay\"><button class=\"kg-video-large-play-icon\"><svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 24 24\"><path d=\"M23.14 10.608 2.253.164A1.559 1.559 0 0 0 0 1.557v20.887a1.558 1.558 0 0 0 2.253 1.392L23.14 13.393a1.557 1.557 0 0 0 0-2.785Z\"></path></svg></button></div><div class=\"kg-video-player-container\"><div class=\"kg-video-player\"><button class=\"kg-video-play-icon\"><svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 24 24\"><path d=\"M23.14 10.608 2.253.164A1.559 1.559 0 0 0 0 1.557v20.887a1.558 1.558 0 0 0 2.253 1.392L23.14 13.393a1.557 1.557 0 0 0 0-2.785Z\"></path></svg></button><button class=\"kg-video-pause-icon kg-video-hide\"><svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 24 24\"><rect x=\"3\" y=\"1\" width=\"7\" height=\"22\" rx=\"1.5\" ry=\"1.5\"></rect><rect x=\"14\" y=\"1\" width=\"7\" height=\"22\" rx=\"1.5\" ry=\"1.5\"></rect></svg></button><span class=\"kg-video-current-time\">0:00</span><div class=\"kg-video-time\">/<span class=\"kg-video-duration\"></span></div><input type=\"range\" class=\"kg-video-seek-slider\" max=\"100\" value=\"0\"><button class=\"kg-video-playback-rate\">1×</button><button class=\"kg-video-unmute-icon\"><svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 24 24\"><path d=\"M15.189 2.021a9.728 9.728 0 0 0-7.924 4.85.249.249 0 0 1-.221.133H5.25a3 3 0 0 0-3 3v2a3 3 0 0 0 3 3h1.794a.249.249 0 0 1 .221.133 9.73 9.73 0 0 0 7.924 4.85h.06a1 1 0 0 0 1-1V3.02a1 1 0 0 0-1.06-.998Z\"></path></svg></button><button class=\"kg-video-mute-icon kg-video-hide\"><svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 24 24\"><path d=\"M16.177 4.3a.248.248 0 0 0 .073-.176v-1.1a1 1 0 0 0-1.061-1 9.728 9.728 0 0 0-7.924 4.85.249.249 0 0 1-.221.133H5.25a3 3 0 0 0-3 3v2a3 3 0 0 0 3 3h.114a.251.251 0 0 0 .177-.073ZM23.707 1.706A1 1 0 0 0 22.293.292l-22 22a1 1 0 0 0 0 1.414l.009.009a1 1 0 0 0 1.405-.009l6.63-6.631A.251.251 0 0 1 8.515 17a.245.245 0 0 1 .177.075 10.081 10.081 0 0 0 6.5 2.92 1 1 0 0 0 1.061-1V9.266a.247.247 0 0 1 .073-.176Z\"></path></svg></button><input type=\"range\" class=\"kg-video-volume-slider\" max=\"100\" value=\"100\"></div></div></div></figure><h2 id=\"conclusion\">Conclusion</h2><p>Transitions in React 18 allow you to optimize your user experience, especially for tasks or features that require some time to load. You can now use Transitions in React 18 to differentiate between instate updates and updates that can be delayed, and show in the UI any necessary loading more efficiently for those that require more time.</p>","htmlAst":{"type":"root","children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"https://reactjs.org/blog/2022/03/29/react-v18.html"},"children":[{"type":"text","value":"React 18 came out at the end of March"}]},{"type":"text","value":" with a bundle of new features. One of these new features is Transitions."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In this tutorial, you'll learn more about Transitions in React 18 and see them in action."}]},{"type":"element","tagName":"h2","properties":{"id":"definition"},"children":[{"type":"text","value":"Definition"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You have 2 types of transitions. Urgent transitions and non-urgent transitions."}]},{"type":"element","tagName":"h3","properties":{"id":"urgent-transitions"},"children":[{"type":"text","value":"Urgent Transitions"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Urgent transitions are transitions that the user needs to see instantly. For example, if the user changes their profile name and saves it, they should be able to see the change in the displayed profile name in the navigation bar."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Urgent transitions are done the same way you've been setting a state before:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"js"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"const"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" setName"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"useState"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"''"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"function"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"handleChange"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","parameter"]},"children":[{"type":"text","value":"e"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n\t"},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"setName"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"e"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"target"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"value"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"//urgent transition"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]}]}]},{"type":"element","tagName":"h3","properties":{"id":"non-urgent-transitions"},"children":[{"type":"text","value":"Non-Urgent Transitions"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Non-urgent transitions are transitions that are ok to be delayed a little. For example, if the user is performing a search, it's ok for the results to appear not so instantly."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"There are 2 ways to start a non-urgent transition. The first one is using the hook "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"useTransition"}]},{"type":"text","value":": "}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"js"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"useTransition"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" useState"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'react'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"export"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"default"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"function"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"MyApp"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"const"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"results"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" setResults"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"useState"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"const"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"pending"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" startTransition"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"useTransition"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n    \n    "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"function"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"handleChange"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","parameter"]},"children":[{"type":"text","value":"e"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n        "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"let"}]},{"type":"text","value":" tempResults "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n        "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"..."}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"// set results from APIs"}]},{"type":"text","value":"\n        "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"startTransition"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=>"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n            "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"setResults"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"tempResults"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n        "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The hook returns the boolean variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pending"}]},{"type":"text","value":" which indicates whether the transition is active or not. It can be used to show a loading component."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The hook also returns a function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"startTransition"}]},{"type":"text","value":" that accepts a callback function in which you set the state. The state will not be set immediately."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The second way to start a non-urgent transition is using the function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"startTransition"}]},{"type":"text","value":" imported directly from React:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"js"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"startTransition"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'react'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"export"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"default"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"function"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"MyApp"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"const"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"results"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" setResults"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"useState"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n    \n    "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"function"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"handleChange"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","parameter"]},"children":[{"type":"text","value":"e"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n        "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"let"}]},{"type":"text","value":" tempResults "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n        "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"..."}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"// set results from APIs"}]},{"type":"text","value":"\n        "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"startTransition"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=>"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n            "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"setResults"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"tempResults"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n        "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This approach does not give you access to a variable like "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"isPending"}]},{"type":"text","value":" to check whether the transition is active or not."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This approach is mainly available for places in your code when you can't use hooks like "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"useTransition"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"h2","properties":{"id":"usage-example"},"children":[{"type":"text","value":"Usage Example"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In this example, you'll be creating a number input that accepts a large number of images to be shown. Then, random images will be retrieved using "},{"type":"element","tagName":"a","properties":{"href":"https://ngneat.github.io/falso/"},"children":[{"type":"text","value":"Falso"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Start by creating a new React app if you don't have one available:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"bash"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-bash"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-bash"]},"children":[{"type":"text","value":"npx create-react-app my-app"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Next, change into the directory "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"my-app"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"bash"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-bash"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-bash"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","builtin","class-name"]},"children":[{"type":"text","value":"cd"}]},{"type":"text","value":" my-app"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Then, install the Falso library:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"bash"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-bash"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-bash"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"npm"}]},{"type":"text","value":" i @ngneat/falso"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Now, open "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"src/App.js"}]},{"type":"text","value":" and change it to the following:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"js"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'./App.css'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":" useState"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" useTransition "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'react'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":" randImg "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'@ngneat/falso'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"function"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"App"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n  "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"const"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"number"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" setNumber"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"useState"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n  "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"const"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"images"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" setImages"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"useState"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n  "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"const"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"isPending"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" startTransition"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"useTransition"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n\n  "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"function"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"showImages"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"//TODO add transition"}]},{"type":"text","value":"\n  "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n\n  "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"div style"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n      "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"padding"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'10px'"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n      "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"h1"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"Images"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"h1"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n      "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"div"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n        "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"label"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"Number "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"of"}]},{"type":"text","value":" images"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"label"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n        "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"input type"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"number\""}]},{"type":"text","value":" min"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"1\""}]},{"type":"text","value":" max"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"10000\""}]},{"type":"text","value":" value"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"number"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":" onChange"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","parameter"]},"children":[{"type":"text","value":"e"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=>"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"setNumber"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"e"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"target"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"value"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":" style"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n          "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"display"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'block'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n          "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"marginTop"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'10px'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n          "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"width"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'3rem'"}]},{"type":"text","value":"\n        "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n        "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"button type"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"button\""}]},{"type":"text","value":" onClick"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"showImages"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":" style"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"marginTop"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'10px'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"Show Images"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"button"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n      "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"div"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n      "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"div"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n        "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"span"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"Number selected"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"number"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"span"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"br"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n        "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"span"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"Results"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"span"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n        "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"isPending "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"&&"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"span"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"Loading"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"..."}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"span"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n        "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"!"}]},{"type":"text","value":"isPending "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"&&"}]},{"type":"text","value":" images"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"length "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"&&"}]},{"type":"text","value":" images"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n      "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"div"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"div"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n  "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"export"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"default"}]},{"type":"text","value":" App"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You first create 2 state variables "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"number"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"images"}]},{"type":"text","value":". You also use "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"useTransition"}]},{"type":"text","value":" which gives you access to "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"isPending"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"startTransition"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the returned JSX, you show a number input and a button that will later retrieve the images on click."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Below the input and button, the number entered by the user in the input will be shown. Notice that in the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"onChange"}]},{"type":"text","value":" event handler for the input the name is updated "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"urgently. "}]},{"type":"text","value":"This will show the number instantly as it is updated by the user."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's test it out now. Run the website server:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"bash"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-bash"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-bash"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"npm"}]},{"type":"text","value":" start"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This will open the website in your browser. If you try to update the input, you'll notice that the number displayed below it will update instantly."}]},{"type":"element","tagName":"figure","properties":{"className":["kg-card","kg-video-card"]},"children":[{"type":"element","tagName":"div","properties":{"className":["kg-video-container"]},"children":[{"type":"element","tagName":"video","properties":{"src":"https://backend.shahednasser.com/content/media/2022/04/Screen-Recording-2022-04-23-at-3.45.38-PM.mp4","poster":"https://img.spacergif.org/v1/798x692/0a/spacer.png","width":798,"height":692,"playsInline":true,"preload":"metadata","style":"background: transparent url('https://backend.shahednasser.com/content/images/2022/04/media-thumbnail-ember409.jpg') 50% 50% / cover no-repeat;"},"children":[]},{"type":"element","tagName":"div","properties":{"className":["kg-video-overlay"]},"children":[{"type":"element","tagName":"button","properties":{"className":["kg-video-large-play-icon"]},"children":[{"type":"element","tagName":"svg","properties":{"xmlns":"http://www.w3.org/2000/svg","viewBox":"0 0 24 24"},"children":[{"type":"element","tagName":"path","properties":{"d":"M23.14 10.608 2.253.164A1.559 1.559 0 0 0 0 1.557v20.887a1.558 1.558 0 0 0 2.253 1.392L23.14 13.393a1.557 1.557 0 0 0 0-2.785Z"},"children":[]}]}]}]},{"type":"element","tagName":"div","properties":{"className":["kg-video-player-container"]},"children":[{"type":"element","tagName":"div","properties":{"className":["kg-video-player"]},"children":[{"type":"element","tagName":"button","properties":{"className":["kg-video-play-icon"]},"children":[{"type":"element","tagName":"svg","properties":{"xmlns":"http://www.w3.org/2000/svg","viewBox":"0 0 24 24"},"children":[{"type":"element","tagName":"path","properties":{"d":"M23.14 10.608 2.253.164A1.559 1.559 0 0 0 0 1.557v20.887a1.558 1.558 0 0 0 2.253 1.392L23.14 13.393a1.557 1.557 0 0 0 0-2.785Z"},"children":[]}]}]},{"type":"element","tagName":"button","properties":{"className":["kg-video-pause-icon","kg-video-hide"]},"children":[{"type":"element","tagName":"svg","properties":{"xmlns":"http://www.w3.org/2000/svg","viewBox":"0 0 24 24"},"children":[{"type":"element","tagName":"rect","properties":{"x":"3","y":"1","width":7,"height":22,"rx":"1.5","ry":"1.5"},"children":[]},{"type":"element","tagName":"rect","properties":{"x":"14","y":"1","width":7,"height":22,"rx":"1.5","ry":"1.5"},"children":[]}]}]},{"type":"element","tagName":"span","properties":{"className":["kg-video-current-time"]},"children":[{"type":"text","value":"0:00"}]},{"type":"element","tagName":"div","properties":{"className":["kg-video-time"]},"children":[{"type":"text","value":"/"},{"type":"element","tagName":"span","properties":{"className":["kg-video-duration"]},"children":[]}]},{"type":"element","tagName":"input","properties":{"type":"range","className":["kg-video-seek-slider"],"max":"100","value":"0"},"children":[]},{"type":"element","tagName":"button","properties":{"className":["kg-video-playback-rate"]},"children":[{"type":"text","value":"1×"}]},{"type":"element","tagName":"button","properties":{"className":["kg-video-unmute-icon"]},"children":[{"type":"element","tagName":"svg","properties":{"xmlns":"http://www.w3.org/2000/svg","viewBox":"0 0 24 24"},"children":[{"type":"element","tagName":"path","properties":{"d":"M15.189 2.021a9.728 9.728 0 0 0-7.924 4.85.249.249 0 0 1-.221.133H5.25a3 3 0 0 0-3 3v2a3 3 0 0 0 3 3h1.794a.249.249 0 0 1 .221.133 9.73 9.73 0 0 0 7.924 4.85h.06a1 1 0 0 0 1-1V3.02a1 1 0 0 0-1.06-.998Z"},"children":[]}]}]},{"type":"element","tagName":"button","properties":{"className":["kg-video-mute-icon","kg-video-hide"]},"children":[{"type":"element","tagName":"svg","properties":{"xmlns":"http://www.w3.org/2000/svg","viewBox":"0 0 24 24"},"children":[{"type":"element","tagName":"path","properties":{"d":"M16.177 4.3a.248.248 0 0 0 .073-.176v-1.1a1 1 0 0 0-1.061-1 9.728 9.728 0 0 0-7.924 4.85.249.249 0 0 1-.221.133H5.25a3 3 0 0 0-3 3v2a3 3 0 0 0 3 3h.114a.251.251 0 0 0 .177-.073ZM23.707 1.706A1 1 0 0 0 22.293.292l-22 22a1 1 0 0 0 0 1.414l.009.009a1 1 0 0 0 1.405-.009l6.63-6.631A.251.251 0 0 1 8.515 17a.245.245 0 0 1 .177.075 10.081 10.081 0 0 0 6.5 2.92 1 1 0 0 0 1.061-1V9.266a.247.247 0 0 1 .073-.176Z"},"children":[]}]}]},{"type":"element","tagName":"input","properties":{"type":"range","className":["kg-video-volume-slider"],"max":"100","value":"100"},"children":[]}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Now, let's test the non-urgent transition. In "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"showImages"}]},{"type":"text","value":" replace the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"TODO"}]},{"type":"text","value":" with the following code:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"js"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"const"}]},{"type":"text","value":" imgSources "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"randImg"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"length"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" number"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"map"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","parameter"]},"children":[{"type":"text","value":"url"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" index"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=>"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"img src"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","template-string"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","template-punctuation","string"]},"children":[{"type":"text","value":"`"}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","interpolation-punctuation","punctuation"]},"children":[{"type":"text","value":"${"}]},{"type":"text","value":"url"},{"type":"element","tagName":"span","properties":{"className":["token","interpolation-punctuation","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"?v="}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","interpolation-punctuation","punctuation"]},"children":[{"type":"text","value":"${"}]},{"type":"text","value":"index"},{"type":"element","tagName":"span","properties":{"className":["token","interpolation-punctuation","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","template-punctuation","string"]},"children":[{"type":"text","value":"`"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":" key"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"index"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"startTransition"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=>"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n\t"},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"setImages"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"imgSources"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This will get the images using the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"falso"}]},{"type":"text","value":" library and inside "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"startTransition"}]},{"type":"text","value":" with set the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"images"}]},{"type":"text","value":" state variable."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Notice that in the returned JSX of the function we use "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"isPending"}]},{"type":"text","value":" to indicate whether to show \"Loading...\" or not."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If you try clicking on the button now, the \"Loading...\" text will show first, and then the images will be shown gradually."}]},{"type":"element","tagName":"figure","properties":{"className":["kg-card","kg-video-card"]},"children":[{"type":"element","tagName":"div","properties":{"className":["kg-video-container"]},"children":[{"type":"element","tagName":"video","properties":{"src":"https://backend.shahednasser.com/content/media/2022/04/Screen-Recording-2022-04-23-at-4.01.34-PM.mp4","poster":"https://img.spacergif.org/v1/794x896/0a/spacer.png","width":794,"height":896,"playsInline":true,"preload":"metadata","style":"background: transparent url('https://backend.shahednasser.com/content/images/2022/04/media-thumbnail-ember441.jpg') 50% 50% / cover no-repeat;"},"children":[]},{"type":"element","tagName":"div","properties":{"className":["kg-video-overlay"]},"children":[{"type":"element","tagName":"button","properties":{"className":["kg-video-large-play-icon"]},"children":[{"type":"element","tagName":"svg","properties":{"xmlns":"http://www.w3.org/2000/svg","viewBox":"0 0 24 24"},"children":[{"type":"element","tagName":"path","properties":{"d":"M23.14 10.608 2.253.164A1.559 1.559 0 0 0 0 1.557v20.887a1.558 1.558 0 0 0 2.253 1.392L23.14 13.393a1.557 1.557 0 0 0 0-2.785Z"},"children":[]}]}]}]},{"type":"element","tagName":"div","properties":{"className":["kg-video-player-container"]},"children":[{"type":"element","tagName":"div","properties":{"className":["kg-video-player"]},"children":[{"type":"element","tagName":"button","properties":{"className":["kg-video-play-icon"]},"children":[{"type":"element","tagName":"svg","properties":{"xmlns":"http://www.w3.org/2000/svg","viewBox":"0 0 24 24"},"children":[{"type":"element","tagName":"path","properties":{"d":"M23.14 10.608 2.253.164A1.559 1.559 0 0 0 0 1.557v20.887a1.558 1.558 0 0 0 2.253 1.392L23.14 13.393a1.557 1.557 0 0 0 0-2.785Z"},"children":[]}]}]},{"type":"element","tagName":"button","properties":{"className":["kg-video-pause-icon","kg-video-hide"]},"children":[{"type":"element","tagName":"svg","properties":{"xmlns":"http://www.w3.org/2000/svg","viewBox":"0 0 24 24"},"children":[{"type":"element","tagName":"rect","properties":{"x":"3","y":"1","width":7,"height":22,"rx":"1.5","ry":"1.5"},"children":[]},{"type":"element","tagName":"rect","properties":{"x":"14","y":"1","width":7,"height":22,"rx":"1.5","ry":"1.5"},"children":[]}]}]},{"type":"element","tagName":"span","properties":{"className":["kg-video-current-time"]},"children":[{"type":"text","value":"0:00"}]},{"type":"element","tagName":"div","properties":{"className":["kg-video-time"]},"children":[{"type":"text","value":"/"},{"type":"element","tagName":"span","properties":{"className":["kg-video-duration"]},"children":[]}]},{"type":"element","tagName":"input","properties":{"type":"range","className":["kg-video-seek-slider"],"max":"100","value":"0"},"children":[]},{"type":"element","tagName":"button","properties":{"className":["kg-video-playback-rate"]},"children":[{"type":"text","value":"1×"}]},{"type":"element","tagName":"button","properties":{"className":["kg-video-unmute-icon"]},"children":[{"type":"element","tagName":"svg","properties":{"xmlns":"http://www.w3.org/2000/svg","viewBox":"0 0 24 24"},"children":[{"type":"element","tagName":"path","properties":{"d":"M15.189 2.021a9.728 9.728 0 0 0-7.924 4.85.249.249 0 0 1-.221.133H5.25a3 3 0 0 0-3 3v2a3 3 0 0 0 3 3h1.794a.249.249 0 0 1 .221.133 9.73 9.73 0 0 0 7.924 4.85h.06a1 1 0 0 0 1-1V3.02a1 1 0 0 0-1.06-.998Z"},"children":[]}]}]},{"type":"element","tagName":"button","properties":{"className":["kg-video-mute-icon","kg-video-hide"]},"children":[{"type":"element","tagName":"svg","properties":{"xmlns":"http://www.w3.org/2000/svg","viewBox":"0 0 24 24"},"children":[{"type":"element","tagName":"path","properties":{"d":"M16.177 4.3a.248.248 0 0 0 .073-.176v-1.1a1 1 0 0 0-1.061-1 9.728 9.728 0 0 0-7.924 4.85.249.249 0 0 1-.221.133H5.25a3 3 0 0 0-3 3v2a3 3 0 0 0 3 3h.114a.251.251 0 0 0 .177-.073ZM23.707 1.706A1 1 0 0 0 22.293.292l-22 22a1 1 0 0 0 0 1.414l.009.009a1 1 0 0 0 1.405-.009l6.63-6.631A.251.251 0 0 1 8.515 17a.245.245 0 0 1 .177.075 10.081 10.081 0 0 0 6.5 2.92 1 1 0 0 0 1.061-1V9.266a.247.247 0 0 1 .073-.176Z"},"children":[]}]}]},{"type":"element","tagName":"input","properties":{"type":"range","className":["kg-video-volume-slider"],"max":"100","value":"100"},"children":[]}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"conclusion"},"children":[{"type":"text","value":"Conclusion"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Transitions in React 18 allow you to optimize your user experience, especially for tasks or features that require some time to load. You can now use Transitions in React 18 to differentiate between instate updates and updates that can be delayed, and show in the UI any necessary loading more efficiently for those that require more time."}]}],"data":{"quirksMode":false}},"tableOfContents":[{"id":"definition","heading":"Definition","items":[{"id":"urgent-transitions","heading":"Urgent Transitions"},{"id":"non-urgent-transitions","heading":"Non-Urgent Transitions"}]},{"id":"usage-example","heading":"Usage Example"},{"id":"conclusion","heading":"Conclusion"}]},"featureImageSharp":{"base":"How-to-use-Transitions-in-React-18-2.jpg","publicURL":"/static/a653d678a3e8e1cde9337a2091d4db0c/How-to-use-Transitions-in-React-18-2.jpg","imageMeta":{"width":1560,"height":876},"childImageSharp":{"fluid":{"base64":"data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAFwABAQEBAAAAAAAAAAAAAAAAAAECBf/EABQBAQAAAAAAAAAAAAAAAAAAAAD/2gAMAwEAAhADEAAAAe60Io//xAAWEAEBAQAAAAAAAAAAAAAAAAAhACD/2gAIAQEAAQUCZz//xAAUEQEAAAAAAAAAAAAAAAAAAAAQ/9oACAEDAQE/AT//xAAUEQEAAAAAAAAAAAAAAAAAAAAQ/9oACAECAQE/AT//xAAWEAEBAQAAAAAAAAAAAAAAAAAxACD/2gAIAQEABj8CIz//xAAZEAACAwEAAAAAAAAAAAAAAAAAEQEh8SD/2gAIAQEAAT8heg9CGrrj/9oADAMBAAIAAwAAABAjz//EABQRAQAAAAAAAAAAAAAAAAAAABD/2gAIAQMBAT8QP//EABQRAQAAAAAAAAAAAAAAAAAAABD/2gAIAQIBAT8QP//EABsQAAICAwEAAAAAAAAAAAAAAAERADEgIWFR/9oACAEBAAE/EHEqpW7dnPJkwN43h//Z","aspectRatio":1.7857142857142858,"src":"/static/a653d678a3e8e1cde9337a2091d4db0c/ea4ab/How-to-use-Transitions-in-React-18-2.jpg","srcSet":"/static/a653d678a3e8e1cde9337a2091d4db0c/477ba/How-to-use-Transitions-in-React-18-2.jpg 175w,\n/static/a653d678a3e8e1cde9337a2091d4db0c/06776/How-to-use-Transitions-in-React-18-2.jpg 350w,\n/static/a653d678a3e8e1cde9337a2091d4db0c/ea4ab/How-to-use-Transitions-in-React-18-2.jpg 700w,\n/static/a653d678a3e8e1cde9337a2091d4db0c/3055e/How-to-use-Transitions-in-React-18-2.jpg 1050w,\n/static/a653d678a3e8e1cde9337a2091d4db0c/eff08/How-to-use-Transitions-in-React-18-2.jpg 1400w,\n/static/a653d678a3e8e1cde9337a2091d4db0c/81a53/How-to-use-Transitions-in-React-18-2.jpg 1560w","srcWebp":"/static/a653d678a3e8e1cde9337a2091d4db0c/89afa/How-to-use-Transitions-in-React-18-2.webp","srcSetWebp":"/static/a653d678a3e8e1cde9337a2091d4db0c/9fca7/How-to-use-Transitions-in-React-18-2.webp 175w,\n/static/a653d678a3e8e1cde9337a2091d4db0c/37a4e/How-to-use-Transitions-in-React-18-2.webp 350w,\n/static/a653d678a3e8e1cde9337a2091d4db0c/89afa/How-to-use-Transitions-in-React-18-2.webp 700w,\n/static/a653d678a3e8e1cde9337a2091d4db0c/78e7a/How-to-use-Transitions-in-React-18-2.webp 1050w,\n/static/a653d678a3e8e1cde9337a2091d4db0c/03d34/How-to-use-Transitions-in-React-18-2.webp 1400w,\n/static/a653d678a3e8e1cde9337a2091d4db0c/f5845/How-to-use-Transitions-in-React-18-2.webp 1560w","sizes":"(max-width: 700px) 100vw, 700px"}}}}},{"node":{"id":"Ghost__Post__6235d3ec1594e705e60e186f","title":"How to Create and Validate Forms in React using Formik and Yup","slug":"how-to-create-and-validate-forms-in-react-using-formik-and-yup","featured":true,"feature_image":"https://backend.shahednasser.com/content/images/2022/03/How-to-Create-and-Validate-Forms-in-React-using-Formik-and-Yup.webp","excerpt":"In this tutorial, you'll learn how creating and validating forms can be simpler in React using Formik and Yup.","custom_excerpt":"In this tutorial, you'll learn how creating and validating forms can be simpler in React using Formik and Yup.","visibility":"public","created_at_pretty":"19 Mar 2022","published_at_pretty":"21 Mar 2022","updated_at_pretty":"27 Mar 2022","created_at":"2022-03-19T13:00:28.000+00:00","published_at":"2022-03-21T10:55:46.000+00:00","updated_at":"2022-03-27T10:36:28.000+00:00","meta_title":null,"meta_description":null,"og_description":null,"og_image":null,"og_title":null,"twitter_description":null,"twitter_image":null,"twitter_title":null,"authors":[{"slug":"shahed","url":"https://backend.shahednasser.com/author/shahed/","name":"Shahed Nasser","bio":null,"cover_image":null,"profile_image":"https://backend.shahednasser.com/content/images/2022/03/IMG_0591.jpg","location":null,"website":null,"twitter":null,"facebook":null,"meta_title":null,"meta_description":null,"coverImageSharp":null,"profileImageSharp":null}],"primary_author":{"slug":"shahed","url":"https://backend.shahednasser.com/author/shahed/","name":"Shahed Nasser","bio":null,"cover_image":null,"profile_image":"https://backend.shahednasser.com/content/images/2022/03/IMG_0591.jpg","location":null,"website":null,"twitter":null,"facebook":null,"meta_title":null,"meta_description":null,"coverImageSharp":null,"profileImageSharp":{"base":"IMG_0591.jpg","publicURL":"/static/ceb49c3c631485453e71e00d7f84b069/IMG_0591.jpg","imageMeta":{"width":1182,"height":1179},"childImageSharp":{"fluid":{"base64":"data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAUABQDASIAAhEBAxEB/8QAGAABAQEBAQAAAAAAAAAAAAAAAAMEAQL/xAAWAQEBAQAAAAAAAAAAAAAAAAADBAL/2gAMAwEAAhADEAAAAdXiFM6i0CohUWXoKn//xAAcEAACAgIDAAAAAAAAAAAAAAACAwESBBEhM0H/2gAIAQEAAQUCWySE3WEr7SzbXjAj4iKty+sOQ//EABYRAQEBAAAAAAAAAAAAAAAAAAERIP/aAAgBAwEBPwEhj//EABQRAQAAAAAAAAAAAAAAAAAAACD/2gAIAQIBAT8BH//EAB4QAAIBBAMBAAAAAAAAAAAAAAABIRESMUECECJx/9oACAEBAAY/ApVGWvOjzgtUwLlTZA0sdL4f/8QAHBAAAwACAwEAAAAAAAAAAAAAAAERITFBkbHB/9oACAEBAAE/IahkCy+N2GwZpjQiJHJCspUFY0QrSi+HqiW2rgf/2gAMAwEAAgADAAAAEPw3/wD/xAAYEQEBAAMAAAAAAAAAAAAAAAAAARExQf/aAAgBAwEBPxCtjDqP/8QAFxEBAQEBAAAAAAAAAAAAAAAAAQAxEf/aAAgBAgEBPxBFus6Tt//EAB8QAQEAAgIBBQAAAAAAAAAAAAERACExQWFRcYGR0f/aAAgBAQABPxAuaBPPzkO1wyX7F4wkwXanfZrFQgeqE9JgS14vVOvrERIJomVBKwt2jebAeP0yVa8h1n//2Q==","aspectRatio":1,"src":"/static/ceb49c3c631485453e71e00d7f84b069/31709/IMG_0591.jpg","srcSet":"/static/ceb49c3c631485453e71e00d7f84b069/f340b/IMG_0591.jpg 28w,\n/static/ceb49c3c631485453e71e00d7f84b069/22d64/IMG_0591.jpg 55w,\n/static/ceb49c3c631485453e71e00d7f84b069/31709/IMG_0591.jpg 110w,\n/static/ceb49c3c631485453e71e00d7f84b069/aa249/IMG_0591.jpg 165w,\n/static/ceb49c3c631485453e71e00d7f84b069/0dc33/IMG_0591.jpg 220w,\n/static/ceb49c3c631485453e71e00d7f84b069/d8257/IMG_0591.jpg 1182w","srcWebp":"/static/ceb49c3c631485453e71e00d7f84b069/8678c/IMG_0591.webp","srcSetWebp":"/static/ceb49c3c631485453e71e00d7f84b069/59cda/IMG_0591.webp 28w,\n/static/ceb49c3c631485453e71e00d7f84b069/7da75/IMG_0591.webp 55w,\n/static/ceb49c3c631485453e71e00d7f84b069/8678c/IMG_0591.webp 110w,\n/static/ceb49c3c631485453e71e00d7f84b069/f282e/IMG_0591.webp 165w,\n/static/ceb49c3c631485453e71e00d7f84b069/a7b21/IMG_0591.webp 220w,\n/static/ceb49c3c631485453e71e00d7f84b069/63099/IMG_0591.webp 1182w","sizes":"(max-width: 110px) 100vw, 110px"}}}},"primary_tag":{"slug":"react","url":"https://backend.shahednasser.com/tag/react/","name":"React","visibility":"public","feature_image":"https://images.unsplash.com/photo-1581276879432-15e50529f34b?crop=entropy&cs=tinysrgb&fit=max&fm=jpg&ixid=MnwxMTc3M3wwfDF8c2VhcmNofDN8fHJlYWN0fGVufDB8fHx8MTYyMjYzMzI0MA&ixlib=rb-1.2.1&q=80&w=2000","description":"Learn more about React through tutorials, articles, and tips.","meta_title":null,"meta_description":null,"featureImageSharp":{"base":"photo-1581276879432-15e50529f34b.jpg","publicURL":"/static/7140ea32e1157ba61402d5d67ab846d4/photo-1581276879432-15e50529f34b.jpg","imageMeta":{"width":2000,"height":1333},"childImageSharp":{"fluid":{"base64":"data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAANABQDASIAAhEBAxEB/8QAFwAAAwEAAAAAAAAAAAAAAAAAAAIDBf/EABUBAQEAAAAAAAAAAAAAAAAAAAAB/9oADAMBAAIQAxAAAAHJai1MBP/EABcQAAMBAAAAAAAAAAAAAAAAAAABEBH/2gAIAQEAAQUCuDU//8QAFBEBAAAAAAAAAAAAAAAAAAAAEP/aAAgBAwEBPwE//8QAFREBAQAAAAAAAAAAAAAAAAAAEBH/2gAIAQIBAT8Bh//EABQQAQAAAAAAAAAAAAAAAAAAACD/2gAIAQEABj8CX//EABgQAQEBAQEAAAAAAAAAAAAAAAABESFR/9oACAEBAAE/IYy+rHKDH//aAAwDAQACAAMAAAAQC9//xAAVEQEBAAAAAAAAAAAAAAAAAAAQEf/aAAgBAwEBPxCn/8QAFREBAQAAAAAAAAAAAAAAAAAAARD/2gAIAQIBAT8QUT//xAAaEAADAQADAAAAAAAAAAAAAAAAAREhQWFx/9oACAEBAAE/EMY0neijzwMrrVEm7auUQPNGlP/Z","aspectRatio":1.5028901734104045,"src":"/static/7140ea32e1157ba61402d5d67ab846d4/d5c54/photo-1581276879432-15e50529f34b.jpg","srcSet":"/static/7140ea32e1157ba61402d5d67ab846d4/65d8c/photo-1581276879432-15e50529f34b.jpg 260w,\n/static/7140ea32e1157ba61402d5d67ab846d4/c5f21/photo-1581276879432-15e50529f34b.jpg 520w,\n/static/7140ea32e1157ba61402d5d67ab846d4/d5c54/photo-1581276879432-15e50529f34b.jpg 1040w,\n/static/7140ea32e1157ba61402d5d67ab846d4/81a53/photo-1581276879432-15e50529f34b.jpg 1560w,\n/static/7140ea32e1157ba61402d5d67ab846d4/4e5f3/photo-1581276879432-15e50529f34b.jpg 2000w","srcWebp":"/static/7140ea32e1157ba61402d5d67ab846d4/e4875/photo-1581276879432-15e50529f34b.webp","srcSetWebp":"/static/7140ea32e1157ba61402d5d67ab846d4/dc8f3/photo-1581276879432-15e50529f34b.webp 260w,\n/static/7140ea32e1157ba61402d5d67ab846d4/2db4b/photo-1581276879432-15e50529f34b.webp 520w,\n/static/7140ea32e1157ba61402d5d67ab846d4/e4875/photo-1581276879432-15e50529f34b.webp 1040w,\n/static/7140ea32e1157ba61402d5d67ab846d4/f5845/photo-1581276879432-15e50529f34b.webp 1560w,\n/static/7140ea32e1157ba61402d5d67ab846d4/49d6b/photo-1581276879432-15e50529f34b.webp 2000w","sizes":"(max-width: 1040px) 100vw, 1040px"}}}},"tags":[{"slug":"react","url":"https://backend.shahednasser.com/tag/react/","name":"React","visibility":"public","feature_image":"https://images.unsplash.com/photo-1581276879432-15e50529f34b?crop=entropy&cs=tinysrgb&fit=max&fm=jpg&ixid=MnwxMTc3M3wwfDF8c2VhcmNofDN8fHJlYWN0fGVufDB8fHx8MTYyMjYzMzI0MA&ixlib=rb-1.2.1&q=80&w=2000","description":"Learn more about React through tutorials, articles, and tips.","meta_title":null,"meta_description":null,"featureImageSharp":null},{"slug":"js","url":"https://backend.shahednasser.com/tag/js/","name":"Javascript","visibility":"public","feature_image":"https://backend.shahednasser.com/content/images/2022/01/photo-1592609931095-54a2168ae893-2.jpeg","description":"Learn more about Javascript through tutorials, articles, and tips.","meta_title":null,"meta_description":"Learn more about Javascript through tutorials, articles and tips.","featureImageSharp":null}],"plaintext":"Perhaps one of the most annoying tasks in React is creating forms and validating\nthem, especially if you're doing it without using any libraries. You'll have to\nmanage the states, values, and validation of all inputs.\n\nFormik [https://formik.org] is a React and React Native library that helps you\ncreate forms in React \"without the tears\". You can pair Formik with validation\nlibraries like Yup [https://github.com/jquense/yup] to make the process even\nsimpler.\n\nIn this tutorial, you'll learn how creating and validating forms can be simpler\nin React using Formik and Yup. You'll create a simple form with different types\nof fields and see the different ways you can validate that form.\n\nYou can find the code for this tutorial in this GitHub repository\n[https://github.com/shahednasser/react-forms-tutorial].\n\nProject Setup\nIn this section, you'll set up your website using Create React App\n[https://create-react-app.dev] (CRA) and install some dependencies for the sake\nof the tutorial. If you already have a website set up you can skip this part.\n\nIn your terminal, run the following command to create a new React website with\nCRA:\n\nnpx create-react-app react-forms\n\nI'm calling the website react-forms but you can change it to whatever you want.\n\nOnce the installation is done, change to the newly created directory:\n\ncd react-forms\n\nThen, install Tailwind CSS [https://tailwindcss.com] to add some styling to your\nwebsite:\n\nnpm install -D tailwindcss postcss autoprefixer\n\nTo set up Tailwind CSS create the file tailwind.config.js with the following\ncontent:\n\nmodule.exports = {\n  content: [\n    \"./src/**/*.{js,jsx,ts,tsx}\",\n  ],\n  theme: {\n    extend: {},\n  },\n  plugins: [],\n}\n\nAnd replace the content of src/index.css with the following:\n\n@tailwind base;\n@tailwind components;\n@tailwind utilities;\n\nCreate the Form with Formik\nYou'll now use Formik to create a form. First, install Formik:\n\nnpm i formik\n\nReplace the content of src/App.js with the following:\n\nimport { useFormik } from 'formik';\n\nfunction App() {\n    const professions = ['Developer', 'Designer', 'Other'];\n    //TODO create formik instance\n    \n    return (\n    \t<div className=\"bg-blue-300 min-w-screen min-h-screen overflow-x-hidden\">\n        </div>\n    );\n}\n\nexport default App;\n\nAll you did here is create the component App which does nothing special at the\nmoment.\n\nNotice how you import the useFormik hook at the beginning of the file. You'll\nuse this hook to create a Formik instance with all the states and helpers you'll\nneed.\n\nThe useFormik hook accepts as a parameter an object of configurations\n[https://formik.org/docs/api/formik#props-1]. These configurations can be used\nto modify and shape your form as you need.\n\nIn this tutorial, you'll use the pass the following properties in the object:\n\n 1. initialValues: includes the form fields and their initial values.\n 2. validationSchema: A Yup schema to validate the fields. You'll use this in\n    the next section.\n 3. onSubmit: a function to execute when the form is submitted.\n\nReplace the TODO in the App component with the following:\n\nconst formik = useFormik({\n    initialValues: {\n      name: '',\n      email: '',\n      profession: professions[0],\n      age: '',\n    },\n    onSubmit: function (values) {\n      alert(`You are registered! Name: ${values.name}. Email: ${values.email}. Profession: ${values.profession}. \n        Age: ${values.age}`);\n    }\n  })\n\nAs you can see, you set the value of the property initialValues to an object.\nThis object's keys are the names of the fields in the form. Their values are the\ninitial value.\n\nIn the onSubmit function, you receive the values object as a parameter. Here you\ncan access the values and use them to save them in the database or send them to\na server. For the sake of this tutorial, you just print them out in an alert.\n\nNote that the onSubmit function is only executed once the form is validated. So,\nyou don't need to perform any validation inside this function.\n\nNow, you can use the formik variable to create a form, link its fields to the\nfields you defined in useFormik, link the validation, and link the submit\nhandler.\n\nformik includes the following properties among others\n[https://formik.org/docs/api/formik#props-1]:\n\n 1. handleSubmit: the submit function that should be called when the form is\n    submitted. This is usually assigned to the onSubmit event handler of form \n    elements.\n 2. errors: An object that has the field names as properties and the value of\n    each is the error message resulted from validating that field if there are\n    any errors.\n 3. touched: An object that has the field names as properties and the value is a\n    boolean indicating whether the user has interacted with the field or not.\n 4. values: An object that has the field names as properties and the value of\n    each is the current value of that field. It's usually used to set the value \n    property of input elements.\n 5. handleChange: A function that should be used as the handler of the change\n    event of input elements. It's passed as the value of the onChange prop of\n    elements.\n 6. handleBlur: A function that should be used as the handler of the blur event\n    of input elements. It's passed as the value of the onBlur prop of elements.\n\nReplace the return statement in App with the following:\n\nreturn (\n    <div className=\"bg-blue-300 min-w-screen min-h-screen overflow-x-hidden\">\n      <form onSubmit={formik.handleSubmit} className=\"max-w-lg mx-auto bg-white rounded shadow-lg mt-7 p-3\">\n      <h1 className='text-3xl mb-3 text-center'>Register</h1>\n        <div className='mb-4'>\n          <label for=\"name\">Full Name</label>\n          <input type=\"text\" name=\"name\" id=\"name\" \n            className={`block w-full rounded border py-1 px-2 ${formik.touched.name && formik.errors.name ? 'border-red-400' : 'border-gray-300'}`}\n            onChange={formik.handleChange} onBlur={formik.handleBlur} value={formik.values.name} />\n          {formik.touched.name && formik.errors.name && (\n            <span className='text-red-400'>{formik.errors.name}</span>\n          )}\n        </div>\n        <div className='mb-4'>\n          <label for=\"email\">Email</label>\n          <input type=\"email\" name=\"email\" id=\"email\"\n            className={`block w-full rounded border py-1 px-2 ${formik.touched.email && formik.errors.email ? 'border-red-400' : 'border-gray-300'}`}\n            onChange={formik.handleChange} onBlur={formik.handleBlur} value={formik.values.email} />\n          {formik.touched.email && formik.errors.email && (\n            <span className='text-red-400'>{formik.errors.email}</span>\n          )}\n        </div>\n        <div className='mb-4'>\n          <label for=\"profession\">Profession</label>\n          <select name=\"profession\" id=\"profession\"\n            className={`block w-full rounded border py-1 px-2 ${formik.touched.profession && formik.errors.profession ? 'border-red-400' : 'border-gray-300'}`}\n            onChange={formik.handleChange} onBlur={formik.handleBlur} value={formik.values.profession} >\n            {professions.map((profession, index) => (\n              <option value={profession} key={index}>{profession}</option>\n            ))}\n          </select>\n          {formik.touched.profession && formik.errors.profession && (\n            <span className='text-red-400'>{formik.errors.profession}</span>\n          )}\n        </div>\n        <div className='mb-4'>\n          <label for=\"age\">Age</label>\n          <input type=\"number\" name=\"age\" id=\"age\"\n            className={`block w-full rounded border py-1 px-2 ${formik.touched.age && formik.errors.age ? 'border-red-400' : 'border-gray-300'}`}\n            onChange={formik.handleChange} onBlur={formik.handleBlur} value={formik.values.age} />\n          {formik.touched.age && formik.errors.age && (\n            <span className='text-red-400'>{formik.errors.age}</span>\n          )}\n        </div>\n        <div className='text-center'>\n          <button className='bg-blue-500 rounded p-3 text-white' type='submit'>Submit</button>\n        </div>\n      </form>\n    </div>\n  );\n\nNotice how you used all the properties in the formik variable mentioned earlier.\n\nTest it Out\nThe form is now created and ready to be used, even if there's no validation yet.\n\nTo test it out, run the server using the following command:\n\nnpm start\n\nYou can then open the website at localhost:3000 (default port). If you open the\nwebsite, you'll see the form with 4 fields.\n\nYou can try and fill out the form. As currently there's no validation, you can\nfill out (or not) values as you want and click Submit. An alert will show with\nthe values you entered.\n\nAdd Validation with Yup\nIn this section, you'll add validation to the form using Yup.\n\nFirst, you need to install Yup. Run the following in your terminal:\n\nnpm i yup\n\nYup has a lot of methods and validation rules\n[https://github.com/jquense/yup#table-of-contents] you can use. The way it works\nwith Formik is you need to create a validation schema and pass it to useFormik \nas a value to the property validationSchema.\n\nYup validation schemas are created using Yup.object method which takes as a\nparameter an object. This object has the field names as properties and their\nvalues are validation rules from the Yup library.\n\nImport Yup at the beginning of src/App.js:\n\nimport * as Yup from 'yup';\n\nThen, add the property validationSchema to the object passed to useFormik with\nthe following value:\n\nconst formik = useFormik({\n    ...,\n    validationSchema: Yup.object({\n      name: Yup.string()\n              .label('Full Name')\n              .required(),\n      email: Yup.string()\n              .email()\n              .required(),\n      profession: Yup.string()\n                  .oneOf(professions, 'The profession you chose does not exist'),\n      age: Yup.number()\n            .min(15, 'You need to be older than 15 to register')\n            .required()\n    })\n  })\n\nYou add the following validation rules:\n\n 1. name: Should be a string and is required. You also use the label method to\n    ensure that when the error message is shown it refers to the field as \"Full\n    Name\". By default, the fields are referred to by the field name, which in\n    this case is name.\n 2. email: Should be a string, an email, and required.\n 3. profession: Should be a string and one of the values in the professions \n    array. You also pass a message as a second parameter to oneOf which will be\n    the message that shows in case there's an error. It's also required.\n 4. age: Should be a number and at least 15. If the age is less than 15, the\n    message \"You need to be older than 15 to register\" will show. It's also\n    required.\n\nTest it Out\nLet's test it out. Run the server again if it's not running and open the\nwebsite. If you enter values now that don't comply with the rules you set in the\nvalidation schema, an error will show in red and you won't be able to submit the\nform before resolving the errors.\n\nIf you all values are valid, then the form will be submitted and an alert will\nshow.\n\nCustom Validation Rules\nAlthough Yup has helpful validation rules that you can use with most common\ncases, a lot of times you might need a custom validation rule. You can use the \ntest\n[https://github.com/jquense/yup#schematestname-string-message-string--function--any-test-function-schema] \nfunction to add a custom rule.\n\nIn this section, you'll add a rule to make sure that the name field has both\nfirst and last name.\n\nChange the name property inside the validationSchema to the following:\n\nconst formik = useFormik({\n    ...,\n    validationSchema: Yup.object({\n      name: Yup.string()\n              .label('Full Name')\n              .required()\n              .test('is-full-name', 'Please enter both your first and last name', function (value) {\n                const nameArr = value.split(\" \");\n                return nameArr.length >= 2;\n              }),\n      ...\n    })\n  })\n\nThe first parameter is the name of the custom rule. The second parameter is the\nmessage to show in case the field is invalid. \n\nThe third parameter is the function that determines whether the field is valid\nor not. It should return a boolean value. If the value is true, then the field\nis valid. Otherwise, it's invalid.\n\nYou validate the name field to contain both first and last names by just\nsplitting it  on the space delimiter which will return an array. You then check\nthe array length. If it's at least 2, then the field is valid. Otherwise it's\ninvalid.\n\nTest it Out\nRun the server again now and go to the website. If you enter one word in the\nFull Name field you'll see an error.\n\nYou'll need to enter at least two words for the field to be valid.\n\nConclusion\nIn this tutorial you learned how to use Formik and Yup in React. You can use\nthese two libraries to create forms, validate them, and handle their submission.\nUsing these two libraries makes creating forms in React easier and less\nstressful.","html":"<p>Perhaps one of the most annoying tasks in React is creating forms and validating them, especially if you're doing it without using any libraries. You'll have to manage the states, values, and validation of all inputs.</p><p><a href=\"https://formik.org\">Formik</a> is a React and React Native library that helps you create forms in React \"without the tears\". You can pair Formik with validation libraries like <a href=\"https://github.com/jquense/yup\">Yup</a> to make the process even simpler.</p><p>In this tutorial, you'll learn how creating and validating forms can be simpler in React using Formik and Yup. You'll create a simple form with different types of fields and see the different ways you can validate that form.</p><p>You can find the code for this tutorial in <a href=\"https://github.com/shahednasser/react-forms-tutorial\">this GitHub repository</a>.</p><h2 id=\"project-setup\">Project Setup</h2><p>In this section, you'll set up your website using <a href=\"https://create-react-app.dev\">Create React App</a> (CRA) and install some dependencies for the sake of the tutorial. If you already have a website set up you can skip this part.</p><p>In your terminal, run the following command to create a new React website with CRA:</p><pre><code class=\"language-bash\">npx create-react-app react-forms</code></pre><p>I'm calling the website <code>react-forms</code> but you can change it to whatever you want.</p><p>Once the installation is done, change to the newly created directory:</p><pre><code class=\"language-bash\">cd react-forms</code></pre><p>Then, install <a href=\"https://tailwindcss.com\">Tailwind CSS</a> to add some styling to your website:</p><pre><code class=\"language-bash\">npm install -D tailwindcss postcss autoprefixer</code></pre><p>To set up Tailwind CSS create the file <code>tailwind.config.js</code> with the following content:</p><pre><code class=\"language-bash\">module.exports = {\n  content: [\n    \"./src/**/*.{js,jsx,ts,tsx}\",\n  ],\n  theme: {\n    extend: {},\n  },\n  plugins: [],\n}</code></pre><p>And replace the content of <code>src/index.css</code> with the following:</p><pre><code class=\"language-bash\">@tailwind base;\n@tailwind components;\n@tailwind utilities;</code></pre><h2 id=\"create-the-form-with-formik\">Create the Form with Formik</h2><p>You'll now use Formik to create a form. First, install Formik:</p><pre><code class=\"language-bash\">npm i formik</code></pre><p>Replace the content of <code>src/App.js</code> with the following:</p><pre><code class=\"language-js\">import { useFormik } from 'formik';\n\nfunction App() {\n    const professions = ['Developer', 'Designer', 'Other'];\n    //TODO create formik instance\n    \n    return (\n    \t&lt;div className=\"bg-blue-300 min-w-screen min-h-screen overflow-x-hidden\"&gt;\n        &lt;/div&gt;\n    );\n}\n\nexport default App;</code></pre><p>All you did here is create the component App which does nothing special at the moment.</p><p>Notice how you import the <code>useFormik</code> hook at the beginning of the file. You'll use this hook to create a Formik instance with all the states and helpers you'll need.</p><p>The <code>useFormik</code> hook accepts as a parameter an object of <a href=\"https://formik.org/docs/api/formik#props-1\">configurations</a>. These configurations can be used to modify and shape your form as you need.</p><p>In this tutorial, you'll use the pass the following properties in the object:</p><ol><li><code>initialValues</code>: includes the form fields and their initial values.</li><li><code>validationSchema</code>: A Yup schema to validate the fields. You'll use this in the next section.</li><li><code>onSubmit</code>: a function to execute when the form is submitted.</li></ol><p>Replace the <code>TODO</code> in the <code>App</code> component with the following:</p><pre><code class=\"language-js\">const formik = useFormik({\n    initialValues: {\n      name: '',\n      email: '',\n      profession: professions[0],\n      age: '',\n    },\n    onSubmit: function (values) {\n      alert(`You are registered! Name: ${values.name}. Email: ${values.email}. Profession: ${values.profession}. \n        Age: ${values.age}`);\n    }\n  })</code></pre><p>As you can see, you set the value of the property <code>initialValues</code> to an object. This object's keys are the names of the fields in the form. Their values are the initial value.</p><p>In the <code>onSubmit</code> function, you receive the <code>values</code> object as a parameter. Here you can access the values and use them to save them in the database or send them to a server. For the sake of this tutorial, you just print them out in an alert.</p><p>Note that the <code>onSubmit</code> function is only executed once the form is validated. So, you don't need to perform any validation inside this function.</p><p>Now, you can use the <code>formik</code> variable to create a form, link its fields to the fields you defined in <code>useFormik</code>, link the validation, and link the submit handler.</p><p><code>formik</code> includes the following properties among <a href=\"https://formik.org/docs/api/formik#props-1\">others</a>:</p><ol><li><code>handleSubmit</code>: the submit function that should be called when the form is submitted. This is usually assigned to the <code>onSubmit</code> event handler of <code>form</code> elements.</li><li><code>errors</code>: An object that has the field names as properties and the value of each is the error message resulted from validating that field if there are any errors.</li><li><code>touched</code>: An object that has the field names as properties and the value is a boolean indicating whether the user has interacted with the field or not.</li><li><code>values</code>: An object that has the field names as properties and the value of each is the current value of that field. It's usually used to set the <code>value</code> property of input elements.</li><li><code>handleChange</code>: A function that should be used as the handler of the change event of input elements. It's passed as the value of the <code>onChange</code> prop of elements.</li><li><code>handleBlur</code>: A function that should be used as the handler of the blur event of input elements. It's passed as the value of the <code>onBlur</code> prop of elements.</li></ol><p>Replace the return statement in <code>App</code> with the following:</p><pre><code class=\"language-js\">return (\n    &lt;div className=\"bg-blue-300 min-w-screen min-h-screen overflow-x-hidden\"&gt;\n      &lt;form onSubmit={formik.handleSubmit} className=\"max-w-lg mx-auto bg-white rounded shadow-lg mt-7 p-3\"&gt;\n      &lt;h1 className='text-3xl mb-3 text-center'&gt;Register&lt;/h1&gt;\n        &lt;div className='mb-4'&gt;\n          &lt;label for=\"name\"&gt;Full Name&lt;/label&gt;\n          &lt;input type=\"text\" name=\"name\" id=\"name\" \n            className={`block w-full rounded border py-1 px-2 ${formik.touched.name &amp;&amp; formik.errors.name ? 'border-red-400' : 'border-gray-300'}`}\n            onChange={formik.handleChange} onBlur={formik.handleBlur} value={formik.values.name} /&gt;\n          {formik.touched.name &amp;&amp; formik.errors.name &amp;&amp; (\n            &lt;span className='text-red-400'&gt;{formik.errors.name}&lt;/span&gt;\n          )}\n        &lt;/div&gt;\n        &lt;div className='mb-4'&gt;\n          &lt;label for=\"email\"&gt;Email&lt;/label&gt;\n          &lt;input type=\"email\" name=\"email\" id=\"email\"\n            className={`block w-full rounded border py-1 px-2 ${formik.touched.email &amp;&amp; formik.errors.email ? 'border-red-400' : 'border-gray-300'}`}\n            onChange={formik.handleChange} onBlur={formik.handleBlur} value={formik.values.email} /&gt;\n          {formik.touched.email &amp;&amp; formik.errors.email &amp;&amp; (\n            &lt;span className='text-red-400'&gt;{formik.errors.email}&lt;/span&gt;\n          )}\n        &lt;/div&gt;\n        &lt;div className='mb-4'&gt;\n          &lt;label for=\"profession\"&gt;Profession&lt;/label&gt;\n          &lt;select name=\"profession\" id=\"profession\"\n            className={`block w-full rounded border py-1 px-2 ${formik.touched.profession &amp;&amp; formik.errors.profession ? 'border-red-400' : 'border-gray-300'}`}\n            onChange={formik.handleChange} onBlur={formik.handleBlur} value={formik.values.profession} &gt;\n            {professions.map((profession, index) =&gt; (\n              &lt;option value={profession} key={index}&gt;{profession}&lt;/option&gt;\n            ))}\n          &lt;/select&gt;\n          {formik.touched.profession &amp;&amp; formik.errors.profession &amp;&amp; (\n            &lt;span className='text-red-400'&gt;{formik.errors.profession}&lt;/span&gt;\n          )}\n        &lt;/div&gt;\n        &lt;div className='mb-4'&gt;\n          &lt;label for=\"age\"&gt;Age&lt;/label&gt;\n          &lt;input type=\"number\" name=\"age\" id=\"age\"\n            className={`block w-full rounded border py-1 px-2 ${formik.touched.age &amp;&amp; formik.errors.age ? 'border-red-400' : 'border-gray-300'}`}\n            onChange={formik.handleChange} onBlur={formik.handleBlur} value={formik.values.age} /&gt;\n          {formik.touched.age &amp;&amp; formik.errors.age &amp;&amp; (\n            &lt;span className='text-red-400'&gt;{formik.errors.age}&lt;/span&gt;\n          )}\n        &lt;/div&gt;\n        &lt;div className='text-center'&gt;\n          &lt;button className='bg-blue-500 rounded p-3 text-white' type='submit'&gt;Submit&lt;/button&gt;\n        &lt;/div&gt;\n      &lt;/form&gt;\n    &lt;/div&gt;\n  );</code></pre><p>Notice how you used all the properties in the <code>formik</code> variable mentioned earlier.</p><h3 id=\"test-it-out\">Test it Out</h3><p>The form is now created and ready to be used, even if there's no validation yet.</p><p>To test it out, run the server using the following command:</p><pre><code class=\"language-bash\">npm start</code></pre><p>You can then open the website at <code>localhost:3000</code> (default port). If you open the website, you'll see the form with 4 fields.</p><figure class=\"kg-card kg-image-card\"><img src=\"https://backend.shahednasser.com/content/images/2022/03/Screen-Shot-2022-03-19-at-2.49.28-PM-1.png\" class=\"kg-image\" alt loading=\"lazy\" width=\"1084\" height=\"900\"></figure><p>You can try and fill out the form. As currently there's no validation, you can fill out (or not) values as you want and click Submit. An alert will show with the values you entered.</p><figure class=\"kg-card kg-image-card\"><img src=\"https://backend.shahednasser.com/content/images/2022/03/Screen-Shot-2022-03-19-at-2.50.35-PM-1.png\" class=\"kg-image\" alt loading=\"lazy\" width=\"1010\" height=\"410\"></figure><h2 id=\"add-validation-with-yup\">Add Validation with Yup</h2><p>In this section, you'll add validation to the form using Yup.</p><p>First, you need to install Yup. Run the following in your terminal:</p><pre><code class=\"language-bash\">npm i yup</code></pre><p>Yup has a lot of <a href=\"https://github.com/jquense/yup#table-of-contents\">methods and validation rules</a> you can use. The way it works with Formik is you need to create a validation schema and pass it to <code>useFormik</code> as a value to the property <code>validationSchema</code>.</p><p>Yup validation schemas are created using <code>Yup.object</code> method which takes as a parameter an object. This object has the field names as properties and their values are validation rules from the Yup library.</p><p>Import Yup at the beginning of <code>src/App.js</code>:</p><pre><code class=\"language-js\">import * as Yup from 'yup';</code></pre><p>Then, add the property <code>validationSchema</code> to the object passed to <code>useFormik</code> with the following value:</p><pre><code class=\"language-js\">const formik = useFormik({\n    ...,\n    validationSchema: Yup.object({\n      name: Yup.string()\n              .label('Full Name')\n              .required(),\n      email: Yup.string()\n              .email()\n              .required(),\n      profession: Yup.string()\n                  .oneOf(professions, 'The profession you chose does not exist'),\n      age: Yup.number()\n            .min(15, 'You need to be older than 15 to register')\n            .required()\n    })\n  })</code></pre><p>You add the following validation rules:</p><ol><li><code>name</code>: Should be a string and is required. You also use the <code>label</code> method to ensure that when the error message is shown it refers to the field as \"Full Name\". By default, the fields are referred to by the field name, which in this case is <code>name</code>.</li><li><code>email</code>: Should be a string, an email, and required.</li><li><code>profession</code>: Should be a string and one of the values in the <code>professions</code> array. You also pass a message as a second parameter to <code>oneOf</code> which will be the message that shows in case there's an error. It's also required.</li><li><code>age</code>: Should be a number and at least 15. If the age is less than 15, the message \"You need to be older than 15 to register\" will show. It's also required.</li></ol><h3 id=\"test-it-out-1\">Test it Out</h3><p>Let's test it out. Run the server again if it's not running and open the website. If you enter values now that don't comply with the rules you set in the validation schema, an error will show in red and you won't be able to submit the form before resolving the errors.</p><figure class=\"kg-card kg-image-card\"><img src=\"https://backend.shahednasser.com/content/images/2022/03/Screen-Shot-2022-03-19-at-2.55.58-PM-1.png\" class=\"kg-image\" alt loading=\"lazy\" width=\"1102\" height=\"938\"></figure><p>If you all values are valid, then the form will be submitted and an alert will show.</p><h3 id=\"custom-validation-rules\">Custom Validation Rules</h3><p>Although Yup has helpful validation rules that you can use with most common cases, a lot of times you might need a custom validation rule. You can use the <a href=\"https://github.com/jquense/yup#schematestname-string-message-string--function--any-test-function-schema\">test</a> function to add a custom rule.</p><p>In this section, you'll add a rule to make sure that the <code>name</code> field has both first and last name.</p><p>Change the <code>name</code> property inside the <code>validationSchema</code> to the following:</p><pre><code class=\"language-js\">const formik = useFormik({\n    ...,\n    validationSchema: Yup.object({\n      name: Yup.string()\n              .label('Full Name')\n              .required()\n              .test('is-full-name', 'Please enter both your first and last name', function (value) {\n                const nameArr = value.split(\" \");\n                return nameArr.length &gt;= 2;\n              }),\n      ...\n    })\n  })</code></pre><p>The first parameter is the name of the custom rule. The second parameter is the message to show in case the field is invalid. </p><p>The third parameter is the function that determines whether the field is valid or not. It should return a boolean value. If the value is true, then the field is valid. Otherwise, it's invalid.</p><p>You validate the name field to contain both first and last names by just splitting it  on the space delimiter which will return an array. You then check the array length. If it's at least 2, then the field is valid. Otherwise it's invalid.</p><h3 id=\"test-it-out-2\">Test it Out</h3><p>Run the server again now and go to the website. If you enter one word in the Full Name field you'll see an error.</p><figure class=\"kg-card kg-image-card\"><img src=\"https://backend.shahednasser.com/content/images/2022/03/us9mldxrs5dvf174r4w9.png.jpeg\" class=\"kg-image\" alt loading=\"lazy\" width=\"880\" height=\"192\"></figure><p>You'll need to enter at least two words for the field to be valid.</p><h2 id=\"conclusion\">Conclusion</h2><p>In this tutorial you learned how to use Formik and Yup in React. You can use these two libraries to create forms, validate them, and handle their submission. Using these two libraries makes creating forms in React easier and less stressful.</p>","url":"https://backend.shahednasser.com/how-to-create-and-validate-forms-in-react-using-formik-and-yup/","canonical_url":null,"uuid":"a59816b7-049f-45fe-9497-bb708c803a15","codeinjection_foot":null,"codeinjection_head":null,"codeinjection_styles":null,"comment_id":"6235d3ec1594e705e60e186f","reading_time":8,"send_email_when_published":null,"email_subject":null,"childHtmlRehype":{"html":"<p>Perhaps one of the most annoying tasks in React is creating forms and validating them, especially if you're doing it without using any libraries. You'll have to manage the states, values, and validation of all inputs.</p><p><a href=\"https://formik.org\">Formik</a> is a React and React Native library that helps you create forms in React \"without the tears\". You can pair Formik with validation libraries like <a href=\"https://github.com/jquense/yup\">Yup</a> to make the process even simpler.</p><p>In this tutorial, you'll learn how creating and validating forms can be simpler in React using Formik and Yup. You'll create a simple form with different types of fields and see the different ways you can validate that form.</p><p>You can find the code for this tutorial in <a href=\"https://github.com/shahednasser/react-forms-tutorial\">this GitHub repository</a>.</p><h2 id=\"project-setup\">Project Setup</h2><p>In this section, you'll set up your website using <a href=\"https://create-react-app.dev\">Create React App</a> (CRA) and install some dependencies for the sake of the tutorial. If you already have a website set up you can skip this part.</p><p>In your terminal, run the following command to create a new React website with CRA:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\">npx create-react-app react-forms</code></pre></div><p>I'm calling the website <code class=\"language-text\">react-forms</code> but you can change it to whatever you want.</p><p>Once the installation is done, change to the newly created directory:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\"><span class=\"token builtin class-name\">cd</span> react-forms</code></pre></div><p>Then, install <a href=\"https://tailwindcss.com\">Tailwind CSS</a> to add some styling to your website:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\"><span class=\"token function\">npm</span> <span class=\"token function\">install</span> -D tailwindcss postcss autoprefixer</code></pre></div><p>To set up Tailwind CSS create the file <code class=\"language-text\">tailwind.config.js</code> with the following content:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\">module.exports <span class=\"token operator\">=</span> <span class=\"token punctuation\">{</span>\n  content: <span class=\"token punctuation\">[</span>\n    <span class=\"token string\">\"./src/**/*.{js,jsx,ts,tsx}\"</span>,\n  <span class=\"token punctuation\">]</span>,\n  theme: <span class=\"token punctuation\">{</span>\n    extend: <span class=\"token punctuation\">{</span><span class=\"token punctuation\">}</span>,\n  <span class=\"token punctuation\">}</span>,\n  plugins: <span class=\"token punctuation\">[</span><span class=\"token punctuation\">]</span>,\n<span class=\"token punctuation\">}</span></code></pre></div><p>And replace the content of <code class=\"language-text\">src/index.css</code> with the following:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\">@tailwind base<span class=\"token punctuation\">;</span>\n@tailwind components<span class=\"token punctuation\">;</span>\n@tailwind utilities<span class=\"token punctuation\">;</span></code></pre></div><h2 id=\"create-the-form-with-formik\">Create the Form with Formik</h2><p>You'll now use Formik to create a form. First, install Formik:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\"><span class=\"token function\">npm</span> i formik</code></pre></div><p>Replace the content of <code class=\"language-text\">src/App.js</code> with the following:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> useFormik <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'formik'</span><span class=\"token punctuation\">;</span>\n\n<span class=\"token keyword\">function</span> <span class=\"token function\">App</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token keyword\">const</span> professions <span class=\"token operator\">=</span> <span class=\"token punctuation\">[</span><span class=\"token string\">'Developer'</span><span class=\"token punctuation\">,</span> <span class=\"token string\">'Designer'</span><span class=\"token punctuation\">,</span> <span class=\"token string\">'Other'</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span>\n    <span class=\"token comment\">//TODO create formik instance</span>\n    \n    <span class=\"token keyword\">return</span> <span class=\"token punctuation\">(</span>\n    \t<span class=\"token operator\">&#x3C;</span>div className<span class=\"token operator\">=</span><span class=\"token string\">\"bg-blue-300 min-w-screen min-h-screen overflow-x-hidden\"</span><span class=\"token operator\">></span>\n        <span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>div<span class=\"token operator\">></span>\n    <span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span>\n\n<span class=\"token keyword\">export</span> <span class=\"token keyword\">default</span> App<span class=\"token punctuation\">;</span></code></pre></div><p>All you did here is create the component App which does nothing special at the moment.</p><p>Notice how you import the <code class=\"language-text\">useFormik</code> hook at the beginning of the file. You'll use this hook to create a Formik instance with all the states and helpers you'll need.</p><p>The <code class=\"language-text\">useFormik</code> hook accepts as a parameter an object of <a href=\"https://formik.org/docs/api/formik#props-1\">configurations</a>. These configurations can be used to modify and shape your form as you need.</p><p>In this tutorial, you'll use the pass the following properties in the object:</p><ol><li><code class=\"language-text\">initialValues</code>: includes the form fields and their initial values.</li><li><code class=\"language-text\">validationSchema</code>: A Yup schema to validate the fields. You'll use this in the next section.</li><li><code class=\"language-text\">onSubmit</code>: a function to execute when the form is submitted.</li></ol><p>Replace the <code class=\"language-text\">TODO</code> in the <code class=\"language-text\">App</code> component with the following:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token keyword\">const</span> formik <span class=\"token operator\">=</span> <span class=\"token function\">useFormik</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span>\n    <span class=\"token literal-property property\">initialValues</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span>\n      <span class=\"token literal-property property\">name</span><span class=\"token operator\">:</span> <span class=\"token string\">''</span><span class=\"token punctuation\">,</span>\n      <span class=\"token literal-property property\">email</span><span class=\"token operator\">:</span> <span class=\"token string\">''</span><span class=\"token punctuation\">,</span>\n      <span class=\"token literal-property property\">profession</span><span class=\"token operator\">:</span> professions<span class=\"token punctuation\">[</span><span class=\"token number\">0</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">,</span>\n      <span class=\"token literal-property property\">age</span><span class=\"token operator\">:</span> <span class=\"token string\">''</span><span class=\"token punctuation\">,</span>\n    <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\n    <span class=\"token function-variable function\">onSubmit</span><span class=\"token operator\">:</span> <span class=\"token keyword\">function</span> <span class=\"token punctuation\">(</span><span class=\"token parameter\">values</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n      <span class=\"token function\">alert</span><span class=\"token punctuation\">(</span><span class=\"token template-string\"><span class=\"token template-punctuation string\">`</span><span class=\"token string\">You are registered! Name: </span><span class=\"token interpolation\"><span class=\"token interpolation-punctuation punctuation\">${</span>values<span class=\"token punctuation\">.</span>name<span class=\"token interpolation-punctuation punctuation\">}</span></span><span class=\"token string\">. Email: </span><span class=\"token interpolation\"><span class=\"token interpolation-punctuation punctuation\">${</span>values<span class=\"token punctuation\">.</span>email<span class=\"token interpolation-punctuation punctuation\">}</span></span><span class=\"token string\">. Profession: </span><span class=\"token interpolation\"><span class=\"token interpolation-punctuation punctuation\">${</span>values<span class=\"token punctuation\">.</span>profession<span class=\"token interpolation-punctuation punctuation\">}</span></span><span class=\"token string\">. \n        Age: </span><span class=\"token interpolation\"><span class=\"token interpolation-punctuation punctuation\">${</span>values<span class=\"token punctuation\">.</span>age<span class=\"token interpolation-punctuation punctuation\">}</span></span><span class=\"token template-punctuation string\">`</span></span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n    <span class=\"token punctuation\">}</span>\n  <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span></code></pre></div><p>As you can see, you set the value of the property <code class=\"language-text\">initialValues</code> to an object. This object's keys are the names of the fields in the form. Their values are the initial value.</p><p>In the <code class=\"language-text\">onSubmit</code> function, you receive the <code class=\"language-text\">values</code> object as a parameter. Here you can access the values and use them to save them in the database or send them to a server. For the sake of this tutorial, you just print them out in an alert.</p><p>Note that the <code class=\"language-text\">onSubmit</code> function is only executed once the form is validated. So, you don't need to perform any validation inside this function.</p><p>Now, you can use the <code class=\"language-text\">formik</code> variable to create a form, link its fields to the fields you defined in <code class=\"language-text\">useFormik</code>, link the validation, and link the submit handler.</p><p><code class=\"language-text\">formik</code> includes the following properties among <a href=\"https://formik.org/docs/api/formik#props-1\">others</a>:</p><ol><li><code class=\"language-text\">handleSubmit</code>: the submit function that should be called when the form is submitted. This is usually assigned to the <code class=\"language-text\">onSubmit</code> event handler of <code class=\"language-text\">form</code> elements.</li><li><code class=\"language-text\">errors</code>: An object that has the field names as properties and the value of each is the error message resulted from validating that field if there are any errors.</li><li><code class=\"language-text\">touched</code>: An object that has the field names as properties and the value is a boolean indicating whether the user has interacted with the field or not.</li><li><code class=\"language-text\">values</code>: An object that has the field names as properties and the value of each is the current value of that field. It's usually used to set the <code class=\"language-text\">value</code> property of input elements.</li><li><code class=\"language-text\">handleChange</code>: A function that should be used as the handler of the change event of input elements. It's passed as the value of the <code class=\"language-text\">onChange</code> prop of elements.</li><li><code class=\"language-text\">handleBlur</code>: A function that should be used as the handler of the blur event of input elements. It's passed as the value of the <code class=\"language-text\">onBlur</code> prop of elements.</li></ol><p>Replace the return statement in <code class=\"language-text\">App</code> with the following:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token keyword\">return</span> <span class=\"token punctuation\">(</span>\n    <span class=\"token operator\">&#x3C;</span>div className<span class=\"token operator\">=</span><span class=\"token string\">\"bg-blue-300 min-w-screen min-h-screen overflow-x-hidden\"</span><span class=\"token operator\">></span>\n      <span class=\"token operator\">&#x3C;</span>form onSubmit<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span>formik<span class=\"token punctuation\">.</span>handleSubmit<span class=\"token punctuation\">}</span> className<span class=\"token operator\">=</span><span class=\"token string\">\"max-w-lg mx-auto bg-white rounded shadow-lg mt-7 p-3\"</span><span class=\"token operator\">></span>\n      <span class=\"token operator\">&#x3C;</span>h1 className<span class=\"token operator\">=</span><span class=\"token string\">'text-3xl mb-3 text-center'</span><span class=\"token operator\">></span>Register<span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>h1<span class=\"token operator\">></span>\n        <span class=\"token operator\">&#x3C;</span>div className<span class=\"token operator\">=</span><span class=\"token string\">'mb-4'</span><span class=\"token operator\">></span>\n          <span class=\"token operator\">&#x3C;</span>label <span class=\"token keyword\">for</span><span class=\"token operator\">=</span><span class=\"token string\">\"name\"</span><span class=\"token operator\">></span>Full Name<span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>label<span class=\"token operator\">></span>\n          <span class=\"token operator\">&#x3C;</span>input type<span class=\"token operator\">=</span><span class=\"token string\">\"text\"</span> name<span class=\"token operator\">=</span><span class=\"token string\">\"name\"</span> id<span class=\"token operator\">=</span><span class=\"token string\">\"name\"</span> \n            className<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span><span class=\"token template-string\"><span class=\"token template-punctuation string\">`</span><span class=\"token string\">block w-full rounded border py-1 px-2 </span><span class=\"token interpolation\"><span class=\"token interpolation-punctuation punctuation\">${</span>formik<span class=\"token punctuation\">.</span>touched<span class=\"token punctuation\">.</span>name <span class=\"token operator\">&#x26;&#x26;</span> formik<span class=\"token punctuation\">.</span>errors<span class=\"token punctuation\">.</span>name <span class=\"token operator\">?</span> <span class=\"token string\">'border-red-400'</span> <span class=\"token operator\">:</span> <span class=\"token string\">'border-gray-300'</span><span class=\"token interpolation-punctuation punctuation\">}</span></span><span class=\"token template-punctuation string\">`</span></span><span class=\"token punctuation\">}</span>\n            onChange<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span>formik<span class=\"token punctuation\">.</span>handleChange<span class=\"token punctuation\">}</span> onBlur<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span>formik<span class=\"token punctuation\">.</span>handleBlur<span class=\"token punctuation\">}</span> value<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span>formik<span class=\"token punctuation\">.</span>values<span class=\"token punctuation\">.</span>name<span class=\"token punctuation\">}</span> <span class=\"token operator\">/</span><span class=\"token operator\">></span>\n          <span class=\"token punctuation\">{</span>formik<span class=\"token punctuation\">.</span>touched<span class=\"token punctuation\">.</span>name <span class=\"token operator\">&#x26;&#x26;</span> formik<span class=\"token punctuation\">.</span>errors<span class=\"token punctuation\">.</span>name <span class=\"token operator\">&#x26;&#x26;</span> <span class=\"token punctuation\">(</span>\n            <span class=\"token operator\">&#x3C;</span>span className<span class=\"token operator\">=</span><span class=\"token string\">'text-red-400'</span><span class=\"token operator\">></span><span class=\"token punctuation\">{</span>formik<span class=\"token punctuation\">.</span>errors<span class=\"token punctuation\">.</span>name<span class=\"token punctuation\">}</span><span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>span<span class=\"token operator\">></span>\n          <span class=\"token punctuation\">)</span><span class=\"token punctuation\">}</span>\n        <span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>div<span class=\"token operator\">></span>\n        <span class=\"token operator\">&#x3C;</span>div className<span class=\"token operator\">=</span><span class=\"token string\">'mb-4'</span><span class=\"token operator\">></span>\n          <span class=\"token operator\">&#x3C;</span>label <span class=\"token keyword\">for</span><span class=\"token operator\">=</span><span class=\"token string\">\"email\"</span><span class=\"token operator\">></span>Email<span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>label<span class=\"token operator\">></span>\n          <span class=\"token operator\">&#x3C;</span>input type<span class=\"token operator\">=</span><span class=\"token string\">\"email\"</span> name<span class=\"token operator\">=</span><span class=\"token string\">\"email\"</span> id<span class=\"token operator\">=</span><span class=\"token string\">\"email\"</span>\n            className<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span><span class=\"token template-string\"><span class=\"token template-punctuation string\">`</span><span class=\"token string\">block w-full rounded border py-1 px-2 </span><span class=\"token interpolation\"><span class=\"token interpolation-punctuation punctuation\">${</span>formik<span class=\"token punctuation\">.</span>touched<span class=\"token punctuation\">.</span>email <span class=\"token operator\">&#x26;&#x26;</span> formik<span class=\"token punctuation\">.</span>errors<span class=\"token punctuation\">.</span>email <span class=\"token operator\">?</span> <span class=\"token string\">'border-red-400'</span> <span class=\"token operator\">:</span> <span class=\"token string\">'border-gray-300'</span><span class=\"token interpolation-punctuation punctuation\">}</span></span><span class=\"token template-punctuation string\">`</span></span><span class=\"token punctuation\">}</span>\n            onChange<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span>formik<span class=\"token punctuation\">.</span>handleChange<span class=\"token punctuation\">}</span> onBlur<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span>formik<span class=\"token punctuation\">.</span>handleBlur<span class=\"token punctuation\">}</span> value<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span>formik<span class=\"token punctuation\">.</span>values<span class=\"token punctuation\">.</span>email<span class=\"token punctuation\">}</span> <span class=\"token operator\">/</span><span class=\"token operator\">></span>\n          <span class=\"token punctuation\">{</span>formik<span class=\"token punctuation\">.</span>touched<span class=\"token punctuation\">.</span>email <span class=\"token operator\">&#x26;&#x26;</span> formik<span class=\"token punctuation\">.</span>errors<span class=\"token punctuation\">.</span>email <span class=\"token operator\">&#x26;&#x26;</span> <span class=\"token punctuation\">(</span>\n            <span class=\"token operator\">&#x3C;</span>span className<span class=\"token operator\">=</span><span class=\"token string\">'text-red-400'</span><span class=\"token operator\">></span><span class=\"token punctuation\">{</span>formik<span class=\"token punctuation\">.</span>errors<span class=\"token punctuation\">.</span>email<span class=\"token punctuation\">}</span><span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>span<span class=\"token operator\">></span>\n          <span class=\"token punctuation\">)</span><span class=\"token punctuation\">}</span>\n        <span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>div<span class=\"token operator\">></span>\n        <span class=\"token operator\">&#x3C;</span>div className<span class=\"token operator\">=</span><span class=\"token string\">'mb-4'</span><span class=\"token operator\">></span>\n          <span class=\"token operator\">&#x3C;</span>label <span class=\"token keyword\">for</span><span class=\"token operator\">=</span><span class=\"token string\">\"profession\"</span><span class=\"token operator\">></span>Profession<span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>label<span class=\"token operator\">></span>\n          <span class=\"token operator\">&#x3C;</span>select name<span class=\"token operator\">=</span><span class=\"token string\">\"profession\"</span> id<span class=\"token operator\">=</span><span class=\"token string\">\"profession\"</span>\n            className<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span><span class=\"token template-string\"><span class=\"token template-punctuation string\">`</span><span class=\"token string\">block w-full rounded border py-1 px-2 </span><span class=\"token interpolation\"><span class=\"token interpolation-punctuation punctuation\">${</span>formik<span class=\"token punctuation\">.</span>touched<span class=\"token punctuation\">.</span>profession <span class=\"token operator\">&#x26;&#x26;</span> formik<span class=\"token punctuation\">.</span>errors<span class=\"token punctuation\">.</span>profession <span class=\"token operator\">?</span> <span class=\"token string\">'border-red-400'</span> <span class=\"token operator\">:</span> <span class=\"token string\">'border-gray-300'</span><span class=\"token interpolation-punctuation punctuation\">}</span></span><span class=\"token template-punctuation string\">`</span></span><span class=\"token punctuation\">}</span>\n            onChange<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span>formik<span class=\"token punctuation\">.</span>handleChange<span class=\"token punctuation\">}</span> onBlur<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span>formik<span class=\"token punctuation\">.</span>handleBlur<span class=\"token punctuation\">}</span> value<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span>formik<span class=\"token punctuation\">.</span>values<span class=\"token punctuation\">.</span>profession<span class=\"token punctuation\">}</span> <span class=\"token operator\">></span>\n            <span class=\"token punctuation\">{</span>professions<span class=\"token punctuation\">.</span><span class=\"token function\">map</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">profession<span class=\"token punctuation\">,</span> index</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">(</span>\n              <span class=\"token operator\">&#x3C;</span>option value<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span>profession<span class=\"token punctuation\">}</span> key<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span>index<span class=\"token punctuation\">}</span><span class=\"token operator\">></span><span class=\"token punctuation\">{</span>profession<span class=\"token punctuation\">}</span><span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>option<span class=\"token operator\">></span>\n            <span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">}</span>\n          <span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>select<span class=\"token operator\">></span>\n          <span class=\"token punctuation\">{</span>formik<span class=\"token punctuation\">.</span>touched<span class=\"token punctuation\">.</span>profession <span class=\"token operator\">&#x26;&#x26;</span> formik<span class=\"token punctuation\">.</span>errors<span class=\"token punctuation\">.</span>profession <span class=\"token operator\">&#x26;&#x26;</span> <span class=\"token punctuation\">(</span>\n            <span class=\"token operator\">&#x3C;</span>span className<span class=\"token operator\">=</span><span class=\"token string\">'text-red-400'</span><span class=\"token operator\">></span><span class=\"token punctuation\">{</span>formik<span class=\"token punctuation\">.</span>errors<span class=\"token punctuation\">.</span>profession<span class=\"token punctuation\">}</span><span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>span<span class=\"token operator\">></span>\n          <span class=\"token punctuation\">)</span><span class=\"token punctuation\">}</span>\n        <span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>div<span class=\"token operator\">></span>\n        <span class=\"token operator\">&#x3C;</span>div className<span class=\"token operator\">=</span><span class=\"token string\">'mb-4'</span><span class=\"token operator\">></span>\n          <span class=\"token operator\">&#x3C;</span>label <span class=\"token keyword\">for</span><span class=\"token operator\">=</span><span class=\"token string\">\"age\"</span><span class=\"token operator\">></span>Age<span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>label<span class=\"token operator\">></span>\n          <span class=\"token operator\">&#x3C;</span>input type<span class=\"token operator\">=</span><span class=\"token string\">\"number\"</span> name<span class=\"token operator\">=</span><span class=\"token string\">\"age\"</span> id<span class=\"token operator\">=</span><span class=\"token string\">\"age\"</span>\n            className<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span><span class=\"token template-string\"><span class=\"token template-punctuation string\">`</span><span class=\"token string\">block w-full rounded border py-1 px-2 </span><span class=\"token interpolation\"><span class=\"token interpolation-punctuation punctuation\">${</span>formik<span class=\"token punctuation\">.</span>touched<span class=\"token punctuation\">.</span>age <span class=\"token operator\">&#x26;&#x26;</span> formik<span class=\"token punctuation\">.</span>errors<span class=\"token punctuation\">.</span>age <span class=\"token operator\">?</span> <span class=\"token string\">'border-red-400'</span> <span class=\"token operator\">:</span> <span class=\"token string\">'border-gray-300'</span><span class=\"token interpolation-punctuation punctuation\">}</span></span><span class=\"token template-punctuation string\">`</span></span><span class=\"token punctuation\">}</span>\n            onChange<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span>formik<span class=\"token punctuation\">.</span>handleChange<span class=\"token punctuation\">}</span> onBlur<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span>formik<span class=\"token punctuation\">.</span>handleBlur<span class=\"token punctuation\">}</span> value<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span>formik<span class=\"token punctuation\">.</span>values<span class=\"token punctuation\">.</span>age<span class=\"token punctuation\">}</span> <span class=\"token operator\">/</span><span class=\"token operator\">></span>\n          <span class=\"token punctuation\">{</span>formik<span class=\"token punctuation\">.</span>touched<span class=\"token punctuation\">.</span>age <span class=\"token operator\">&#x26;&#x26;</span> formik<span class=\"token punctuation\">.</span>errors<span class=\"token punctuation\">.</span>age <span class=\"token operator\">&#x26;&#x26;</span> <span class=\"token punctuation\">(</span>\n            <span class=\"token operator\">&#x3C;</span>span className<span class=\"token operator\">=</span><span class=\"token string\">'text-red-400'</span><span class=\"token operator\">></span><span class=\"token punctuation\">{</span>formik<span class=\"token punctuation\">.</span>errors<span class=\"token punctuation\">.</span>age<span class=\"token punctuation\">}</span><span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>span<span class=\"token operator\">></span>\n          <span class=\"token punctuation\">)</span><span class=\"token punctuation\">}</span>\n        <span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>div<span class=\"token operator\">></span>\n        <span class=\"token operator\">&#x3C;</span>div className<span class=\"token operator\">=</span><span class=\"token string\">'text-center'</span><span class=\"token operator\">></span>\n          <span class=\"token operator\">&#x3C;</span>button className<span class=\"token operator\">=</span><span class=\"token string\">'bg-blue-500 rounded p-3 text-white'</span> type<span class=\"token operator\">=</span><span class=\"token string\">'submit'</span><span class=\"token operator\">></span>Submit<span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>button<span class=\"token operator\">></span>\n        <span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>div<span class=\"token operator\">></span>\n      <span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>form<span class=\"token operator\">></span>\n    <span class=\"token operator\">&#x3C;</span><span class=\"token operator\">/</span>div<span class=\"token operator\">></span>\n  <span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code></pre></div><p>Notice how you used all the properties in the <code class=\"language-text\">formik</code> variable mentioned earlier.</p><h3 id=\"test-it-out\">Test it Out</h3><p>The form is now created and ready to be used, even if there's no validation yet.</p><p>To test it out, run the server using the following command:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\"><span class=\"token function\">npm</span> start</code></pre></div><p>You can then open the website at <code class=\"language-text\">localhost:3000</code> (default port). If you open the website, you'll see the form with 4 fields.</p><figure class=\"kg-card kg-image-card\"><img src=\"https://backend.shahednasser.com/content/images/2022/03/Screen-Shot-2022-03-19-at-2.49.28-PM-1.png\" class=\"kg-image\" alt=\"\" loading=\"lazy\" width=\"1084\" height=\"900\"></figure><p>You can try and fill out the form. As currently there's no validation, you can fill out (or not) values as you want and click Submit. An alert will show with the values you entered.</p><figure class=\"kg-card kg-image-card\"><img src=\"https://backend.shahednasser.com/content/images/2022/03/Screen-Shot-2022-03-19-at-2.50.35-PM-1.png\" class=\"kg-image\" alt=\"\" loading=\"lazy\" width=\"1010\" height=\"410\"></figure><h2 id=\"add-validation-with-yup\">Add Validation with Yup</h2><p>In this section, you'll add validation to the form using Yup.</p><p>First, you need to install Yup. Run the following in your terminal:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\"><span class=\"token function\">npm</span> i yup</code></pre></div><p>Yup has a lot of <a href=\"https://github.com/jquense/yup#table-of-contents\">methods and validation rules</a> you can use. The way it works with Formik is you need to create a validation schema and pass it to <code class=\"language-text\">useFormik</code> as a value to the property <code class=\"language-text\">validationSchema</code>.</p><p>Yup validation schemas are created using <code class=\"language-text\">Yup.object</code> method which takes as a parameter an object. This object has the field names as properties and their values are validation rules from the Yup library.</p><p>Import Yup at the beginning of <code class=\"language-text\">src/App.js</code>:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token keyword\">import</span> <span class=\"token operator\">*</span> <span class=\"token keyword\">as</span> Yup <span class=\"token keyword\">from</span> <span class=\"token string\">'yup'</span><span class=\"token punctuation\">;</span></code></pre></div><p>Then, add the property <code class=\"language-text\">validationSchema</code> to the object passed to <code class=\"language-text\">useFormik</code> with the following value:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token keyword\">const</span> formik <span class=\"token operator\">=</span> <span class=\"token function\">useFormik</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span>\n    <span class=\"token operator\">...</span><span class=\"token punctuation\">,</span>\n    <span class=\"token literal-property property\">validationSchema</span><span class=\"token operator\">:</span> Yup<span class=\"token punctuation\">.</span><span class=\"token function\">object</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span>\n      <span class=\"token literal-property property\">name</span><span class=\"token operator\">:</span> Yup<span class=\"token punctuation\">.</span><span class=\"token function\">string</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\n              <span class=\"token punctuation\">.</span><span class=\"token function\">label</span><span class=\"token punctuation\">(</span><span class=\"token string\">'Full Name'</span><span class=\"token punctuation\">)</span>\n              <span class=\"token punctuation\">.</span><span class=\"token function\">required</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">,</span>\n      <span class=\"token literal-property property\">email</span><span class=\"token operator\">:</span> Yup<span class=\"token punctuation\">.</span><span class=\"token function\">string</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\n              <span class=\"token punctuation\">.</span><span class=\"token function\">email</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\n              <span class=\"token punctuation\">.</span><span class=\"token function\">required</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">,</span>\n      <span class=\"token literal-property property\">profession</span><span class=\"token operator\">:</span> Yup<span class=\"token punctuation\">.</span><span class=\"token function\">string</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\n                  <span class=\"token punctuation\">.</span><span class=\"token function\">oneOf</span><span class=\"token punctuation\">(</span>professions<span class=\"token punctuation\">,</span> <span class=\"token string\">'The profession you chose does not exist'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">,</span>\n      <span class=\"token literal-property property\">age</span><span class=\"token operator\">:</span> Yup<span class=\"token punctuation\">.</span><span class=\"token function\">number</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\n            <span class=\"token punctuation\">.</span><span class=\"token function\">min</span><span class=\"token punctuation\">(</span><span class=\"token number\">15</span><span class=\"token punctuation\">,</span> <span class=\"token string\">'You need to be older than 15 to register'</span><span class=\"token punctuation\">)</span>\n            <span class=\"token punctuation\">.</span><span class=\"token function\">required</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\n    <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span>\n  <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span></code></pre></div><p>You add the following validation rules:</p><ol><li><code class=\"language-text\">name</code>: Should be a string and is required. You also use the <code class=\"language-text\">label</code> method to ensure that when the error message is shown it refers to the field as \"Full Name\". By default, the fields are referred to by the field name, which in this case is <code class=\"language-text\">name</code>.</li><li><code class=\"language-text\">email</code>: Should be a string, an email, and required.</li><li><code class=\"language-text\">profession</code>: Should be a string and one of the values in the <code class=\"language-text\">professions</code> array. You also pass a message as a second parameter to <code class=\"language-text\">oneOf</code> which will be the message that shows in case there's an error. It's also required.</li><li><code class=\"language-text\">age</code>: Should be a number and at least 15. If the age is less than 15, the message \"You need to be older than 15 to register\" will show. It's also required.</li></ol><h3 id=\"test-it-out-1\">Test it Out</h3><p>Let's test it out. Run the server again if it's not running and open the website. If you enter values now that don't comply with the rules you set in the validation schema, an error will show in red and you won't be able to submit the form before resolving the errors.</p><figure class=\"kg-card kg-image-card\"><img src=\"https://backend.shahednasser.com/content/images/2022/03/Screen-Shot-2022-03-19-at-2.55.58-PM-1.png\" class=\"kg-image\" alt=\"\" loading=\"lazy\" width=\"1102\" height=\"938\"></figure><p>If you all values are valid, then the form will be submitted and an alert will show.</p><h3 id=\"custom-validation-rules\">Custom Validation Rules</h3><p>Although Yup has helpful validation rules that you can use with most common cases, a lot of times you might need a custom validation rule. You can use the <a href=\"https://github.com/jquense/yup#schematestname-string-message-string--function--any-test-function-schema\">test</a> function to add a custom rule.</p><p>In this section, you'll add a rule to make sure that the <code class=\"language-text\">name</code> field has both first and last name.</p><p>Change the <code class=\"language-text\">name</code> property inside the <code class=\"language-text\">validationSchema</code> to the following:</p><div class=\"kg-card kg-code-card gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token keyword\">const</span> formik <span class=\"token operator\">=</span> <span class=\"token function\">useFormik</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span>\n    <span class=\"token operator\">...</span><span class=\"token punctuation\">,</span>\n    <span class=\"token literal-property property\">validationSchema</span><span class=\"token operator\">:</span> Yup<span class=\"token punctuation\">.</span><span class=\"token function\">object</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span>\n      <span class=\"token literal-property property\">name</span><span class=\"token operator\">:</span> Yup<span class=\"token punctuation\">.</span><span class=\"token function\">string</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\n              <span class=\"token punctuation\">.</span><span class=\"token function\">label</span><span class=\"token punctuation\">(</span><span class=\"token string\">'Full Name'</span><span class=\"token punctuation\">)</span>\n              <span class=\"token punctuation\">.</span><span class=\"token function\">required</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\n              <span class=\"token punctuation\">.</span><span class=\"token function\">test</span><span class=\"token punctuation\">(</span><span class=\"token string\">'is-full-name'</span><span class=\"token punctuation\">,</span> <span class=\"token string\">'Please enter both your first and last name'</span><span class=\"token punctuation\">,</span> <span class=\"token keyword\">function</span> <span class=\"token punctuation\">(</span><span class=\"token parameter\">value</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n                <span class=\"token keyword\">const</span> nameArr <span class=\"token operator\">=</span> value<span class=\"token punctuation\">.</span><span class=\"token function\">split</span><span class=\"token punctuation\">(</span><span class=\"token string\">\" \"</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n                <span class=\"token keyword\">return</span> nameArr<span class=\"token punctuation\">.</span>length <span class=\"token operator\">>=</span> <span class=\"token number\">2</span><span class=\"token punctuation\">;</span>\n              <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">,</span>\n      <span class=\"token operator\">...</span>\n    <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span>\n  <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span></code></pre></div><p>The first parameter is the name of the custom rule. The second parameter is the message to show in case the field is invalid. </p><p>The third parameter is the function that determines whether the field is valid or not. It should return a boolean value. If the value is true, then the field is valid. Otherwise, it's invalid.</p><p>You validate the name field to contain both first and last names by just splitting it  on the space delimiter which will return an array. You then check the array length. If it's at least 2, then the field is valid. Otherwise it's invalid.</p><h3 id=\"test-it-out-2\">Test it Out</h3><p>Run the server again now and go to the website. If you enter one word in the Full Name field you'll see an error.</p><figure class=\"kg-card kg-image-card\"><img src=\"https://backend.shahednasser.com/content/images/2022/03/us9mldxrs5dvf174r4w9.png.jpeg\" class=\"kg-image\" alt=\"\" loading=\"lazy\" width=\"880\" height=\"192\"></figure><p>You'll need to enter at least two words for the field to be valid.</p><h2 id=\"conclusion\">Conclusion</h2><p>In this tutorial you learned how to use Formik and Yup in React. You can use these two libraries to create forms, validate them, and handle their submission. Using these two libraries makes creating forms in React easier and less stressful.</p>","htmlAst":{"type":"root","children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Perhaps one of the most annoying tasks in React is creating forms and validating them, especially if you're doing it without using any libraries. You'll have to manage the states, values, and validation of all inputs."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"https://formik.org"},"children":[{"type":"text","value":"Formik"}]},{"type":"text","value":" is a React and React Native library that helps you create forms in React \"without the tears\". You can pair Formik with validation libraries like "},{"type":"element","tagName":"a","properties":{"href":"https://github.com/jquense/yup"},"children":[{"type":"text","value":"Yup"}]},{"type":"text","value":" to make the process even simpler."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In this tutorial, you'll learn how creating and validating forms can be simpler in React using Formik and Yup. You'll create a simple form with different types of fields and see the different ways you can validate that form."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You can find the code for this tutorial in "},{"type":"element","tagName":"a","properties":{"href":"https://github.com/shahednasser/react-forms-tutorial"},"children":[{"type":"text","value":"this GitHub repository"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"h2","properties":{"id":"project-setup"},"children":[{"type":"text","value":"Project Setup"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In this section, you'll set up your website using "},{"type":"element","tagName":"a","properties":{"href":"https://create-react-app.dev"},"children":[{"type":"text","value":"Create React App"}]},{"type":"text","value":" (CRA) and install some dependencies for the sake of the tutorial. If you already have a website set up you can skip this part."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In your terminal, run the following command to create a new React website with CRA:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"bash"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-bash"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-bash"]},"children":[{"type":"text","value":"npx create-react-app react-forms"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I'm calling the website "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"react-forms"}]},{"type":"text","value":" but you can change it to whatever you want."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Once the installation is done, change to the newly created directory:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"bash"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-bash"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-bash"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","builtin","class-name"]},"children":[{"type":"text","value":"cd"}]},{"type":"text","value":" react-forms"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Then, install "},{"type":"element","tagName":"a","properties":{"href":"https://tailwindcss.com"},"children":[{"type":"text","value":"Tailwind CSS"}]},{"type":"text","value":" to add some styling to your website:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"bash"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-bash"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-bash"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"npm"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"install"}]},{"type":"text","value":" -D tailwindcss postcss autoprefixer"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"To set up Tailwind CSS create the file "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"tailwind.config.js"}]},{"type":"text","value":" with the following content:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"bash"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-bash"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-bash"]},"children":[{"type":"text","value":"module.exports "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n  content: "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"./src/**/*.{js,jsx,ts,tsx}\""}]},{"type":"text","value":",\n  "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":",\n  theme: "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n    extend: "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":",\n  "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":",\n  plugins: "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":",\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"And replace the content of "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"src/index.css"}]},{"type":"text","value":" with the following:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"bash"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-bash"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-bash"]},"children":[{"type":"text","value":"@tailwind base"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n@tailwind components"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n@tailwind utilities"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"create-the-form-with-formik"},"children":[{"type":"text","value":"Create the Form with Formik"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You'll now use Formik to create a form. First, install Formik:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"bash"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-bash"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-bash"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"npm"}]},{"type":"text","value":" i formik"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Replace the content of "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"src/App.js"}]},{"type":"text","value":" with the following:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"js"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":" useFormik "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'formik'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"function"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"App"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"const"}]},{"type":"text","value":" professions "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'Developer'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'Designer'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'Other'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"//TODO create formik instance"}]},{"type":"text","value":"\n    \n    "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"\n    \t"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"div className"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"bg-blue-300 min-w-screen min-h-screen overflow-x-hidden\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n        "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"div"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"export"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"default"}]},{"type":"text","value":" App"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"All you did here is create the component App which does nothing special at the moment."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Notice how you import the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"useFormik"}]},{"type":"text","value":" hook at the beginning of the file. You'll use this hook to create a Formik instance with all the states and helpers you'll need."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"useFormik"}]},{"type":"text","value":" hook accepts as a parameter an object of "},{"type":"element","tagName":"a","properties":{"href":"https://formik.org/docs/api/formik#props-1"},"children":[{"type":"text","value":"configurations"}]},{"type":"text","value":". These configurations can be used to modify and shape your form as you need."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In this tutorial, you'll use the pass the following properties in the object:"}]},{"type":"element","tagName":"ol","properties":{},"children":[{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"initialValues"}]},{"type":"text","value":": includes the form fields and their initial values."}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"validationSchema"}]},{"type":"text","value":": A Yup schema to validate the fields. You'll use this in the next section."}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"onSubmit"}]},{"type":"text","value":": a function to execute when the form is submitted."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Replace the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"TODO"}]},{"type":"text","value":" in the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"App"}]},{"type":"text","value":" component with the following:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"js"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"const"}]},{"type":"text","value":" formik "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"useFormik"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"initialValues"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n      "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"name"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"''"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n      "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"email"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"''"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n      "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"profession"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" professions"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n      "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"age"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"''"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","function-variable","function"]},"children":[{"type":"text","value":"onSubmit"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"function"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","parameter"]},"children":[{"type":"text","value":"values"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n      "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"alert"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","template-string"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","template-punctuation","string"]},"children":[{"type":"text","value":"`"}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"You are registered! Name: "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","interpolation-punctuation","punctuation"]},"children":[{"type":"text","value":"${"}]},{"type":"text","value":"values"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"type":"element","tagName":"span","properties":{"className":["token","interpolation-punctuation","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":". Email: "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","interpolation-punctuation","punctuation"]},"children":[{"type":"text","value":"${"}]},{"type":"text","value":"values"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"email"},{"type":"element","tagName":"span","properties":{"className":["token","interpolation-punctuation","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":". Profession: "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","interpolation-punctuation","punctuation"]},"children":[{"type":"text","value":"${"}]},{"type":"text","value":"values"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"profession"},{"type":"element","tagName":"span","properties":{"className":["token","interpolation-punctuation","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":". \n        Age: "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","interpolation-punctuation","punctuation"]},"children":[{"type":"text","value":"${"}]},{"type":"text","value":"values"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"age"},{"type":"element","tagName":"span","properties":{"className":["token","interpolation-punctuation","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","template-punctuation","string"]},"children":[{"type":"text","value":"`"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n  "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"As you can see, you set the value of the property "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"initialValues"}]},{"type":"text","value":" to an object. This object's keys are the names of the fields in the form. Their values are the initial value."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"onSubmit"}]},{"type":"text","value":" function, you receive the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"values"}]},{"type":"text","value":" object as a parameter. Here you can access the values and use them to save them in the database or send them to a server. For the sake of this tutorial, you just print them out in an alert."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Note that the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"onSubmit"}]},{"type":"text","value":" function is only executed once the form is validated. So, you don't need to perform any validation inside this function."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Now, you can use the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"formik"}]},{"type":"text","value":" variable to create a form, link its fields to the fields you defined in "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"useFormik"}]},{"type":"text","value":", link the validation, and link the submit handler."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"formik"}]},{"type":"text","value":" includes the following properties among "},{"type":"element","tagName":"a","properties":{"href":"https://formik.org/docs/api/formik#props-1"},"children":[{"type":"text","value":"others"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"ol","properties":{},"children":[{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"handleSubmit"}]},{"type":"text","value":": the submit function that should be called when the form is submitted. This is usually assigned to the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"onSubmit"}]},{"type":"text","value":" event handler of "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"form"}]},{"type":"text","value":" elements."}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"errors"}]},{"type":"text","value":": An object that has the field names as properties and the value of each is the error message resulted from validating that field if there are any errors."}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"touched"}]},{"type":"text","value":": An object that has the field names as properties and the value is a boolean indicating whether the user has interacted with the field or not."}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"values"}]},{"type":"text","value":": An object that has the field names as properties and the value of each is the current value of that field. It's usually used to set the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"value"}]},{"type":"text","value":" property of input elements."}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"handleChange"}]},{"type":"text","value":": A function that should be used as the handler of the change event of input elements. It's passed as the value of the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"onChange"}]},{"type":"text","value":" prop of elements."}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"handleBlur"}]},{"type":"text","value":": A function that should be used as the handler of the blur event of input elements. It's passed as the value of the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"onBlur"}]},{"type":"text","value":" prop of elements."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Replace the return statement in "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"App"}]},{"type":"text","value":" with the following:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"js"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"div className"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"bg-blue-300 min-w-screen min-h-screen overflow-x-hidden\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n      "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"form onSubmit"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"formik"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"handleSubmit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":" className"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"max-w-lg mx-auto bg-white rounded shadow-lg mt-7 p-3\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n      "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"h1 className"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'text-3xl mb-3 text-center'"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"Register"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"h1"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n        "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"div className"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'mb-4'"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n          "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"label "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"name\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"Full Name"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"label"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n          "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"input type"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"text\""}]},{"type":"text","value":" name"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"name\""}]},{"type":"text","value":" id"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"name\""}]},{"type":"text","value":" \n            className"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","template-string"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","template-punctuation","string"]},"children":[{"type":"text","value":"`"}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"block w-full rounded border py-1 px-2 "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","interpolation-punctuation","punctuation"]},"children":[{"type":"text","value":"${"}]},{"type":"text","value":"formik"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"touched"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"&&"}]},{"type":"text","value":" formik"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"errors"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"?"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'border-red-400'"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'border-gray-300'"}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation-punctuation","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","template-punctuation","string"]},"children":[{"type":"text","value":"`"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n            onChange"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"formik"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"handleChange"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":" onBlur"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"formik"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"handleBlur"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":" value"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"formik"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"values"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n          "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"formik"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"touched"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"&&"}]},{"type":"text","value":" formik"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"errors"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"&&"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"\n            "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"span className"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'text-red-400'"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"formik"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"errors"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"span"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n          "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n        "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"div"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n        "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"div className"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'mb-4'"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n          "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"label "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"email\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"Email"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"label"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n          "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"input type"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"email\""}]},{"type":"text","value":" name"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"email\""}]},{"type":"text","value":" id"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"email\""}]},{"type":"text","value":"\n            className"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","template-string"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","template-punctuation","string"]},"children":[{"type":"text","value":"`"}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"block w-full rounded border py-1 px-2 "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","interpolation-punctuation","punctuation"]},"children":[{"type":"text","value":"${"}]},{"type":"text","value":"formik"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"touched"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"email "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"&&"}]},{"type":"text","value":" formik"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"errors"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"email "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"?"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'border-red-400'"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'border-gray-300'"}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation-punctuation","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","template-punctuation","string"]},"children":[{"type":"text","value":"`"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n            onChange"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"formik"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"handleChange"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":" onBlur"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"formik"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"handleBlur"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":" value"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"formik"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"values"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"email"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n          "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"formik"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"touched"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"email "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"&&"}]},{"type":"text","value":" formik"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"errors"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"email "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"&&"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"\n            "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"span className"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'text-red-400'"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"formik"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"errors"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"email"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"span"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n          "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n        "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"div"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n        "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"div className"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'mb-4'"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n          "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"label "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"profession\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"Profession"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"label"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n          "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"select name"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"profession\""}]},{"type":"text","value":" id"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"profession\""}]},{"type":"text","value":"\n            className"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","template-string"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","template-punctuation","string"]},"children":[{"type":"text","value":"`"}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"block w-full rounded border py-1 px-2 "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","interpolation-punctuation","punctuation"]},"children":[{"type":"text","value":"${"}]},{"type":"text","value":"formik"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"touched"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"profession "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"&&"}]},{"type":"text","value":" formik"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"errors"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"profession "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"?"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'border-red-400'"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'border-gray-300'"}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation-punctuation","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","template-punctuation","string"]},"children":[{"type":"text","value":"`"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n            onChange"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"formik"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"handleChange"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":" onBlur"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"formik"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"handleBlur"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":" value"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"formik"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"values"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"profession"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n            "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"professions"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"map"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","parameter"]},"children":[{"type":"text","value":"profession"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" index"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=>"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"\n              "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"option value"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"profession"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":" key"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"index"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"profession"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"option"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n            "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n          "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"select"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n          "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"formik"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"touched"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"profession "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"&&"}]},{"type":"text","value":" formik"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"errors"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"profession "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"&&"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"\n            "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"span className"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'text-red-400'"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"formik"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"errors"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"profession"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"span"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n          "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n        "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"div"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n        "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"div className"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'mb-4'"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n          "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"label "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"age\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"Age"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"label"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n          "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"input type"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"number\""}]},{"type":"text","value":" name"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"age\""}]},{"type":"text","value":" id"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"age\""}]},{"type":"text","value":"\n            className"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","template-string"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","template-punctuation","string"]},"children":[{"type":"text","value":"`"}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"block w-full rounded border py-1 px-2 "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","interpolation-punctuation","punctuation"]},"children":[{"type":"text","value":"${"}]},{"type":"text","value":"formik"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"touched"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"age "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"&&"}]},{"type":"text","value":" formik"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"errors"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"age "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"?"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'border-red-400'"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'border-gray-300'"}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation-punctuation","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","template-punctuation","string"]},"children":[{"type":"text","value":"`"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n            onChange"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"formik"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"handleChange"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":" onBlur"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"formik"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"handleBlur"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":" value"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"formik"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"values"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"age"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n          "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"formik"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"touched"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"age "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"&&"}]},{"type":"text","value":" formik"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"errors"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"age "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"&&"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"\n            "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"span className"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'text-red-400'"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"formik"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"errors"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"age"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"span"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n          "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n        "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"div"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n        "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"div className"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'text-center'"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n          "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"button className"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'bg-blue-500 rounded p-3 text-white'"}]},{"type":"text","value":" type"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'submit'"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"Submit"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"button"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n        "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"div"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n      "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"form"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"div"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\n  "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Notice how you used all the properties in the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"formik"}]},{"type":"text","value":" variable mentioned earlier."}]},{"type":"element","tagName":"h3","properties":{"id":"test-it-out"},"children":[{"type":"text","value":"Test it Out"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The form is now created and ready to be used, even if there's no validation yet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"To test it out, run the server using the following command:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"bash"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-bash"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-bash"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"npm"}]},{"type":"text","value":" start"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You can then open the website at "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"localhost:3000"}]},{"type":"text","value":" (default port). If you open the website, you'll see the form with 4 fields."}]},{"type":"element","tagName":"figure","properties":{"className":["kg-card","kg-image-card"]},"children":[{"type":"element","tagName":"img","properties":{"src":"https://backend.shahednasser.com/content/images/2022/03/Screen-Shot-2022-03-19-at-2.49.28-PM-1.png","className":["kg-image"],"alt":"","loading":"lazy","width":1084,"height":900},"children":[]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You can try and fill out the form. As currently there's no validation, you can fill out (or not) values as you want and click Submit. An alert will show with the values you entered."}]},{"type":"element","tagName":"figure","properties":{"className":["kg-card","kg-image-card"]},"children":[{"type":"element","tagName":"img","properties":{"src":"https://backend.shahednasser.com/content/images/2022/03/Screen-Shot-2022-03-19-at-2.50.35-PM-1.png","className":["kg-image"],"alt":"","loading":"lazy","width":1010,"height":410},"children":[]}]},{"type":"element","tagName":"h2","properties":{"id":"add-validation-with-yup"},"children":[{"type":"text","value":"Add Validation with Yup"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In this section, you'll add validation to the form using Yup."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"First, you need to install Yup. Run the following in your terminal:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"bash"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-bash"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-bash"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"npm"}]},{"type":"text","value":" i yup"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Yup has a lot of "},{"type":"element","tagName":"a","properties":{"href":"https://github.com/jquense/yup#table-of-contents"},"children":[{"type":"text","value":"methods and validation rules"}]},{"type":"text","value":" you can use. The way it works with Formik is you need to create a validation schema and pass it to "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"useFormik"}]},{"type":"text","value":" as a value to the property "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"validationSchema"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Yup validation schemas are created using "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Yup.object"}]},{"type":"text","value":" method which takes as a parameter an object. This object has the field names as properties and their values are validation rules from the Yup library."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Import Yup at the beginning of "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"src/App.js"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"js"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" Yup "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'yup'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Then, add the property "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"validationSchema"}]},{"type":"text","value":" to the object passed to "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"useFormik"}]},{"type":"text","value":" with the following value:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"js"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"const"}]},{"type":"text","value":" formik "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"useFormik"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"..."}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"validationSchema"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Yup"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"object"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n      "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"name"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Yup"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"string"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n              "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"label"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'Full Name'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n              "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"required"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n      "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"email"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Yup"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"string"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n              "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"email"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n              "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"required"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n      "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"profession"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Yup"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"string"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n                  "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"oneOf"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"professions"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'The profession you chose does not exist'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n      "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"age"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Yup"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"number"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n            "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"min"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"15"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'You need to be older than 15 to register'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n            "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"required"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n  "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You add the following validation rules:"}]},{"type":"element","tagName":"ol","properties":{},"children":[{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"name"}]},{"type":"text","value":": Should be a string and is required. You also use the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"label"}]},{"type":"text","value":" method to ensure that when the error message is shown it refers to the field as \"Full Name\". By default, the fields are referred to by the field name, which in this case is "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"name"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"email"}]},{"type":"text","value":": Should be a string, an email, and required."}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"profession"}]},{"type":"text","value":": Should be a string and one of the values in the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"professions"}]},{"type":"text","value":" array. You also pass a message as a second parameter to "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"oneOf"}]},{"type":"text","value":" which will be the message that shows in case there's an error. It's also required."}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"age"}]},{"type":"text","value":": Should be a number and at least 15. If the age is less than 15, the message \"You need to be older than 15 to register\" will show. It's also required."}]}]},{"type":"element","tagName":"h3","properties":{"id":"test-it-out-1"},"children":[{"type":"text","value":"Test it Out"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's test it out. Run the server again if it's not running and open the website. If you enter values now that don't comply with the rules you set in the validation schema, an error will show in red and you won't be able to submit the form before resolving the errors."}]},{"type":"element","tagName":"figure","properties":{"className":["kg-card","kg-image-card"]},"children":[{"type":"element","tagName":"img","properties":{"src":"https://backend.shahednasser.com/content/images/2022/03/Screen-Shot-2022-03-19-at-2.55.58-PM-1.png","className":["kg-image"],"alt":"","loading":"lazy","width":1102,"height":938},"children":[]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If you all values are valid, then the form will be submitted and an alert will show."}]},{"type":"element","tagName":"h3","properties":{"id":"custom-validation-rules"},"children":[{"type":"text","value":"Custom Validation Rules"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Although Yup has helpful validation rules that you can use with most common cases, a lot of times you might need a custom validation rule. You can use the "},{"type":"element","tagName":"a","properties":{"href":"https://github.com/jquense/yup#schematestname-string-message-string--function--any-test-function-schema"},"children":[{"type":"text","value":"test"}]},{"type":"text","value":" function to add a custom rule."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In this section, you'll add a rule to make sure that the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"name"}]},{"type":"text","value":" field has both first and last name."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Change the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"name"}]},{"type":"text","value":" property inside the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"validationSchema"}]},{"type":"text","value":" to the following:"}]},{"type":"element","tagName":"div","properties":{"className":["kg-card","kg-code-card","gatsby-highlight"],"dataLanguage":"js"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-js"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"const"}]},{"type":"text","value":" formik "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"useFormik"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"..."}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"validationSchema"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Yup"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"object"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n      "},{"type":"element","tagName":"span","properties":{"className":["token","literal-property","property"]},"children":[{"type":"text","value":"name"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Yup"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"string"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n              "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"label"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'Full Name'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n              "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"required"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n              "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"test"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'is-full-name'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'Please enter both your first and last name'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"function"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","parameter"]},"children":[{"type":"text","value":"value"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n                "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"const"}]},{"type":"text","value":" nameArr "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" value"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"split"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\" \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n                "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" nameArr"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"length "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"\n              "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n      "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"..."}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n  "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The first parameter is the name of the custom rule. The second parameter is the message to show in case the field is invalid. "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The third parameter is the function that determines whether the field is valid or not. It should return a boolean value. If the value is true, then the field is valid. Otherwise, it's invalid."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You validate the name field to contain both first and last names by just splitting it  on the space delimiter which will return an array. You then check the array length. If it's at least 2, then the field is valid. Otherwise it's invalid."}]},{"type":"element","tagName":"h3","properties":{"id":"test-it-out-2"},"children":[{"type":"text","value":"Test it Out"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Run the server again now and go to the website. If you enter one word in the Full Name field you'll see an error."}]},{"type":"element","tagName":"figure","properties":{"className":["kg-card","kg-image-card"]},"children":[{"type":"element","tagName":"img","properties":{"src":"https://backend.shahednasser.com/content/images/2022/03/us9mldxrs5dvf174r4w9.png.jpeg","className":["kg-image"],"alt":"","loading":"lazy","width":880,"height":192},"children":[]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You'll need to enter at least two words for the field to be valid."}]},{"type":"element","tagName":"h2","properties":{"id":"conclusion"},"children":[{"type":"text","value":"Conclusion"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In this tutorial you learned how to use Formik and Yup in React. You can use these two libraries to create forms, validate them, and handle their submission. Using these two libraries makes creating forms in React easier and less stressful."}]}],"data":{"quirksMode":false}},"tableOfContents":[{"id":"project-setup","heading":"Project Setup"},{"id":"create-the-form-with-formik","heading":"Create the Form with Formik","items":[{"id":"test-it-out","heading":"Test it Out"}]},{"id":"add-validation-with-yup","heading":"Add Validation with Yup","items":[{"id":"test-it-out-1","heading":"Test it Out"},{"id":"custom-validation-rules","heading":"Custom Validation Rules"},{"id":"test-it-out-2","heading":"Test it Out"}]},{"id":"conclusion","heading":"Conclusion"}]},"featureImageSharp":{"base":"How-to-Create-and-Validate-Forms-in-React-using-Formik-and-Yup.webp","publicURL":"/static/91089d8a50da0ecc97647ef3cac82ba2/How-to-Create-and-Validate-Forms-in-React-using-Formik-and-Yup.webp","imageMeta":{"width":1000,"height":420},"childImageSharp":{"fluid":{"base64":"data:image/webp;base64,UklGRkgAAABXRUJQVlA4IDwAAABQAwCdASoUAAgAPtFWpEwoJKOiMAgBABoJaQAAW9PXmwi7AAD+8jvUupnDXapmZQhCREWIkppAgAX8AAA=","aspectRatio":2.364864864864865,"src":"/static/91089d8a50da0ecc97647ef3cac82ba2/89afa/How-to-Create-and-Validate-Forms-in-React-using-Formik-and-Yup.webp","srcSet":"/static/91089d8a50da0ecc97647ef3cac82ba2/9fca7/How-to-Create-and-Validate-Forms-in-React-using-Formik-and-Yup.webp 175w,\n/static/91089d8a50da0ecc97647ef3cac82ba2/37a4e/How-to-Create-and-Validate-Forms-in-React-using-Formik-and-Yup.webp 350w,\n/static/91089d8a50da0ecc97647ef3cac82ba2/89afa/How-to-Create-and-Validate-Forms-in-React-using-Formik-and-Yup.webp 700w,\n/static/91089d8a50da0ecc97647ef3cac82ba2/47f67/How-to-Create-and-Validate-Forms-in-React-using-Formik-and-Yup.webp 1000w","srcWebp":"/static/91089d8a50da0ecc97647ef3cac82ba2/89afa/How-to-Create-and-Validate-Forms-in-React-using-Formik-and-Yup.webp","srcSetWebp":"/static/91089d8a50da0ecc97647ef3cac82ba2/9fca7/How-to-Create-and-Validate-Forms-in-React-using-Formik-and-Yup.webp 175w,\n/static/91089d8a50da0ecc97647ef3cac82ba2/37a4e/How-to-Create-and-Validate-Forms-in-React-using-Formik-and-Yup.webp 350w,\n/static/91089d8a50da0ecc97647ef3cac82ba2/89afa/How-to-Create-and-Validate-Forms-in-React-using-Formik-and-Yup.webp 700w,\n/static/91089d8a50da0ecc97647ef3cac82ba2/47f67/How-to-Create-and-Validate-Forms-in-React-using-Formik-and-Yup.webp 1000w","sizes":"(max-width: 700px) 100vw, 700px"}}}}}]}},"pageContext":{"slug":"10-reasons-why-you-should-use-react-for-building-interactive-user-interfaces","prev":"create-laravel-blog-with-strapi-v4","next":"react-native-navigation-tutorial","tag":"react","limit":3,"skip":0,"primaryTagCount":15,"collectionPaths":{}}},"staticQueryHashes":["1272700106","1676991999","2138873178","2546165603","2681841279","2938721187","293880488","3052966952","4156497161"]}