{"id":20498,"date":"2022-10-07T03:57:52","date_gmt":"2022-10-07T10:57:52","guid":{"rendered":"https:\/\/coderpad.io\/?p=20498"},"modified":"2023-06-07T13:55:17","modified_gmt":"2023-06-07T20:55:17","slug":"global-state-management-react","status":"publish","type":"post","link":"https:\/\/coderpad.io\/blog\/development\/global-state-management-react\/","title":{"rendered":"An Introduction to Global State Management in React without a Library"},"content":{"rendered":"\n<p>Scalable React projects tend to contain code bases with numerous components making up the primary application. Good data management and communication between these individual components is pivotal for cleaner code, and improved data sharing between components.&nbsp;<\/p>\n\n\n\n<p>Global state management was created as a means to centralize and manage data in an application, making it easily mutable and available to all components in the application. Finding and implementing a suitable means to handle global state management is an integral part of an application.&nbsp;<\/p>\n\n\n\n<p>In this article, we will give you a deep dive on the terms state and state management in React, and discuss the best choices for managing global state in a React application. We will also cover different methods of managing global state in a React application without the use of any 3rd party library or packages.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is state and global state in React?<\/h2>\n\n\n\n<p>In JavaScript applications, state refers to all data the application needs in order to operate. State can be stored in any data type, including arrays, booleans, strings, or numbers.<\/p>\n\n\n\n<p>Within React apps, global state is a JavaScript object that stores data used by React to render components with dynamic content based on user action. Applications can include this global state for both functional and class components.<\/p>\n\n\n\n<p>State can be transferred across components in React apps in different ways depending on the state management technique used.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is state management?<\/h2>\n\n\n\n<p>State management is a method of controlling communication between different parts of state and their usage, including React components.<\/p>\n\n\n\n<p>Except for extremely simple applications, components need to communicate with each other. For example, in an e-commerce application, a cart component needs to communicate to the site header component to show how many items are currently in the cart.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Why state management is crucial&nbsp;<\/h3>\n\n\n\n<p>Components are used in the development of React apps, and these components often manage state on their own because a state object can be included directly in React components.&nbsp;<\/p>\n\n\n\n<p>When present, the component&#8217;s state is considered &#8220;encapsulated data&#8221; and contains the properties that are persistent across component renderings. This is effective for applications with few components, but as the application grows, it becomes difficult to control the complexity of shared state between components.<\/p>\n\n\n\n<p>When state management is implemented:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Data is consolidated, making it easier to reason with your app&#8217;s logic.<\/li>\n\n\n\n<li>The location of your data is always known.<\/li>\n\n\n\n<li>You can get a point-in-time snapshot of all the data stored globally.<\/li>\n\n\n\n<li>Your development will proceed more quickly as a result of this better developer experience.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Use cases where global state management plays a pivotal role<\/h3>\n\n\n\n<p>Global state management is useful in virtually every type of app, but some examples of where it may be particularly critical include:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Ecommerce applications<\/strong>: In a standard ecommerce application, there are usually many components that communicate with each other and multiple actions that users can take which causes the data to change. For example, a wish list for users, where items can be added and saved by a user, to be shopped on later in the future.&nbsp;<\/li>\n\n\n\n<li><strong>Educational applications<\/strong>: Global state management is useful in educational applications that feature a user management platform, where its visitors can log in, sign in, enroll in a course, etc. All these are made possible by state management.<\/li>\n\n\n\n<li><strong>Applications with authentication<\/strong>: Without state management, it would be very hard to build applications where users need to be authenticated. This is due to the fact that most user information would be required by multiple components in the application for security and in order to present the user with the corresponding data stored in the application database. This is made possible with the use of state management, which tells if the user is authenticated and provides information about the user.<\/li>\n\n\n\n<li><strong>Personalized applications &amp; websites<\/strong>: In order to provide users with a personalized experience, some user data has to be saved in the application. Without state management, this would be difficult. This is because state management takes in the user\u2019s preferences and actions, and uses this to determine what kind of content the user receives, ex: YouTube and Pinterest.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Methods of managing state in React<\/h2>\n\n\n\n<p>There are multiple ways we can manage global state in React applications. Let\u2019s take a look at the basic approaches to managing state:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>React Hooks (e.g. <code>useState<\/code> and <code>useReducer<\/code>)<\/li>\n\n\n\n<li>React&#8217;s Context API<\/li>\n\n\n\n<li>State Management Libraries (e.g. Redux and Recoil)<\/li>\n<\/ul>\n\n\n\n<p>Each state management technique is used to build React applications of different scales, ranging from small-scale to large-scale applications.&nbsp;<\/p>\n\n\n\n<p>For example, relying on React Hooks like <code>useState<\/code> and <code>useReducer<\/code> for global state, although<a href=\"https:\/\/reactjs.org\/docs\/hooks-faq.html#should-i-use-hooks-classes-or-a-mix-of-both\" target=\"_blank\" rel=\"noreferrer noopener\"> encouraged<\/a> by the React Docs, only really works well for small and simple React applications.&nbsp;<\/p>\n\n\n\n<p>As the number of features and the size of the application increases, we begin to increasingly rely on \u201c<a href=\"https:\/\/kentcdodds.com\/blog\/prop-drilling\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>prop drilling<\/strong><\/a>\u201d to pass state between components and it becomes hard to manage the application state with only hooks.<\/p>\n\n\n\n<p>This is where the <a href=\"https:\/\/reactjs.org\/docs\/context.html\" target=\"_blank\" rel=\"noreferrer noopener\">Context API<\/a> or state management libraries can come in.&nbsp;<\/p>\n\n\n\n<p>But we&#8217;re getting ahead of ourselves; let&#8217;s start by taking a look at React&#8217;s <code>useState<\/code> and <code>useReducer<\/code> hooks.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">React Hooks<\/h2>\n\n\n\n<p>According to the <a href=\"https:\/\/reactjs.org\/docs\/hooks-overview.html#but-what-is-a-hook\" target=\"_blank\" rel=\"noreferrer noopener\">React Docs<\/a>, \u201cHooks are functions that let you \u2018hook into\u2019 React state and lifecycle features from function components.\u201d<\/p>\n\n\n\n<p>The <code>useState<\/code> hook, for example, is useful for setting and updating a React component&#8217;s state and the <code>useReducer<\/code> hook lets you manage the local state of complex components with a reducer.<\/p>\n\n\n\n<p>Let\u2019s dive in and see how we can use these hooks to manage the application state in our React application.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">useState hook<\/h3>\n\n\n\n<p>With <code>useState<\/code> you can set a value of state within a component.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-keyword\">const<\/span> &#91;state, setState] = useState(initialState);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><code>useState<\/code> is a function that takes in the initial state (<code>initialState<\/code> in the code above) as an argument.&nbsp;<\/p>\n\n\n\n<p>React will preserve this state between re-renders. <code>useState<\/code> returns two things <em>that we can obtain by <\/em><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Operators\/Destructuring_assignment\" target=\"_blank\" rel=\"noreferrer noopener\"><em>destructuring<\/em><\/a>: the<strong> <em>current<\/em> state value<\/strong> and a<strong> function that lets you update it<\/strong>.&nbsp;<\/p>\n\n\n\n<p>You can call this function <code>setState<\/code>&nbsp; function in order to update the <code>state<\/code> to a new value.<\/p>\n\n\n\n<p>Consider this example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-keyword\">import<\/span> { useState } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">\"react\"<\/span>;\n<span class=\"hljs-keyword\">import<\/span> <span class=\"hljs-string\">\".\/styles.css\"<\/span>;\n\n\n<span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-keyword\">default<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">App<\/span>(<span class=\"hljs-params\"><\/span>) <\/span>{\n  <span class=\"hljs-keyword\">const<\/span> &#91;count, setCount] = useState(<span class=\"hljs-number\">0<\/span>)\n  <span class=\"hljs-keyword\">return<\/span> (\n    <span class=\"xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">className<\/span>=<span class=\"hljs-string\">\"App\"<\/span>&gt;<\/span>\n      <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">h1<\/span>&gt;<\/span> State management 101 <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">h1<\/span>&gt;<\/span>\n      <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">button<\/span> <span class=\"hljs-attr\">onClick<\/span>=<span class=\"hljs-string\">{()<\/span> =&gt;<\/span> setCount(count + 1)}&gt;Clicked me {count} times<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">button<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span><\/span>\n  );\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In the simple code example above you can see how the state is updated when we call the <code>setCount<\/code> function by clicking on the button.<\/p>\n\n\n\n<p>Now, let\u2019s use a bit more practical of an example. We\u2019ll take a look at hooks in this Books component &#8211; <code>.\/src\/Books.js<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-comment\">\/\/ .\/src\/Books.js<\/span>\n\n<span class=\"hljs-keyword\">import<\/span> React, { useState } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">\"react\"<\/span>;\n<span class=\"hljs-comment\">\/\/ array of book objects<\/span>\n<span class=\"hljs-keyword\">const<\/span> books = &#91;\n  {\n    <span class=\"hljs-attr\">title<\/span>: <span class=\"hljs-string\">\"Harry Potter and the Deathly Hallows\"<\/span>,\n    <span class=\"hljs-attr\">price<\/span>: <span class=\"hljs-string\">\"5.00\"<\/span>,\n    <span class=\"hljs-attr\">rating<\/span>: <span class=\"hljs-string\">\"5.0\"<\/span>\n  },\n  {\n    <span class=\"hljs-attr\">title<\/span>: <span class=\"hljs-string\">\"Harry Potter and the Goblet of Fire\"<\/span>,\n    <span class=\"hljs-attr\">price<\/span>: <span class=\"hljs-string\">\"5.00\"<\/span>,\n    <span class=\"hljs-attr\">rating<\/span>: <span class=\"hljs-string\">\"4.8\"<\/span>\n  }\n];\n<span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">Books<\/span>(<span class=\"hljs-params\"><\/span>) <\/span>{\n  <span class=\"hljs-comment\">\/\/ create state variables with initial values<\/span>\n  <span class=\"hljs-keyword\">const<\/span> &#91;savedBooks, setSavedBooks] = useState(&#91;]);\n  <span class=\"hljs-keyword\">const<\/span> &#91;totalPrice, setTotalPrice] = useState(<span class=\"hljs-number\">0<\/span>);\n  <span class=\"hljs-keyword\">return<\/span> (\n    <span class=\"xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n      <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">header<\/span>&gt;<\/span>\n        <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">p<\/span>&gt;<\/span> No. of Books: {savedBooks.length} <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">p<\/span>&gt;<\/span>\n        <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">p<\/span>&gt;<\/span> Total price: ${totalPrice} <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">p<\/span>&gt;<\/span>\n      <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">header<\/span>&gt;<\/span>\n      <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">ul<\/span> <span class=\"hljs-attr\">className<\/span>=<span class=\"hljs-string\">\"Books\"<\/span>&gt;<\/span>\n        {books.map((book) =&gt; {\n          return (\n            <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">li<\/span> <span class=\"hljs-attr\">className<\/span>=<span class=\"hljs-string\">\"book\"<\/span> <span class=\"hljs-attr\">key<\/span>=<span class=\"hljs-string\">{book.name}<\/span>&gt;<\/span>\n              <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">header<\/span>&gt;<\/span>\n                <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">h3<\/span>&gt;<\/span> {book.title} <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">h3<\/span>&gt;<\/span>\n                <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">p<\/span>&gt;<\/span>Rating: {book.rating} <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">p<\/span>&gt;<\/span>\n                <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">p<\/span>&gt;<\/span> ${book.price} <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">p<\/span>&gt;<\/span>\n              <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">header<\/span>&gt;<\/span>\n              <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">button<\/span>&gt;<\/span> Add <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">button<\/span>&gt;<\/span>\n              <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">button<\/span>&gt;<\/span> Remove <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">button<\/span>&gt;<\/span>\n            <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">li<\/span>&gt;<\/span>\n          );\n        })}\n      <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">ul<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span><\/span>\n  );\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Then in <code>.\/src\/App.js:<\/code><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-comment\">\/\/.\/src\/App.js<\/span>\n\n<span class=\"hljs-keyword\">import<\/span> { useState } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">\"react\"<\/span>;\n<span class=\"hljs-keyword\">import<\/span> { Books } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">\".\/Books\"<\/span>;\n<span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-keyword\">default<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">App<\/span>(<span class=\"hljs-params\"><\/span>) <\/span>{\n  <span class=\"hljs-keyword\">return<\/span> (\n    <span class=\"xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">className<\/span>=<span class=\"hljs-string\">\"App\"<\/span>&gt;<\/span>\n      <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">Books<\/span> \/&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span><\/span>\n  );\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In the <code>Books<\/code> component, we imported the <code>useState<\/code> Hook from React. Then we initialized two states of data: <code>savedBooks<\/code> and <code>totalPrice<\/code>.<\/p>\n\n\n\n<p>With hooks, we can have multiple and independent state objects by just calling the <code>useState<\/code> function and providing an initial value for the state.<\/p>\n\n\n\n<p>Let&#8217;s see how we can update the value of the state:&nbsp;<\/p>\n\n\n\n<p>To do that, we&#8217;ll create a function that will update our state with some data. First, we&#8217;ll create a function that will add a book from the <code>books<\/code> <a href=\"https:\/\/coderpad.io\/blog\/development\/javascript-array-methods\/\" target=\"_blank\" rel=\"noreferrer noopener\">array<\/a> to our <code>savedBooks<\/code> array and set the price.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-comment\">\/\/ .\/src\/Books.js<\/span>\n\n<span class=\"hljs-keyword\">import<\/span> React, { useState } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">\"react\"<\/span>;\n<span class=\"hljs-comment\">\/\/ array of book objects<\/span>\n<span class=\"hljs-keyword\">const<\/span> books = &#91;\n  {\n    <span class=\"hljs-attr\">id<\/span>: <span class=\"hljs-string\">\"001\"<\/span>,\n    <span class=\"hljs-attr\">title<\/span>: <span class=\"hljs-string\">\"Harry Potter and the Deathly Hallows\"<\/span>,\n    <span class=\"hljs-attr\">price<\/span>: <span class=\"hljs-string\">\"5.00\"<\/span>,\n    <span class=\"hljs-attr\">rating<\/span>: <span class=\"hljs-string\">\"5.0\"<\/span>\n  },\n  {\n    <span class=\"hljs-attr\">id<\/span>: <span class=\"hljs-string\">\"002\"<\/span>,\n    <span class=\"hljs-attr\">title<\/span>: <span class=\"hljs-string\">\"Harry Potter and the Goblet of Fire\"<\/span>,\n    <span class=\"hljs-attr\">price<\/span>: <span class=\"hljs-string\">\"5.00\"<\/span>,\n    <span class=\"hljs-attr\">rating<\/span>: <span class=\"hljs-string\">\"4.8\"<\/span>\n  }\n];\n<span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">Books<\/span>(<span class=\"hljs-params\"><\/span>) <\/span>{\n  <span class=\"hljs-comment\">\/\/ create state variables with initial values<\/span>\n  <span class=\"hljs-keyword\">const<\/span> &#91;savedBooks, setSavedBooks] = useState(&#91;]);\n  <span class=\"hljs-keyword\">const<\/span> &#91;totalPrice, setTotalPrice] = useState(<span class=\"hljs-number\">0<\/span>);\n\n  <span class=\"hljs-comment\">\/\/ functions to set the state<\/span>\n  <span class=\"hljs-keyword\">const<\/span> add = <span class=\"hljs-function\">(<span class=\"hljs-params\">id<\/span>) =&gt;<\/span> {\n    <span class=\"hljs-comment\">\/\/ set state for savedBooks<\/span>\n    setSavedBooks(&#91;books&#91;<span class=\"hljs-number\">0<\/span>]]);\n    <span class=\"hljs-comment\">\/\/ set state for totalPrice<\/span>\n    setTotalPrice(books&#91;<span class=\"hljs-number\">0<\/span>].price);\n  };\n\n  <span class=\"hljs-comment\">\/\/ reset state values<\/span>\n  <span class=\"hljs-keyword\">const<\/span> remove = <span class=\"hljs-function\"><span class=\"hljs-params\">()<\/span> =&gt;<\/span> {\n    <span class=\"hljs-comment\">\/\/ reset values<\/span>\n    setSavedBooks(&#91;]);\n    setTotalPrice(<span class=\"hljs-number\">0<\/span>);\n  };\n  <span class=\"hljs-keyword\">return<\/span> (\n    <span class=\"xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n      <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">header<\/span>&gt;<\/span>\n        <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">p<\/span>&gt;<\/span> No. of Books: {savedBooks.length} <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">p<\/span>&gt;<\/span>\n        <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">p<\/span>&gt;<\/span> Total price: ${totalPrice} <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">p<\/span>&gt;<\/span>\n      <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">header<\/span>&gt;<\/span>\n      <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">ul<\/span> <span class=\"hljs-attr\">className<\/span>=<span class=\"hljs-string\">\"Books\"<\/span>&gt;<\/span>\n        {books.map((book) =&gt; {\n          return (\n            <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">li<\/span> <span class=\"hljs-attr\">className<\/span>=<span class=\"hljs-string\">\"book\"<\/span> <span class=\"hljs-attr\">key<\/span>=<span class=\"hljs-string\">{book.name}<\/span>&gt;<\/span>\n              <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">header<\/span>&gt;<\/span>\n                <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">h3<\/span>&gt;<\/span> {book.title} <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">h3<\/span>&gt;<\/span>\n                <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">p<\/span>&gt;<\/span>Rating: {book.rating} <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">p<\/span>&gt;<\/span>\n                <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">p<\/span>&gt;<\/span> ${book.price} <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">p<\/span>&gt;<\/span>\n              <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">header<\/span>&gt;<\/span>\n              <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">button<\/span> <span class=\"hljs-attr\">onClick<\/span>=<span class=\"hljs-string\">{add}<\/span>&gt;<\/span> Add <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">button<\/span>&gt;<\/span>\n              <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">button<\/span> <span class=\"hljs-attr\">onClick<\/span>=<span class=\"hljs-string\">{remove}<\/span>&gt;<\/span> Remove <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">button<\/span>&gt;<\/span>\n            <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">li<\/span>&gt;<\/span>\n          );\n        })}\n      <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">ul<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span><\/span>\n  );\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>From the code above, you can see we\u2019ve created two functions, <code>add()<\/code> and <code>remove()<\/code>.&nbsp;<\/p>\n\n\n\n<p>In the <code>add()<\/code> function, we use the <code>setSavedBooks()<\/code> function to set the value of <code>savedBooks<\/code> to the first item in the <code>books<\/code> array.<\/p>\n\n\n\n<p>The <code>add()<\/code> function also uses the <code>setTotalPrice()<\/code> function to set the value of <code>totalPrice<\/code> to the price of the first item in the <code>books<\/code> array.<\/p>\n\n\n\n<p>We fire the <code>add()<\/code> function using the <code>onClick<\/code> <a href=\"https:\/\/coderpad.io\/blog\/development\/addeventlistener-javascript\/\" target=\"_blank\" rel=\"noreferrer noopener\">event listener<\/a> on the <strong>Add <\/strong>button.<\/p>\n\n\n\n<p>Once the button is clicked, <code>savedBooks<\/code> is assigned the value of the first book in the <code>books<\/code> list ( <code>\"Harry Potter and the Deathly Hallows\"<\/code> ) and <code>totalPrice<\/code> now contains the price of that same first book ( $<code>5.00<\/code> ).<\/p>\n\n\n\n<p>The <code>remove()<\/code> function, when clicked, sets <code>savedBooks<\/code> to an empty array and <code>totalPrice<\/code> to <code>0<\/code>. You can try out this example of <code>useState<\/code> here:<\/p>\n\n\n<div\n\tclass=\"sandbox-embed responsive-embed  sandbox-embed--full-width\"\n\tstyle=\"padding-top: 125%\"\ndata-block-name=\"coderpad-sandbox-embed\">\n\t<iframe src=\"https:\/\/embed.coderpad.io\/sandbox?question_id=230409&#038;use_question_button\" width=\"640\" height=\"800\" loading=\"lazy\" aria-label=\"Try out the CoderPad sandbox\"><\/iframe>\n<\/div>\n\n\n\n<p>Now that we&#8217;ve seen how we can set up and update state data with <code>useState<\/code>, let&#8217;s explore how the <code>useReducer<\/code> hook may be used to update the state while using the current state since we&#8217;ve been using static, hard-coded values.&nbsp;<\/p>\n\n\n\n<p>Real-world applications necessitate setting the new state based off of the old state rather than overwriting with an original issue. Let\u2019s dive in.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">useReducer hook<\/h3>\n\n\n\n<p>Unlike <code>useState<\/code> which simply replaces the set value with a new one, e.g when we fired the <code>add()<\/code> function, it replaced the content of <code>savedBooks<\/code> state with the new data, we will be able to update the state based on the previous state using <code>useReducer<\/code>. Similar to the<a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/reduce\" target=\"_blank\" rel=\"noreferrer noopener\"> Array Reduce method<\/a>, this hook is intended to update the state based on the current state.<\/p>\n\n\n\n<p>Let\u2019s start by creating a <code>savedBooksReducer<\/code> function that takes in two arguments: <code>state<\/code> and <code>action<\/code>.&nbsp;<\/p>\n\n\n\n<p><code>state<\/code> is the current state,<\/p>\n\n\n\n<p><code>action<\/code>&nbsp; is an object which contains the following two properties:&nbsp;<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>the <code>book<\/code> object and&nbsp;<\/li>\n\n\n\n<li>the <code>type<\/code> of action to be carried out on the value of the state &#8211; <code>\"add\"<\/code> or <code>\"remove\"<\/code>.<\/li>\n<\/ul>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-comment\">\/\/ .\/src\/Books.js<\/span>\n\n...<span class=\"hljs-comment\">\/\/ saved books reducer function<\/span>\n<span class=\"hljs-keyword\">const<\/span> savedBooksReducer = <span class=\"hljs-function\">(<span class=\"hljs-params\">state, action<\/span>) =&gt;<\/span> {\n  <span class=\"hljs-comment\">\/\/ get the book object and the type of action by destructuring<\/span>\n  <span class=\"hljs-keyword\">const<\/span> { book, type } = action;\n  <span class=\"hljs-comment\">\/\/ if \"add\"<\/span>\n  <span class=\"hljs-comment\">\/\/ return an array of the previous state and the book object<\/span>\n  <span class=\"hljs-keyword\">if<\/span> (type === <span class=\"hljs-string\">\"add\"<\/span>) <span class=\"hljs-keyword\">return<\/span> &#91;...state, book];\n  <span class=\"hljs-comment\">\/\/ if \"remove\"<\/span>\n  <span class=\"hljs-comment\">\/\/ remove the book object in the previous state<\/span>\n  <span class=\"hljs-comment\">\/\/ that matches the title of the current book object<\/span>\n  <span class=\"hljs-keyword\">if<\/span> (type === <span class=\"hljs-string\">\"remove\"<\/span>) {\n    <span class=\"hljs-keyword\">const<\/span> bookIndex = state.findIndex(<span class=\"hljs-function\">(<span class=\"hljs-params\">x<\/span>) =&gt;<\/span> x.title === book.title);\n    <span class=\"hljs-comment\">\/\/ if no match, return the previous state<\/span>\n    <span class=\"hljs-keyword\">if<\/span> (bookIndex &lt; <span class=\"hljs-number\">0<\/span>) <span class=\"hljs-keyword\">return<\/span> state;\n    <span class=\"hljs-comment\">\/\/ avoid mutating the original state, create a copy<\/span>\n    <span class=\"hljs-keyword\">const<\/span> stateUpdate = &#91;...state];\n    <span class=\"hljs-comment\">\/\/ then splice it out from the array<\/span>\n    stateUpdate.splice(bookIndex, <span class=\"hljs-number\">1<\/span>);\n    <span class=\"hljs-keyword\">return<\/span> stateUpdate;\n  }\n  <span class=\"hljs-keyword\">return<\/span> state;\n};\n...\n\nexport <span class=\"hljs-keyword\">default<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">Books<\/span>(<span class=\"hljs-params\"><\/span>) <\/span>{\n\u00a0 \u00a0 ...\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The next step involves creating a function called <code>totalPriceReducer<\/code>, which accepts the same arguments as <code>savedBooksReducer<\/code> but only adds and subtracts the value of the previous state to and from the new state, respectively.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-comment\">\/\/ .\/src\/Books.js<\/span>\n\n<span class=\"hljs-comment\">\/\/ ...<\/span>\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">totalPriceReducer<\/span>(<span class=\"hljs-params\">state, action<\/span>) <\/span>{\n  <span class=\"hljs-keyword\">let<\/span> { price, type } = action;\n  <span class=\"hljs-comment\">\/\/ price = parseFloat(price);<\/span>\n  <span class=\"hljs-keyword\">if<\/span> (type === <span class=\"hljs-string\">\"add\"<\/span>) <span class=\"hljs-keyword\">return<\/span> state + price;\n  <span class=\"hljs-comment\">\/\/ return the value when the type of action was not \"add\"<\/span>\n  <span class=\"hljs-comment\">\/\/ subtract the new book price from the previous state<\/span>\n  <span class=\"hljs-keyword\">if<\/span> (state - price &lt; <span class=\"hljs-number\">0<\/span>) <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-number\">0<\/span>;\n  <span class=\"hljs-keyword\">return<\/span> state - price;\n}\n\n<span class=\"hljs-comment\">\/\/ ...<\/span>\n\n<span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-keyword\">default<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">Books<\/span>(<span class=\"hljs-params\"><\/span>) <\/span>{\n  <span class=\"hljs-comment\">\/\/ ...<\/span>\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Note that here, we have a condition that if (state &#8211; price) is ever less than 0 (negative),&nbsp; the calculation is reset to 0.<\/p>\n\n\n\n<p>Let&#8217;s use our refactored functions in <code>useReducer<\/code> now that we have them.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-comment\">\/\/ .\/src\/Books.js<\/span>\n\n<span class=\"hljs-comment\">\/\/ ...<\/span>\n\n<span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">Books<\/span>(<span class=\"hljs-params\"><\/span>) <\/span>{\n\n  <span class=\"hljs-comment\">\/\/ replace useState with useReducer and pass two arguments<\/span>\n  <span class=\"hljs-comment\">\/\/ the first argument is the reducer function<\/span>\n  <span class=\"hljs-comment\">\/\/ the second function is the initialState<\/span>\n  <span class=\"hljs-keyword\">const<\/span> &#91;savedBooks, setSavedBooks] = useReducer(savedBooksReducer, &#91;]);\n  <span class=\"hljs-keyword\">const<\/span> &#91;totalPrice, setTotalPrice] = useReducer(totalPriceReducer, <span class=\"hljs-number\">0<\/span>);\n\n  <span class=\"hljs-comment\">\/\/ functions to set the state<\/span>\n  <span class=\"hljs-comment\">\/\/ to add items and price<\/span>\n  <span class=\"hljs-keyword\">const<\/span> add = <span class=\"hljs-function\">(<span class=\"hljs-params\">book<\/span>) =&gt;<\/span> {\n\n    <span class=\"hljs-comment\">\/\/ set state for savedBooks<\/span>\n    <span class=\"hljs-comment\">\/\/ pass an object containing the book and type of action, \"add\"<\/span>\n    setSavedBooks({ book, <span class=\"hljs-attr\">type<\/span>: <span class=\"hljs-string\">\"add\"<\/span> });\n\n    <span class=\"hljs-comment\">\/\/ set state for totalPrice<\/span>\n    setTotalPrice({ <span class=\"hljs-attr\">price<\/span>: book.price, <span class=\"hljs-attr\">type<\/span>: <span class=\"hljs-string\">\"add\"<\/span> });\n  };\n\n  <span class=\"hljs-comment\">\/\/ reset state values<\/span>\n  <span class=\"hljs-comment\">\/\/ to remove items and price<\/span>\n  <span class=\"hljs-keyword\">const<\/span> remove = <span class=\"hljs-function\">(<span class=\"hljs-params\">book<\/span>) =&gt;<\/span> {\n    setSavedBooks({ book, <span class=\"hljs-attr\">type<\/span>: <span class=\"hljs-string\">\"remove\"<\/span> });\n    setTotalPrice({ <span class=\"hljs-attr\">price<\/span>: book.price, <span class=\"hljs-attr\">type<\/span>: <span class=\"hljs-string\">\"remove\"<\/span> });\n  };\n\n  <span class=\"hljs-keyword\">return<\/span> (\n  \u00a0 \u00a0 <span class=\"hljs-comment\">\/\/ ...<\/span>\n  \u00a0 );\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Each of the functions are returned from their respective <code>useReducer<\/code> functions takes an object as an argument.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-keyword\">const<\/span> &#91;savedBooks, setSavedBooks] = useReducer(savedBooksReducer, &#91;]);\n<span class=\"hljs-keyword\">const<\/span> &#91;totalPrice, setTotalPrice] = useReducer(totalPriceReducer, <span class=\"hljs-number\">0<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>For <code>setSavedBooks<\/code>, it can take, for example, the object &#8211; <code>{book, type: \"add\"}<\/code> as an argument, while, <code>setTotalPrice<\/code> can also take this object &#8211; <code>{price: book.price, type: \"add\"}<\/code> as an argument.<\/p>\n\n\n\n<p>These objects are then passed into their respective reducer functions to update the state. <code>setSavedBooks<\/code> for example passes its object into <code>savedBooksReducer<\/code>.&nbsp;<\/p>\n\n\n\n<p>Also, having changed our <code>add()<\/code> and <code>remove()<\/code> functions to accept a <code>book<\/code> argument, we can now give the <code>book<\/code> as input to our template\u2019s buttons <code>onClick<\/code> listener.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">Books<\/span>(<span class=\"hljs-params\"><\/span>) <\/span>{\n  <span class=\"hljs-comment\">\/\/ ...<\/span>\n\n  <span class=\"hljs-keyword\">return<\/span> (\n    <span class=\"xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n      { \/\/ ... }\n        <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">ul<\/span> <span class=\"hljs-attr\">className<\/span>=<span class=\"hljs-string\">\"Books\"<\/span>&gt;<\/span>\n          {books.map((book) =&gt; {\n            return (\n              <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">li<\/span> <span class=\"hljs-attr\">className<\/span>=<span class=\"hljs-string\">\"book\"<\/span> <span class=\"hljs-attr\">key<\/span>=<span class=\"hljs-string\">{book.name}<\/span>&gt;<\/span>\n                <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">header<\/span>&gt;<\/span>\n                  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">h3<\/span>&gt;<\/span> {book.title} <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">h3<\/span>&gt;<\/span>\n                  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">p<\/span>&gt;<\/span>Rating: {book.rating} <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">p<\/span>&gt;<\/span>\n                  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">p<\/span>&gt;<\/span> ${book.price} <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">p<\/span>&gt;<\/span>\n                <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">header<\/span>&gt;<\/span>\n                <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">button<\/span> <span class=\"hljs-attr\">onClick<\/span>=<span class=\"hljs-string\">{()<\/span> =&gt;<\/span> add(book)}&gt; Add <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">button<\/span>&gt;<\/span>\n                <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">button<\/span> <span class=\"hljs-attr\">onClick<\/span>=<span class=\"hljs-string\">{()<\/span> =&gt;<\/span> remove(book)}&gt; Remove <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">button<\/span>&gt;<\/span>\n              <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">li<\/span>&gt;<\/span>\n            );\n          })}\n        <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">ul<\/span>&gt;<\/span>\n  \u00a0 \u00a0 <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span><\/span>\n  );\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Now, when we click on <strong>Add <\/strong>or <strong>Remove<\/strong>, the reducer function adds the new data to the previous state without overwriting it. Here&#8217;s an example of what that implementation of <code>useReducer<\/code> would look like:<\/p>\n\n\n<div\n\tclass=\"sandbox-embed responsive-embed  sandbox-embed--full-width\"\n\tstyle=\"padding-top: 125%\"\ndata-block-name=\"coderpad-sandbox-embed\">\n\t<iframe src=\"https:\/\/embed.coderpad.io\/sandbox?question_id=230420&#038;use_question_button\" width=\"640\" height=\"800\" loading=\"lazy\" aria-label=\"Try out the CoderPad sandbox\"><\/iframe>\n<\/div>\n\n\n\n<p>So far, we\u2019ve been dealing with state contained in one component. In a real-world app, we\u2019ll need to pass data\/state between multiple components, from parent to child, child to parent, and between sibling components (components with the same parent).<\/p>\n\n\n\n<p>In the next section, we\u2019ll take a look at how we can manage state between components using props.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Props<\/h2>\n\n\n\n<p>\u201cProps\u201d is short for \u201cproperties\u201d and they are arguments passed to React components. We use props to pass data from the parent to child component.<\/p>\n\n\n\n<p>To illustrate this, we\u2019ll move our <code>books<\/code> array, <code>savedBooksReducer<\/code> and <code>totalPriceReducer<\/code> functions to our <code>.\/src\/App.js<\/code> file.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-comment\">\/\/ .\/src\/App.js<\/span>\n\n<span class=\"hljs-keyword\">import<\/span> { Books } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">\".\/Books\"<\/span>;\n\n<span class=\"hljs-comment\">\/\/ array of book objects<\/span>\n<span class=\"hljs-keyword\">const<\/span> books = &#91;\n  <span class=\"hljs-comment\">\/\/ ...<\/span>\n];\n\n<span class=\"hljs-comment\">\/\/ saved books reducer function<\/span>\n<span class=\"hljs-keyword\">const<\/span> savedBooksReducer = <span class=\"hljs-function\">(<span class=\"hljs-params\">state, action<\/span>) =&gt;<\/span> {\n  <span class=\"hljs-comment\">\/\/ ...<\/span>\n};\n\n<span class=\"hljs-comment\">\/\/ total price reducer function<\/span>\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">totalPriceReducer<\/span>(<span class=\"hljs-params\">state, action<\/span>) <\/span>{\n  <span class=\"hljs-comment\">\/\/ ...<\/span>\n}\n\n<span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-keyword\">default<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">App<\/span>(<span class=\"hljs-params\"><\/span>) <\/span>{\n  <span class=\"hljs-keyword\">return<\/span> (\n    <span class=\"xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">className<\/span>=<span class=\"hljs-string\">\"App\"<\/span>&gt;<\/span>\n      <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">Books<\/span>\n        <span class=\"hljs-attr\">books<\/span>=<span class=\"hljs-string\">{books}<\/span>\n        <span class=\"hljs-attr\">savedBooksReducer<\/span>=<span class=\"hljs-string\">{savedBooksReducer}<\/span>\n        <span class=\"hljs-attr\">totalPriceReducer<\/span>=<span class=\"hljs-string\">{totalPriceReducer}<\/span>\n      \/&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span><\/span>\n  );\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>As you can see from the code above, in order to pass the data into the <code>&lt;Books&gt;<\/code> component, we use JSX component bindings and pass the data.<\/p>\n\n\n\n<p>To access this data from within the component we destructure the <code>props<\/code> parameter in our <code>Books<\/code> function definition.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-comment\">\/\/ .\/src\/Books.js<\/span>\n\n<span class=\"hljs-keyword\">import<\/span> React, { useReducer } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">\"react\"<\/span>;\n\n<span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">Books<\/span>(<span class=\"hljs-params\">{ books, savedBooksReducer, totalPriceReducer }<\/span>) <\/span>{\n\u00a0 <span class=\"hljs-comment\">\/\/ ...<\/span>\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<blockquote class=\"wp-block-quote\">\n<p>\u2705 You can see these props in action in the Context API sandbox example below.<\/p>\n<\/blockquote>\n\n\n\n<p>While this is great for simple applications, when the application begins to scale and data needs to be passed from children back up to parents, between sibling components, or even globally throughout the app, relying only on props might not be such a great idea.<\/p>\n\n\n\n<p>Next, we\u2019re going to look at the Context API which helps us share state a bit better.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Context API<\/h2>\n\n\n\n<p>Context API provides a way to move data up and down the component tree of your app without having to manually pass props through numerous component levels. You can configure a &#8220;global&#8221; state for a tree of React components using Context. Once this is done, the state is accessible from any component in the tree&nbsp; without passing it through intermediary components.<\/p>\n\n\n\n<p>Let&#8217;s further restructure our application by creating a new component, <code>SavedBooks<\/code>, which will contain the total number of saved books and the total price. The <code>Books<\/code> component will still contain the list of books.&nbsp;<\/p>\n\n\n\n<p>Before we do that, let\u2019s set up a global state in our application with the Context API.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Initialize context<\/h3>\n\n\n\n<p>Create a new file <code>.\/src\/modules\/BooksContext.js<\/code> specifically for our context, and then import <code>createContext<\/code> and use it to create our <code>BooksContext<\/code>.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-comment\">\/\/ .\/src\/modules\/BooksContext.js<\/span>\n\n<span class=\"hljs-keyword\">import<\/span> React, {createContext} <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">\"react\"<\/span>\n<span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-keyword\">const<\/span> BooksContext = createContext()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Next, we create a context provider.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Create a context provider<\/h3>\n\n\n\n<p>A context object comes with a <code>Provider<\/code> component that allows components wrapped within it to subscribe to context changes.<\/p>\n\n\n\n<p>We\u2019re going to once again move our <code>books<\/code> array and <code>savedBooksReducer<\/code> functions to our <code>.\/src\/modules\/BooksContext.js<\/code> file and initialize the state within a new <code>BooksProvider<\/code> function using the <code>useState<\/code> and <code>useReducer<\/code> hook.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-comment\">\/\/ .\/src\/modules\/BooksContext.js<\/span>\n\n<span class=\"hljs-keyword\">import<\/span> React, { useContext, useReducer, useState } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">\"react\"<\/span>;\n\n<span class=\"hljs-comment\">\/\/ array of book objects<\/span>\n<span class=\"hljs-keyword\">const<\/span> bookList = &#91;\n  <span class=\"hljs-comment\">\/\/ ...<\/span>\n];\n\n<span class=\"hljs-comment\">\/\/ saved books reducer function<\/span>\n<span class=\"hljs-keyword\">const<\/span> savedBooksReducer = <span class=\"hljs-function\">(<span class=\"hljs-params\">state, action<\/span>) =&gt;<\/span> {\n  <span class=\"hljs-comment\">\/\/ ...<\/span>\n};\n\n<span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-keyword\">const<\/span> BooksContext = createContext();\n\n<span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-keyword\">const<\/span> BooksProvider = <span class=\"hljs-function\">(<span class=\"hljs-params\">props<\/span>) =&gt;<\/span> {\n  <span class=\"hljs-keyword\">const<\/span> &#91;books, setBooks] = useState(bookList)\n  <span class=\"hljs-keyword\">const<\/span> &#91;savedBooks, setSavedBooks] = useReducer(savedBooksReducer, &#91;])\n  <span class=\"hljs-keyword\">return<\/span> <span class=\"xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">BooksContext.Provider<\/span>&gt;<\/span> {props.children} <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">BooksContext.Provider<\/span>&gt;<\/span><\/span>\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In the code above, you can see that we&#8217;re returning the Provider component <code>&lt;BooksContext.Provider<\/code>&gt; from <code>BooksProvider<\/code>.<\/p>\n\n\n\n<p><code>props.children<\/code> will allow us to render the components to be nested in the provider.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Accessing the state<\/h3>\n\n\n\n<p>Components that are descendants of the Provider can access a <code>value<\/code> prop that the <code>&lt;BooksContext.Provider&gt;<\/code> component accepts.&nbsp;<\/p>\n\n\n\n<p>All of our state must be entered into this <code>value<\/code> prop in our context file like so:<\/p>\n\n\n\n<p><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-15\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-comment\">\/\/ .\/src\/modules\/BooksContext.js<\/span>\n\n...\nreturn (\n  <span class=\"xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">BooksContext.Provider<\/span>\n    <span class=\"hljs-attr\">value<\/span>=<span class=\"hljs-string\">{{<\/span>\n      <span class=\"hljs-attr\">bookList<\/span>,\n      <span class=\"hljs-attr\">setBookList<\/span>,\n      <span class=\"hljs-attr\">savedBooks<\/span>,\n      <span class=\"hljs-attr\">setSavedBooks<\/span>,\n    }}\n  &gt;<\/span>\n    {props.children}\n  <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">BooksContext.Provider<\/span>&gt;<\/span><\/span>\n);\n...<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-15\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In order for this component to be accessible to the rest of our application, we\u2019ll have refactor our <code>.\/src\/App.js<\/code> file to use and wrap the application with the <code>BooksProvider<\/code> component:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-16\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-comment\">\/\/ .\/src\/App.js<\/span>\n\n<span class=\"hljs-keyword\">import<\/span> { Books } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">\".\/Books\"<\/span>;\n<span class=\"hljs-keyword\">import<\/span> { BooksProvider } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">\".\/modules\/BooksContext\"<\/span>;\n\n<span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-keyword\">default<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">App<\/span>(<span class=\"hljs-params\"><\/span>) <\/span>{\n  <span class=\"hljs-keyword\">return<\/span> (\n    <span class=\"xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">BooksProvider<\/span>&gt;<\/span>\n      <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">className<\/span>=<span class=\"hljs-string\">\"App\"<\/span>&gt;<\/span>\n        <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">Books<\/span> \/&gt;<\/span>\n      <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">BooksProvider<\/span>&gt;<\/span><\/span>\n  );\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-16\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>To subscribe to the state passed to the provider component in our components, we have to import <code>BooksContext<\/code> and consume it with the <code>useContext<\/code> hook.&nbsp;<\/p>\n\n\n\n<p>Here&#8217;s how we can do that in our <code>Books<\/code> component:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-17\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-comment\">\/\/ .\/src\/Books.js<\/span>\n\n<span class=\"hljs-comment\">\/\/ import the useContext Hook<\/span>\n<span class=\"hljs-keyword\">import<\/span> React, { useContext } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">\"react\"<\/span>;\n\n<span class=\"hljs-comment\">\/\/ import the Context<\/span>\n<span class=\"hljs-keyword\">import<\/span> { BooksContext } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">\".\/modules\/BooksContext\"<\/span>;\n\n<span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">Books<\/span>(<span class=\"hljs-params\"><\/span>) <\/span>{\n  <span class=\"hljs-keyword\">const<\/span> { books, setSavedBooks } = useContext(BooksContext);\n\n  <span class=\"hljs-comment\">\/\/ functions to set the state<\/span>\n  <span class=\"hljs-keyword\">const<\/span> add = <span class=\"hljs-function\">(<span class=\"hljs-params\">book<\/span>) =&gt;<\/span> {\n\n    <span class=\"hljs-comment\">\/\/ set state for savedBooks<\/span>\n    <span class=\"hljs-comment\">\/\/ pass an object containing the book and type of action, \"add\"<\/span>\n    setSavedBooks({ book, <span class=\"hljs-attr\">type<\/span>: <span class=\"hljs-string\">\"add\"<\/span> });\n  };\n\n  <span class=\"hljs-comment\">\/\/ reset state values<\/span>\n  <span class=\"hljs-keyword\">const<\/span> remove = <span class=\"hljs-function\">(<span class=\"hljs-params\">book<\/span>) =&gt;<\/span> {\n    setSavedBooks({ book, <span class=\"hljs-attr\">type<\/span>: <span class=\"hljs-string\">\"remove\"<\/span> });\n  };\n\n  <span class=\"hljs-keyword\">return<\/span> (\n    <span class=\"xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n      <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">ul<\/span> <span class=\"hljs-attr\">className<\/span>=<span class=\"hljs-string\">\"Books\"<\/span>&gt;<\/span>\n        {books.map((book) =&gt; {\n          return (\n            <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">li<\/span> <span class=\"hljs-attr\">className<\/span>=<span class=\"hljs-string\">\"book\"<\/span> <span class=\"hljs-attr\">key<\/span>=<span class=\"hljs-string\">{book.name}<\/span>&gt;<\/span>\n              <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">header<\/span>&gt;<\/span>\n                <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">h3<\/span>&gt;<\/span> {book.title} <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">h3<\/span>&gt;<\/span>\n                <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">p<\/span>&gt;<\/span>Rating: {book.rating} <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">p<\/span>&gt;<\/span>\n                <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">p<\/span>&gt;<\/span> ${book.price} <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">p<\/span>&gt;<\/span>\n              <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">header<\/span>&gt;<\/span>\n              <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">button<\/span> <span class=\"hljs-attr\">onClick<\/span>=<span class=\"hljs-string\">{()<\/span> =&gt;<\/span> add(book)}&gt; Add <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">button<\/span>&gt;<\/span>\n              <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">button<\/span> <span class=\"hljs-attr\">onClick<\/span>=<span class=\"hljs-string\">{()<\/span> =&gt;<\/span> remove(book)}&gt; Remove <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">button<\/span>&gt;<\/span>\n            <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">li<\/span>&gt;<\/span>\n          );\n        })}\n      <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">ul<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span><\/span>\n  );\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-17\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Based on the code above, we\u2019re able to access <code>books<\/code> and <code>setSavedBooks<\/code> state from the <code>BooksContext<\/code> using <code>useContext<\/code>.<\/p>\n\n\n\n<p>Now that we have our global state set up with context, let\u2019s create our <code>SavedBooks<\/code> component:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-18\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-comment\">\/\/ .\/src\/SavedBooks.js<\/span>\n\n<span class=\"hljs-keyword\">import<\/span> React, { useContext } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">\"react\"<\/span>;\n<span class=\"hljs-keyword\">import<\/span> { BooksContext } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">\".\/modules\/BooksContext\"<\/span>;\n<span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">SavedBooks<\/span>(<span class=\"hljs-params\"><\/span>) <\/span>{\n  <span class=\"hljs-keyword\">const<\/span> { savedBooks } = useContext(BooksContext);\n\n  <span class=\"hljs-comment\">\/\/ function to get total price from savedbooks array<\/span>\n  <span class=\"hljs-keyword\">const<\/span> getTotalPrice = <span class=\"hljs-function\">(<span class=\"hljs-params\">savedBooks<\/span>) =&gt;<\/span> {\n    <span class=\"hljs-keyword\">const<\/span> totalPrice = savedBooks.reduce(\n      <span class=\"hljs-function\">(<span class=\"hljs-params\">totalCost, item<\/span>) =&gt;<\/span> totalCost + item.price,\n      <span class=\"hljs-number\">0<\/span>\n    );\n    <span class=\"hljs-keyword\">return<\/span> totalPrice;\n  };\n  <span class=\"hljs-keyword\">return<\/span> (\n    <span class=\"xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">header<\/span>&gt;<\/span>\n      <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">p<\/span>&gt;<\/span> No. of Books: {savedBooks.length} <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">p<\/span>&gt;<\/span>\n      <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">p<\/span>&gt;<\/span> Total price: ${getTotalPrice(savedBooks)} <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">p<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">header<\/span>&gt;<\/span><\/span>\n  );\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-18\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Finally, now that we\u2019ve separated the <code>Books<\/code> component and created the <code>SavedBooks<\/code> component, let\u2019s add it in <code>.\/src\/App.js:<\/code><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-19\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-comment\">\/\/ .\/src\/App.js<\/span>\n\n<span class=\"hljs-keyword\">import<\/span> { Books } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">\".\/Books\"<\/span>;\n<span class=\"hljs-keyword\">import<\/span> { BooksProvider } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">\".\/modules\/BooksContext\"<\/span>;\n<span class=\"hljs-keyword\">import<\/span> { SavedBooks } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">\".\/SavedBooks\"<\/span>;\n<span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-keyword\">default<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">App<\/span>(<span class=\"hljs-params\"><\/span>) <\/span>{\n  <span class=\"hljs-keyword\">return<\/span> (\n    <span class=\"xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">BooksProvider<\/span>&gt;<\/span>\n      <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">className<\/span>=<span class=\"hljs-string\">\"App\"<\/span>&gt;<\/span>\n        <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">SavedBooks<\/span> \/&gt;<\/span>\n        <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">Books<\/span> \/&gt;<\/span>\n      <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">BooksProvider<\/span>&gt;<\/span><\/span>\n  );\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-19\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Now, you can see that we\u2019re able to pass the data between sibling components using the Context API:<\/p>\n\n\n<div\n\tclass=\"sandbox-embed responsive-embed  sandbox-embed--full-width\"\n\tstyle=\"padding-top: 125%\"\ndata-block-name=\"coderpad-sandbox-embed\">\n\t<iframe src=\"https:\/\/embed.coderpad.io\/sandbox?question_id=230454&#038;use_question_button\" width=\"640\" height=\"800\" loading=\"lazy\" aria-label=\"Try out the CoderPad sandbox\"><\/iframe>\n<\/div>\n\n\n\n<p>Awesome!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Up next: Comparing two state management libraries<\/h2>\n\n\n\n<p>So far, we\u2019ve covered the basics of state management in a React application using React Hooks like <code>useState<\/code> and <code>useReducer<\/code>.&nbsp;<\/p>\n\n\n\n<p>We\u2019ve also seen how we can take local component state management a step further by setting up a global application state using the Context API which ships with React.<\/p>\n\n\n\n<p>With this, we can build small to medium scale applications without having to install and depend on third party state management libraries.<\/p>\n\n\n\n<p>Do you feel ready to dive into state management with libraries? Stay tuned for my article next week comparing Redux and Recoil.&nbsp;<\/p>\n\n\n\n<p><em>My name is<\/em> <em>Victory Tuduo and I am a software developer who loves building web applications, creating flexible UI designs, mentoring, and writing technical articles.<\/em> <em>You can find out more about me and my work <a href=\"https:\/\/twitter.com\/vhicktri\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a> and <a href=\"https:\/\/victorytuduo.vercel.app\/\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a>.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Good data management and communication between individual components are pivotal for cleaner code and improved data sharing between components. This article introduces you to state and global state in React and the various ways to manage global state without a library in React.<\/p>\n","protected":false},"author":12,"featured_media":20505,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[9],"tags":[],"persona":[29],"blog-programming-language":[61],"keyword-cluster":[],"class_list":["post-20498","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-development"],"acf":[],"_links":{"self":[{"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/posts\/20498","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/users\/12"}],"replies":[{"embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/comments?post=20498"}],"version-history":[{"count":95,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/posts\/20498\/revisions"}],"predecessor-version":[{"id":34568,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/posts\/20498\/revisions\/34568"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/media\/20505"}],"wp:attachment":[{"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/media?parent=20498"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/categories?post=20498"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/tags?post=20498"},{"taxonomy":"persona","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/persona?post=20498"},{"taxonomy":"blog-programming-language","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/blog-programming-language?post=20498"},{"taxonomy":"keyword-cluster","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/keyword-cluster?post=20498"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}