{"id":55,"date":"2024-12-18T08:50:48","date_gmt":"2024-12-18T08:50:48","guid":{"rendered":"https:\/\/fullstacknotes.netblocs.com\/?p=55"},"modified":"2024-12-18T10:07:53","modified_gmt":"2024-12-18T10:07:53","slug":"managing-data-flow-and-state-management-in-react-when-to-use-what","status":"publish","type":"post","link":"https:\/\/netblocs.com\/fullstacknotes\/managing-data-flow-and-state-management-in-react-when-to-use-what\/","title":{"rendered":"Data Flow Management in React: When to Use What"},"content":{"rendered":"\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"767\" src=\"https:\/\/fullstacknotes.netblocs.com\/wp-content\/uploads\/2024\/12\/image-1024x767.png\" alt=\"\" class=\"wp-image-56\" srcset=\"https:\/\/netblocs.com\/fullstacknotes\/wp-content\/uploads\/2024\/12\/image-1024x767.png 1024w, https:\/\/netblocs.com\/fullstacknotes\/wp-content\/uploads\/2024\/12\/image-300x225.png 300w, https:\/\/netblocs.com\/fullstacknotes\/wp-content\/uploads\/2024\/12\/image-768x575.png 768w, https:\/\/netblocs.com\/fullstacknotes\/wp-content\/uploads\/2024\/12\/image.png 1291w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Managing data flow and state management in React can be challenging, especially as your application grows in complexity. Choosing the right method for data management based on the needs of your application is essential. Here\u2019s a concise guide to help you understand when to use each approach, starting from the simplest to the most complex.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. Local State Management with&nbsp;<code>useState<\/code><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">When to Use:<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>When you need to manage\u00a0<strong>small, isolated pieces of state<\/strong>.<\/li>\n\n\n\n<li>For data that will be used\u00a0<strong>only within a single component<\/strong>.<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">How It Works:<\/h4>\n\n\n\n<p><code>useState<\/code>&nbsp;provides local state that is only valid within that component. If the data stays within one component and isn\u2019t shared with others,&nbsp;<code>useState<\/code>&nbsp;is sufficient.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:1rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.5rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#232136\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"function ExampleComponent() {\n  const [data, setData] = useState(&quot;Initial Data&quot;);\n  return &lt;p&gt;{data}&lt;\/p&gt;;\n}\n\" style=\"color:#e0def4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki rose-pine-moon\" style=\"background-color: #232136\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #3E8FB0\">function<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #EA9A97\">ExampleComponent<\/span><span style=\"color: #908CAA\">()<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #908CAA\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">  <\/span><span style=\"color: #3E8FB0\">const<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #908CAA\">[<\/span><span style=\"color: #E0DEF4; font-style: italic\">data<\/span><span style=\"color: #908CAA\">,<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #E0DEF4; font-style: italic\">setData<\/span><span style=\"color: #908CAA\">]<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #3E8FB0\">=<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #EA9A97\">useState<\/span><span style=\"color: #E0DEF4\">(<\/span><span style=\"color: #F6C177\">&quot;Initial Data&quot;<\/span><span style=\"color: #E0DEF4\">)<\/span><span style=\"color: #908CAA\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">  <\/span><span style=\"color: #3E8FB0\">return<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #6E6A86\">&lt;<\/span><span style=\"color: #9CCFD8\">p<\/span><span style=\"color: #6E6A86\">&gt;<\/span><span style=\"color: #908CAA\">{<\/span><span style=\"color: #E0DEF4; font-style: italic\">data<\/span><span style=\"color: #908CAA\">}<\/span><span style=\"color: #6E6A86\">&lt;\/<\/span><span style=\"color: #9CCFD8\">p<\/span><span style=\"color: #6E6A86\">&gt;<\/span><span style=\"color: #908CAA\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #908CAA\">}<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">2. Data Transfer via Props<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">When to Use:<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>When passing data from a\u00a0<strong>parent component<\/strong>\u00a0to\u00a0<strong>child components<\/strong>.<\/li>\n\n\n\n<li>For simple hierarchical data flows.<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">How It Works:<\/h4>\n\n\n\n<p>State from a parent component can be passed down to child components via props. Data flows from the top down (parent to child).<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:1rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.5rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#232136\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"function ParentComponent() {\n  const [data, setData] = useState(&quot;Initial Data&quot;);\n  return &lt;ChildComponent data={data} \/&gt;;\n}\n\nfunction ChildComponent({ data }) {\n  return &lt;p&gt;{data}&lt;\/p&gt;;\n}\n\" style=\"color:#e0def4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki rose-pine-moon\" style=\"background-color: #232136\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #3E8FB0\">function<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #EA9A97\">ParentComponent<\/span><span style=\"color: #908CAA\">()<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #908CAA\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">  <\/span><span style=\"color: #3E8FB0\">const<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #908CAA\">[<\/span><span style=\"color: #E0DEF4; font-style: italic\">data<\/span><span style=\"color: #908CAA\">,<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #E0DEF4; font-style: italic\">setData<\/span><span style=\"color: #908CAA\">]<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #3E8FB0\">=<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #EA9A97\">useState<\/span><span style=\"color: #E0DEF4\">(<\/span><span style=\"color: #F6C177\">&quot;Initial Data&quot;<\/span><span style=\"color: #E0DEF4\">)<\/span><span style=\"color: #908CAA\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">  <\/span><span style=\"color: #3E8FB0\">return<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #6E6A86\">&lt;<\/span><span style=\"color: #9CCFD8\">ChildComponent<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #C4A7E7; font-style: italic\">data<\/span><span style=\"color: #3E8FB0\">=<\/span><span style=\"color: #908CAA\">{<\/span><span style=\"color: #E0DEF4; font-style: italic\">data<\/span><span style=\"color: #908CAA\">}<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #6E6A86\">\/&gt;<\/span><span style=\"color: #908CAA\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #908CAA\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #3E8FB0\">function<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #EA9A97\">ChildComponent<\/span><span style=\"color: #908CAA\">({<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #C4A7E7; font-style: italic\">data<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #908CAA\">})<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #908CAA\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">  <\/span><span style=\"color: #3E8FB0\">return<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #6E6A86\">&lt;<\/span><span style=\"color: #9CCFD8\">p<\/span><span style=\"color: #6E6A86\">&gt;<\/span><span style=\"color: #908CAA\">{<\/span><span style=\"color: #E0DEF4; font-style: italic\">data<\/span><span style=\"color: #908CAA\">}<\/span><span style=\"color: #6E6A86\">&lt;\/<\/span><span style=\"color: #9CCFD8\">p<\/span><span style=\"color: #6E6A86\">&gt;<\/span><span style=\"color: #908CAA\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #908CAA\">}<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">3. Callback Functions for Upward Data Transfer<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">When to Use:<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>When a\u00a0<strong>child component<\/strong>\u00a0needs to send data back to its\u00a0<strong>parent component<\/strong>.<\/li>\n\n\n\n<li>For upward data flow in a hierarchical structure.<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">How It Works:<\/h4>\n\n\n\n<p>The parent component passes a callback function as a prop to the child component. The child can call this function to send data back up.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:1rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.5rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#232136\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"function ParentComponent() {\n  const [data, setData] = useState(&quot;Initial Data&quot;);\n  return &lt;ChildComponent setData={setData} \/&gt;;\n}\n\nfunction ChildComponent({ setData }) {\n  return &lt;button onClick={() =&gt; setData(&quot;New Data&quot;)}&gt;Update Data&lt;\/button&gt;;\n}\n\" style=\"color:#e0def4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki rose-pine-moon\" style=\"background-color: #232136\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #3E8FB0\">function<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #EA9A97\">ParentComponent<\/span><span style=\"color: #908CAA\">()<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #908CAA\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">  <\/span><span style=\"color: #3E8FB0\">const<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #908CAA\">[<\/span><span style=\"color: #E0DEF4; font-style: italic\">data<\/span><span style=\"color: #908CAA\">,<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #E0DEF4; font-style: italic\">setData<\/span><span style=\"color: #908CAA\">]<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #3E8FB0\">=<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #EA9A97\">useState<\/span><span style=\"color: #E0DEF4\">(<\/span><span style=\"color: #F6C177\">&quot;Initial Data&quot;<\/span><span style=\"color: #E0DEF4\">)<\/span><span style=\"color: #908CAA\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">  <\/span><span style=\"color: #3E8FB0\">return<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #6E6A86\">&lt;<\/span><span style=\"color: #9CCFD8\">ChildComponent<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #C4A7E7; font-style: italic\">setData<\/span><span style=\"color: #3E8FB0\">=<\/span><span style=\"color: #908CAA\">{<\/span><span style=\"color: #E0DEF4; font-style: italic\">setData<\/span><span style=\"color: #908CAA\">}<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #6E6A86\">\/&gt;<\/span><span style=\"color: #908CAA\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #908CAA\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #3E8FB0\">function<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #EA9A97\">ChildComponent<\/span><span style=\"color: #908CAA\">({<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #C4A7E7; font-style: italic\">setData<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #908CAA\">})<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #908CAA\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">  <\/span><span style=\"color: #3E8FB0\">return<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #6E6A86\">&lt;<\/span><span style=\"color: #9CCFD8\">button<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #C4A7E7; font-style: italic\">onClick<\/span><span style=\"color: #3E8FB0\">=<\/span><span style=\"color: #908CAA\">{()<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #3E8FB0\">=&gt;<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #EA9A97\">setData<\/span><span style=\"color: #E0DEF4\">(<\/span><span style=\"color: #F6C177\">&quot;New Data&quot;<\/span><span style=\"color: #E0DEF4\">)<\/span><span style=\"color: #908CAA\">}<\/span><span style=\"color: #6E6A86\">&gt;<\/span><span style=\"color: #E0DEF4\">Update Data<\/span><span style=\"color: #6E6A86\">&lt;\/<\/span><span style=\"color: #9CCFD8\">button<\/span><span style=\"color: #6E6A86\">&gt;<\/span><span style=\"color: #908CAA\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #908CAA\">}<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">4. Using Context API for Global State Management<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">When to Use:<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>When you need to manage\u00a0<strong>widely shared state<\/strong>\u00a0across multiple components.<\/li>\n\n\n\n<li>To avoid the problem of props drilling (constantly passing props down through components).<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">How It Works:<\/h4>\n\n\n\n<p>The Context API provides a way to share values globally, allowing any component to access state directly without prop drilling.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:1rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.5rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#232136\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"\/\/ DataContext.js\nimport React, { createContext, useState } from 'react';\n\nconst DataContext = createContext();\n\nexport function DataProvider({ children }) {\n  const [data, setData] = useState(&quot;Initial Data&quot;);\n  return (\n    &lt;DataContext.Provider value={{ data, setData }}&gt;\n      {children}\n    &lt;\/DataContext.Provider&gt;\n  );\n}\n\n\/\/ ParentComponent.jsx\nimport { DataProvider } from '.\/DataContext';\nimport ChildComponent from '.\/ChildComponent';\n\nfunction ParentComponent() {\n  return (\n    &lt;DataProvider&gt;\n      &lt;ChildComponent \/&gt;\n    &lt;\/DataProvider&gt;\n  );\n}\n\n\/\/ ChildComponent.jsx\nimport { useContext } from 'react';\nimport DataContext from '.\/DataContext';\n\nfunction ChildComponent() {\n  const { data, setData } = useContext(DataContext);\n  return (\n    &lt;div&gt;\n      &lt;p&gt;{data}&lt;\/p&gt;\n      &lt;button onClick={() =&gt; setData(&quot;New Data&quot;)}&gt;Update Data&lt;\/button&gt;\n    &lt;\/div&gt;\n  );\n}\n\" style=\"color:#e0def4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki rose-pine-moon\" style=\"background-color: #232136\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #908CAA; font-style: italic\">\/\/<\/span><span style=\"color: #6E6A86; font-style: italic\"> DataContext.js<\/span><\/span>\n<span class=\"line\"><span style=\"color: #3E8FB0\">import <\/span><span style=\"color: #E0DEF4; font-style: italic\">React<\/span><span style=\"color: #908CAA\">,<\/span><span style=\"color: #3E8FB0\"> <\/span><span style=\"color: #908CAA\">{<\/span><span style=\"color: #3E8FB0\"> <\/span><span style=\"color: #E0DEF4; font-style: italic\">createContext<\/span><span style=\"color: #908CAA\">,<\/span><span style=\"color: #3E8FB0\"> <\/span><span style=\"color: #E0DEF4; font-style: italic\">useState<\/span><span style=\"color: #3E8FB0\"> <\/span><span style=\"color: #908CAA\">}<\/span><span style=\"color: #3E8FB0\"> from <\/span><span style=\"color: #F6C177\">&#39;react&#39;<\/span><span style=\"color: #908CAA\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #3E8FB0\">const<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #E0DEF4; font-style: italic\">DataContext<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #3E8FB0\">=<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #EA9A97\">createContext<\/span><span style=\"color: #E0DEF4\">()<\/span><span style=\"color: #908CAA\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #3E8FB0\">export<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #3E8FB0\">function<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #EA9A97\">DataProvider<\/span><span style=\"color: #908CAA\">({<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #C4A7E7; font-style: italic\">children<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #908CAA\">})<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #908CAA\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">  <\/span><span style=\"color: #3E8FB0\">const<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #908CAA\">[<\/span><span style=\"color: #E0DEF4; font-style: italic\">data<\/span><span style=\"color: #908CAA\">,<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #E0DEF4; font-style: italic\">setData<\/span><span style=\"color: #908CAA\">]<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #3E8FB0\">=<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #EA9A97\">useState<\/span><span style=\"color: #E0DEF4\">(<\/span><span style=\"color: #F6C177\">&quot;Initial Data&quot;<\/span><span style=\"color: #E0DEF4\">)<\/span><span style=\"color: #908CAA\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">  <\/span><span style=\"color: #3E8FB0\">return<\/span><span style=\"color: #E0DEF4\"> (<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">    <\/span><span style=\"color: #6E6A86\">&lt;<\/span><span style=\"color: #9CCFD8\">DataContext.Provider<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #C4A7E7; font-style: italic\">value<\/span><span style=\"color: #3E8FB0\">=<\/span><span style=\"color: #908CAA\">{{<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #E0DEF4; font-style: italic\">data<\/span><span style=\"color: #908CAA\">,<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #E0DEF4; font-style: italic\">setData<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #908CAA\">}}<\/span><span style=\"color: #6E6A86\">&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">      <\/span><span style=\"color: #908CAA\">{<\/span><span style=\"color: #E0DEF4; font-style: italic\">children<\/span><span style=\"color: #908CAA\">}<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">    <\/span><span style=\"color: #6E6A86\">&lt;\/<\/span><span style=\"color: #9CCFD8\">DataContext.Provider<\/span><span style=\"color: #6E6A86\">&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">  )<\/span><span style=\"color: #908CAA\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #908CAA\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #908CAA; font-style: italic\">\/\/<\/span><span style=\"color: #6E6A86; font-style: italic\"> ParentComponent.jsx<\/span><\/span>\n<span class=\"line\"><span style=\"color: #3E8FB0\">import <\/span><span style=\"color: #908CAA\">{<\/span><span style=\"color: #3E8FB0\"> <\/span><span style=\"color: #E0DEF4; font-style: italic\">DataProvider<\/span><span style=\"color: #3E8FB0\"> <\/span><span style=\"color: #908CAA\">}<\/span><span style=\"color: #3E8FB0\"> from <\/span><span style=\"color: #F6C177\">&#39;.\/DataContext&#39;<\/span><span style=\"color: #908CAA\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #3E8FB0\">import <\/span><span style=\"color: #E0DEF4; font-style: italic\">ChildComponent<\/span><span style=\"color: #3E8FB0\"> from <\/span><span style=\"color: #F6C177\">&#39;.\/ChildComponent&#39;<\/span><span style=\"color: #908CAA\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #3E8FB0\">function<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #EA9A97\">ParentComponent<\/span><span style=\"color: #908CAA\">()<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #908CAA\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">  <\/span><span style=\"color: #3E8FB0\">return<\/span><span style=\"color: #E0DEF4\"> (<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">    <\/span><span style=\"color: #6E6A86\">&lt;<\/span><span style=\"color: #9CCFD8\">DataProvider<\/span><span style=\"color: #6E6A86\">&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">      <\/span><span style=\"color: #6E6A86\">&lt;<\/span><span style=\"color: #9CCFD8\">ChildComponent<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #6E6A86\">\/&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">    <\/span><span style=\"color: #6E6A86\">&lt;\/<\/span><span style=\"color: #9CCFD8\">DataProvider<\/span><span style=\"color: #6E6A86\">&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">  )<\/span><span style=\"color: #908CAA\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #908CAA\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #908CAA; font-style: italic\">\/\/<\/span><span style=\"color: #6E6A86; font-style: italic\"> ChildComponent.jsx<\/span><\/span>\n<span class=\"line\"><span style=\"color: #3E8FB0\">import <\/span><span style=\"color: #908CAA\">{<\/span><span style=\"color: #3E8FB0\"> <\/span><span style=\"color: #E0DEF4; font-style: italic\">useContext<\/span><span style=\"color: #3E8FB0\"> <\/span><span style=\"color: #908CAA\">}<\/span><span style=\"color: #3E8FB0\"> from <\/span><span style=\"color: #F6C177\">&#39;react&#39;<\/span><span style=\"color: #908CAA\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #3E8FB0\">import <\/span><span style=\"color: #E0DEF4; font-style: italic\">DataContext<\/span><span style=\"color: #3E8FB0\"> from <\/span><span style=\"color: #F6C177\">&#39;.\/DataContext&#39;<\/span><span style=\"color: #908CAA\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #3E8FB0\">function<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #EA9A97\">ChildComponent<\/span><span style=\"color: #908CAA\">()<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #908CAA\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">  <\/span><span style=\"color: #3E8FB0\">const<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #908CAA\">{<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #E0DEF4; font-style: italic\">data<\/span><span style=\"color: #908CAA\">,<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #E0DEF4; font-style: italic\">setData<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #908CAA\">}<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #3E8FB0\">=<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #EA9A97\">useContext<\/span><span style=\"color: #E0DEF4\">(<\/span><span style=\"color: #E0DEF4; font-style: italic\">DataContext<\/span><span style=\"color: #E0DEF4\">)<\/span><span style=\"color: #908CAA\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">  <\/span><span style=\"color: #3E8FB0\">return<\/span><span style=\"color: #E0DEF4\"> (<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">    <\/span><span style=\"color: #6E6A86\">&lt;<\/span><span style=\"color: #9CCFD8\">div<\/span><span style=\"color: #6E6A86\">&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">      <\/span><span style=\"color: #6E6A86\">&lt;<\/span><span style=\"color: #9CCFD8\">p<\/span><span style=\"color: #6E6A86\">&gt;<\/span><span style=\"color: #908CAA\">{<\/span><span style=\"color: #E0DEF4; font-style: italic\">data<\/span><span style=\"color: #908CAA\">}<\/span><span style=\"color: #6E6A86\">&lt;\/<\/span><span style=\"color: #9CCFD8\">p<\/span><span style=\"color: #6E6A86\">&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">      <\/span><span style=\"color: #6E6A86\">&lt;<\/span><span style=\"color: #9CCFD8\">button<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #C4A7E7; font-style: italic\">onClick<\/span><span style=\"color: #3E8FB0\">=<\/span><span style=\"color: #908CAA\">{()<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #3E8FB0\">=&gt;<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #EA9A97\">setData<\/span><span style=\"color: #E0DEF4\">(<\/span><span style=\"color: #F6C177\">&quot;New Data&quot;<\/span><span style=\"color: #E0DEF4\">)<\/span><span style=\"color: #908CAA\">}<\/span><span style=\"color: #6E6A86\">&gt;<\/span><span style=\"color: #E0DEF4\">Update Data<\/span><span style=\"color: #6E6A86\">&lt;\/<\/span><span style=\"color: #9CCFD8\">button<\/span><span style=\"color: #6E6A86\">&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">    <\/span><span style=\"color: #6E6A86\">&lt;\/<\/span><span style=\"color: #9CCFD8\">div<\/span><span style=\"color: #6E6A86\">&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">  )<\/span><span style=\"color: #908CAA\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #908CAA\">}<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">5. Centralized State Management with Redux<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">When to Use:<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>When building\u00a0<strong>large and complex applications<\/strong>\u00a0that require global state management.<\/li>\n\n\n\n<li>When state consistency is crucial across multiple components.<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">How It Works:<\/h4>\n\n\n\n<p>Redux uses a central store to manage the global state. All components can access this store to get data and dispatch actions to update it.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:1rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.5rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#232136\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"\/\/ store.js\nimport { createStore } from 'redux';\n\nconst initialState = { data: &quot;Initial Data&quot; };\n\nfunction reducer(state = initialState, action) {\n  switch (action.type) {\n    case 'UPDATE_DATA':\n      return { ...state, data: action.payload };\n    default:\n      return state;\n  }\n}\n\nconst store = createStore(reducer);\nexport default store;\n\" style=\"color:#e0def4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki rose-pine-moon\" style=\"background-color: #232136\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #908CAA; font-style: italic\">\/\/<\/span><span style=\"color: #6E6A86; font-style: italic\"> store.js<\/span><\/span>\n<span class=\"line\"><span style=\"color: #3E8FB0\">import <\/span><span style=\"color: #908CAA\">{<\/span><span style=\"color: #3E8FB0\"> <\/span><span style=\"color: #E0DEF4; font-style: italic\">createStore<\/span><span style=\"color: #3E8FB0\"> <\/span><span style=\"color: #908CAA\">}<\/span><span style=\"color: #3E8FB0\"> from <\/span><span style=\"color: #F6C177\">&#39;redux&#39;<\/span><span style=\"color: #908CAA\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #3E8FB0\">const<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #E0DEF4; font-style: italic\">initialState<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #3E8FB0\">=<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #908CAA\">{<\/span><span style=\"color: #E0DEF4\"> data<\/span><span style=\"color: #908CAA\">:<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #F6C177\">&quot;Initial Data&quot;<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #908CAA\">};<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #3E8FB0\">function<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #EA9A97\">reducer<\/span><span style=\"color: #908CAA\">(<\/span><span style=\"color: #C4A7E7; font-style: italic\">state<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #3E8FB0\">=<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #E0DEF4; font-style: italic\">initialState<\/span><span style=\"color: #908CAA\">,<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #C4A7E7; font-style: italic\">action<\/span><span style=\"color: #908CAA\">)<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #908CAA\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">  <\/span><span style=\"color: #3E8FB0\">switch<\/span><span style=\"color: #E0DEF4\"> (<\/span><span style=\"color: #E0DEF4; font-style: italic\">action<\/span><span style=\"color: #3E8FB0\">.<\/span><span style=\"color: #E0DEF4; font-style: italic\">type<\/span><span style=\"color: #E0DEF4\">) <\/span><span style=\"color: #908CAA\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">    <\/span><span style=\"color: #3E8FB0\">case<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #F6C177\">&#39;UPDATE_DATA&#39;<\/span><span style=\"color: #908CAA\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">      <\/span><span style=\"color: #3E8FB0\">return<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #908CAA\">{<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #3E8FB0\">...<\/span><span style=\"color: #E0DEF4; font-style: italic\">state<\/span><span style=\"color: #908CAA\">,<\/span><span style=\"color: #E0DEF4\"> data<\/span><span style=\"color: #908CAA\">:<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #E0DEF4; font-style: italic\">action<\/span><span style=\"color: #3E8FB0\">.<\/span><span style=\"color: #E0DEF4; font-style: italic\">payload<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #908CAA\">};<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">    <\/span><span style=\"color: #3E8FB0\">default<\/span><span style=\"color: #908CAA\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">      <\/span><span style=\"color: #3E8FB0\">return<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #E0DEF4; font-style: italic\">state<\/span><span style=\"color: #908CAA\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">  <\/span><span style=\"color: #908CAA\">}<\/span><\/span>\n<span class=\"line\"><span style=\"color: #908CAA\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #3E8FB0\">const<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #E0DEF4; font-style: italic\">store<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #3E8FB0\">=<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #EA9A97\">createStore<\/span><span style=\"color: #E0DEF4\">(<\/span><span style=\"color: #E0DEF4; font-style: italic\">reducer<\/span><span style=\"color: #E0DEF4\">)<\/span><span style=\"color: #908CAA\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #3E8FB0\">export default <\/span><span style=\"color: #E0DEF4; font-style: italic\">store<\/span><span style=\"color: #908CAA\">;<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:1rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.5rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#232136\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"\/\/ index.js\nimport { Provider } from 'react-redux';\nimport store from '.\/store';\nimport App from '.\/App';\n\nReactDOM.render(\n  &lt;Provider store={store}&gt;\n    &lt;App \/&gt;\n  &lt;\/Provider&gt;,\n  document.getElementById('root')\n);\n\" style=\"color:#e0def4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki rose-pine-moon\" style=\"background-color: #232136\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #908CAA; font-style: italic\">\/\/<\/span><span style=\"color: #6E6A86; font-style: italic\"> index.js<\/span><\/span>\n<span class=\"line\"><span style=\"color: #3E8FB0\">import <\/span><span style=\"color: #908CAA\">{<\/span><span style=\"color: #3E8FB0\"> <\/span><span style=\"color: #E0DEF4; font-style: italic\">Provider<\/span><span style=\"color: #3E8FB0\"> <\/span><span style=\"color: #908CAA\">}<\/span><span style=\"color: #3E8FB0\"> from <\/span><span style=\"color: #F6C177\">&#39;react-redux&#39;<\/span><span style=\"color: #908CAA\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #3E8FB0\">import <\/span><span style=\"color: #E0DEF4; font-style: italic\">store<\/span><span style=\"color: #3E8FB0\"> from <\/span><span style=\"color: #F6C177\">&#39;.\/store&#39;<\/span><span style=\"color: #908CAA\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #3E8FB0\">import <\/span><span style=\"color: #E0DEF4; font-style: italic\">App<\/span><span style=\"color: #3E8FB0\"> from <\/span><span style=\"color: #F6C177\">&#39;.\/App&#39;<\/span><span style=\"color: #908CAA\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4; font-style: italic\">ReactDOM<\/span><span style=\"color: #3E8FB0\">.<\/span><span style=\"color: #EA9A97\">render<\/span><span style=\"color: #E0DEF4\">(<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">  <\/span><span style=\"color: #6E6A86\">&lt;<\/span><span style=\"color: #9CCFD8\">Provider<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #C4A7E7; font-style: italic\">store<\/span><span style=\"color: #3E8FB0\">=<\/span><span style=\"color: #908CAA\">{<\/span><span style=\"color: #E0DEF4; font-style: italic\">store<\/span><span style=\"color: #908CAA\">}<\/span><span style=\"color: #6E6A86\">&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">    <\/span><span style=\"color: #6E6A86\">&lt;<\/span><span style=\"color: #9CCFD8\">App<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #6E6A86\">\/&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">  <\/span><span style=\"color: #6E6A86\">&lt;\/<\/span><span style=\"color: #9CCFD8\">Provider<\/span><span style=\"color: #6E6A86\">&gt;<\/span><span style=\"color: #908CAA\">,<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">  <\/span><span style=\"color: #E0DEF4; font-style: italic\">document<\/span><span style=\"color: #3E8FB0\">.<\/span><span style=\"color: #EA9A97\">getElementById<\/span><span style=\"color: #E0DEF4\">(<\/span><span style=\"color: #F6C177\">&#39;root&#39;<\/span><span style=\"color: #E0DEF4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">)<\/span><span style=\"color: #908CAA\">;<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:1rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.5rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#232136\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"\/\/ ChildComponentA.jsx\nimport { useSelector } from 'react-redux';\n\nfunction ChildComponentA() {\n  const data = useSelector(state =&gt; state.data);\n  return &lt;p&gt;{data}&lt;\/p&gt;;\n}\n\n\/\/ ChildComponentB.jsx\nimport { useDispatch } from 'react-redux';\n\nfunction ChildComponentB() {\n  const dispatch = useDispatch();\n  return (\n    &lt;button onClick={() =&gt; dispatch({ type: 'UPDATE_DATA', payload: 'New Data' })}&gt;\n      Update Data\n    &lt;\/button&gt;\n  );\n}\n\" style=\"color:#e0def4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki rose-pine-moon\" style=\"background-color: #232136\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #908CAA; font-style: italic\">\/\/<\/span><span style=\"color: #6E6A86; font-style: italic\"> ChildComponentA.jsx<\/span><\/span>\n<span class=\"line\"><span style=\"color: #3E8FB0\">import <\/span><span style=\"color: #908CAA\">{<\/span><span style=\"color: #3E8FB0\"> <\/span><span style=\"color: #E0DEF4; font-style: italic\">useSelector<\/span><span style=\"color: #3E8FB0\"> <\/span><span style=\"color: #908CAA\">}<\/span><span style=\"color: #3E8FB0\"> from <\/span><span style=\"color: #F6C177\">&#39;react-redux&#39;<\/span><span style=\"color: #908CAA\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #3E8FB0\">function<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #EA9A97\">ChildComponentA<\/span><span style=\"color: #908CAA\">()<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #908CAA\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">  <\/span><span style=\"color: #3E8FB0\">const<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #E0DEF4; font-style: italic\">data<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #3E8FB0\">=<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #EA9A97\">useSelector<\/span><span style=\"color: #E0DEF4\">(<\/span><span style=\"color: #C4A7E7; font-style: italic\">state<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #3E8FB0\">=&gt;<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #E0DEF4; font-style: italic\">state<\/span><span style=\"color: #3E8FB0\">.<\/span><span style=\"color: #E0DEF4; font-style: italic\">data<\/span><span style=\"color: #E0DEF4\">)<\/span><span style=\"color: #908CAA\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">  <\/span><span style=\"color: #3E8FB0\">return<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #6E6A86\">&lt;<\/span><span style=\"color: #9CCFD8\">p<\/span><span style=\"color: #6E6A86\">&gt;<\/span><span style=\"color: #908CAA\">{<\/span><span style=\"color: #E0DEF4; font-style: italic\">data<\/span><span style=\"color: #908CAA\">}<\/span><span style=\"color: #6E6A86\">&lt;\/<\/span><span style=\"color: #9CCFD8\">p<\/span><span style=\"color: #6E6A86\">&gt;<\/span><span style=\"color: #908CAA\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #908CAA\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #908CAA; font-style: italic\">\/\/<\/span><span style=\"color: #6E6A86; font-style: italic\"> ChildComponentB.jsx<\/span><\/span>\n<span class=\"line\"><span style=\"color: #3E8FB0\">import <\/span><span style=\"color: #908CAA\">{<\/span><span style=\"color: #3E8FB0\"> <\/span><span style=\"color: #E0DEF4; font-style: italic\">useDispatch<\/span><span style=\"color: #3E8FB0\"> <\/span><span style=\"color: #908CAA\">}<\/span><span style=\"color: #3E8FB0\"> from <\/span><span style=\"color: #F6C177\">&#39;react-redux&#39;<\/span><span style=\"color: #908CAA\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #3E8FB0\">function<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #EA9A97\">ChildComponentB<\/span><span style=\"color: #908CAA\">()<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #908CAA\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">  <\/span><span style=\"color: #3E8FB0\">const<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #E0DEF4; font-style: italic\">dispatch<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #3E8FB0\">=<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #EA9A97\">useDispatch<\/span><span style=\"color: #E0DEF4\">()<\/span><span style=\"color: #908CAA\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">  <\/span><span style=\"color: #3E8FB0\">return<\/span><span style=\"color: #E0DEF4\"> (<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">    <\/span><span style=\"color: #6E6A86\">&lt;<\/span><span style=\"color: #9CCFD8\">button<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #C4A7E7; font-style: italic\">onClick<\/span><span style=\"color: #3E8FB0\">=<\/span><span style=\"color: #908CAA\">{()<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #3E8FB0\">=&gt;<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #EA9A97\">dispatch<\/span><span style=\"color: #E0DEF4\">(<\/span><span style=\"color: #908CAA\">{<\/span><span style=\"color: #E0DEF4\"> type<\/span><span style=\"color: #908CAA\">:<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #F6C177\">&#39;UPDATE_DATA&#39;<\/span><span style=\"color: #908CAA\">,<\/span><span style=\"color: #E0DEF4\"> payload<\/span><span style=\"color: #908CAA\">:<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #F6C177\">&#39;New Data&#39;<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #908CAA\">}<\/span><span style=\"color: #E0DEF4\">)<\/span><span style=\"color: #908CAA\">}<\/span><span style=\"color: #6E6A86\">&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">      Update Data<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">    <\/span><span style=\"color: #6E6A86\">&lt;\/<\/span><span style=\"color: #9CCFD8\">button<\/span><span style=\"color: #6E6A86\">&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">  )<\/span><span style=\"color: #908CAA\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #908CAA\">}<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Summary Table<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Method<\/th><th>When to Use<\/th><th>Advantages<\/th><th>Disadvantages<\/th><\/tr><\/thead><tbody><tr><td><strong>useState<\/strong><\/td><td>Simple state management in a single component<\/td><td>Easy and quick<\/td><td>Component-specific only<\/td><\/tr><tr><td><strong>Props<\/strong><\/td><td>Passing data from parent to child components<\/td><td>Simple and effective<\/td><td>Can lead to props drilling<\/td><\/tr><tr><td><strong>Callback Functions<\/strong><\/td><td>Sending data from child to parent<\/td><td>Allows upward data flow<\/td><td>Requires a hierarchical setup<\/td><\/tr><tr><td><strong>Context API<\/strong><\/td><td>Sharing state across multiple components<\/td><td>Global state management<\/td><td>Can be complex to manage<\/td><\/tr><tr><td><strong>Redux<\/strong><\/td><td>Managing state in large, complex applications<\/td><td>Centralized and scalable<\/td><td>Learning curve and complexity<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>This guide will help you understand how to optimize data flow and state management in React applications. By carefully selecting the appropriate solution based on your project size and requirements, you can make data management more efficient and effective.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Managing data flow and state management in React can be challenging, especially as your application grows in complexity. Choosing the right method for data management based on the needs of&hellip;<\/p>\n","protected":false},"author":1,"featured_media":57,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[36,37,33,35,14,22,34],"class_list":["post-55","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-react","tag-callback","tag-context","tag-data-flow","tag-props","tag-react","tag-redux","tag-state"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/netblocs.com\/fullstacknotes\/wp-json\/wp\/v2\/posts\/55","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/netblocs.com\/fullstacknotes\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/netblocs.com\/fullstacknotes\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/netblocs.com\/fullstacknotes\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/netblocs.com\/fullstacknotes\/wp-json\/wp\/v2\/comments?post=55"}],"version-history":[{"count":2,"href":"https:\/\/netblocs.com\/fullstacknotes\/wp-json\/wp\/v2\/posts\/55\/revisions"}],"predecessor-version":[{"id":114,"href":"https:\/\/netblocs.com\/fullstacknotes\/wp-json\/wp\/v2\/posts\/55\/revisions\/114"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/netblocs.com\/fullstacknotes\/wp-json\/wp\/v2\/media\/57"}],"wp:attachment":[{"href":"https:\/\/netblocs.com\/fullstacknotes\/wp-json\/wp\/v2\/media?parent=55"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/netblocs.com\/fullstacknotes\/wp-json\/wp\/v2\/categories?post=55"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/netblocs.com\/fullstacknotes\/wp-json\/wp\/v2\/tags?post=55"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}