<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Martin Crabtree on Medium]]></title>
        <description><![CDATA[Stories by Martin Crabtree on Medium]]></description>
        <link>https://medium.com/@martin.crabtree?source=rss-486964e11fac------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*apy_bLYjAKiIFZuAayttlw.jpeg</url>
            <title>Stories by Martin Crabtree on Medium</title>
            <link>https://medium.com/@martin.crabtree?source=rss-486964e11fac------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 22:13:55 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@martin.crabtree/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[JavaScript: Testing  With Jest.js]]></title>
            <link>https://medium.com/@martin.crabtree/javascript-testing-with-jest-js-52d7adb40ffd?source=rss-486964e11fac------2</link>
            <guid isPermaLink="false">https://medium.com/p/52d7adb40ffd</guid>
            <dc:creator><![CDATA[Martin Crabtree]]></dc:creator>
            <pubDate>Thu, 09 Apr 2020 17:57:35 GMT</pubDate>
            <atom:updated>2020-04-09T17:57:35.453Z</atom:updated>
            <content:encoded><![CDATA[<h3>JavaScript: Testing Your Code With Jest.js</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Co50nMhifubh-uz-IKoBBg.png" /></figure><h4>What is Jest.js?</h4><p>Jest.js is a JavaScript testing framework that can be used to generate automated tests and coverage reports for your code. It is a common framework that is used in real-world development so I thought it would be helpful to provide an introduction to using Jest.js in a development environment.</p><h4>Installing Jest.js</h4><p>To install Jest.jst, use the Bash Command Line to navigate to your program folder and use npm to create a new JavaScript project with the command “npm init -y”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/775/1*1WWJ8XX8ep-bxspcuHhx-w.png" /></figure><p>To install, use the bash command line to navigate to your program folder and install jest by typing, “npm install jest — save-dev”. Since you will only deploy tested code, the “ — save-dev” tag only installs Jest to your development environment for testing.</p><p>We also need to add Jest to the package.json file so that npm is aware of Jest when running and testing code.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/671/1*GrZYsDocCyjkLPm28mcvaA.png" /></figure><h4>Writing a Simple Test</h4><p>First create a new folder in your project called “_tests_” to house all of the tests that will be referenced by Jest.</p><p>Inside we will create a test file in the format “<em>xxxxx</em>.spec.js”, where <em>xxxxx</em> should be a descriptive reference to the function of the test.</p><p>Jest comes with a series of global methods that are accessible without an import or require reference. A full list of methods can be found <a href="https://jestjs.io/docs/en/api">here</a>. In this case we will use the <em>describe()</em> method, which accepts two arguments: a name (String), and a function.</p><p>In this case the function we want to execute is the Jest.js <em>global test()</em> function, which accepts the following arguments: a test name (String), function, and an optional timeout duration (in milliseconds). In this example we create a test case called “Cat Checker”, with a test that checks to see if the animal property of an object contains the word “CAT”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/857/1*rn95msGCedY7EjDTTPSRVw.png" /></figure><p>This test has 3 components: an input, an output, and the Jest.js <em>expect()</em> function. The <em>expect()</em> function usually houses a matcher function that evaluates if the given input returns the expected output. In this we compare the result of a custom filterByTerm function to the expected output using the <em>toEqual()</em> matcher function.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/855/1*dRPzPrRJWDtY9Ks8NFahcQ.png" /></figure><h4>Code Coverage:</h4><p>Code coverage is a measure of how much code has been checked by the testing software. Conveniently, Jest.js has a built in code coverage reporting utility tied to its testing function. To run a code coverage report, add the “ — coverage” flag when running a test from the command line.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/616/1*-5gUQ4sLwnkxKJ1oxJPSNg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/709/1*ZjmrcUS0MgW7ISxWytImCg.png" /></figure><p>Jest.js also has an option to visualize your code coverage report in HTML format. To enable this feature add the following properties to your package.json file.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/725/1*9Z8ymwgUpp4kShhd_CgOzg.png" /></figure><p>This now, after running your tests, the results can be viewed by opening the newly created ../coverage/index.html file.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/966/1*CDvrGqvqCavaYjsuQo4dGg.png" /></figure><p>For more info on the other tests available in Jest.js, check out their documentation at <a href="https://jestjs.io/en/">https://jestjs.io/en/</a>. Happy testing!</p><h4>Sources:</h4><p><a href="https://jestjs.io/">Jest</a></p><p><a href="https://www.valentinog.com/blog/jest/">https://www.valentinog.com/blog/jest/</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=52d7adb40ffd" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[JavaScript: Unit Testing With Jest]]></title>
            <link>https://medium.com/@martin.crabtree/javascript-unit-testing-with-jest-cc2575da5203?source=rss-486964e11fac------2</link>
            <guid isPermaLink="false">https://medium.com/p/cc2575da5203</guid>
            <dc:creator><![CDATA[Martin Crabtree]]></dc:creator>
            <pubDate>Sat, 04 Apr 2020 21:25:07 GMT</pubDate>
            <atom:updated>2020-04-04T21:25:07.672Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/250/1*LtdmlbCCgYXqROUujqS_eg.png" /></figure><p>What is Jest?</p><p>Installing Jest</p><p>First, use the Bash Command Line to navigate to your program folder and use npm to create a new JavaScript project with the command “npm init -y”.</p><p>To install, use the bash command line to navigate to your program folder and install jest by typing, “npm install jest — save-dev”. Since you will only deploy tested code, the “ — save-dev” tag only installs Jest to your development environment for testing.</p><p>We also need to add Jest to the package.json file so that npm is aware of Jest when running and testing code.</p><p>“scripts”: {</p><p>“test”: “jest”</p><p>},</p><p>Writing a Simple Test</p><p>First create a new folder in your project called “_tests_” to house all of the tests that will be referenced by Jest.</p><p>Inside we will create a test file in the format “xxxxx.spec.js”, where xxxxx should be a descriptive reference to the function of the test.</p><p>Jest comes with a series of global methods that are accessible without an import or require reference.</p><p><a href="https://jestjs.io/">https://jestjs.io/</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cc2575da5203" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[JavaScript: Tracking Key Value Pairs Using Hashmaps]]></title>
            <link>https://medium.com/@martin.crabtree/javascript-tracking-key-value-pairs-using-hashmaps-7de6df598257?source=rss-486964e11fac------2</link>
            <guid isPermaLink="false">https://medium.com/p/7de6df598257</guid>
            <dc:creator><![CDATA[Martin Crabtree]]></dc:creator>
            <pubDate>Fri, 27 Mar 2020 03:33:50 GMT</pubDate>
            <atom:updated>2020-03-27T03:33:50.413Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*02E8ak_ONo1OjoV9yPS6TA.png" /></figure><p>Hashtables are often coveted in algorithm optimization for their O(1) constant time lookup. While JavaScript doesn’t have a native Hashtable class, it does have native Objects and Hashmaps(Map) that offer similar functionality when it comes to organizing key/value pairs.</p><h4>Hashtable vs Hashmap:</h4><p>Hashtables and hashmaps are data structures that store data in an array-like format, using key/value pairs, where the (hashed) key corresponds to the index in the array. One of the primary benefits that a hashtable has over a hashmap is the native ability to handle synchronous updates. This means that a hashtable can be shared by multiple threads without introducing desynching errors.</p><p>Hashmaps offer the same key/value functionality and come native in JavaScript (ES6) in the form of the Map() object (not to be confused with Array.prototype.map()). While hashmaps are limited to single-threaded code, they do have some benefits, for instance the allowance of null values which allows for greater flexibility.</p><h4>JavaScript Objects: Similar but Different</h4><p>You might be thinking, “but JavaScript objects already keep track of key/value pairs.” While you can use JS objects to serve the same function of a hashmap, a hashmap does offer some benefits. For instance, hashmaps offer greater flexibility. The key in a hashmap can be any datatype, this includes arrays and objects. Meanwhile, objects can only use integers, strings, and symbols as their keys.</p><p>Hashmaps are organized as linked lists, so the order of its elements is maintained, which allows the hashmap to be iterable. Also, unlike key/value pairs stored in an Object, the number of items in a hashmap can be determined by the size property.</p><p><em>Note: One of the primary pitfalls to be aware of when it comes to hashmaps in JavaScript, is that they cannot be directly translated to JSON.</em></p><h4>Using the JavaScript Hashmap Object Map():</h4><p><strong><em>Declaration and Initialization:</em></strong></p><figure><img alt="How to declare and initialize a new JS hashmap object." src="https://cdn-images-1.medium.com/max/970/1*GLjtAmfB4uOCQGf9i00p-g.png" /><figcaption>How to declare and initialize a new JS hashmap object.</figcaption></figure><p><strong><em>Useful Methods and Properties:</em></strong></p><ul><li><strong><em>hashmap.size()</em></strong> returns the # of elements in the hashmap</li><li><strong><em>hashmap.get(&lt;key&gt;)</em></strong> returns the value of the element of the given key</li><li><strong><em>hashmap.has(&lt;key&gt;)</em></strong> checks to see if the hashmap contains the key that is passed as an argument</li><li><strong><em>hashmap.set(&lt;key&gt;, &lt;value&gt;)</em></strong> accepts 2 arguments and creates a new element to the hashmap</li><li><strong><em>hashmap.delete(&lt;key&gt;)</em></strong> deletes the key/value pair that matches the key that is passed in as an argument</li><li><strong><em>hashmap.clear()</em></strong> clears all elements from the hashmap</li></ul><figure><img alt=".size() returns the number of elements in the hashmap" src="https://cdn-images-1.medium.com/max/844/1*Dc37awhRHKdByysB1dYPnQ.png" /><figcaption>The .size() method for JS hashmap objects is similar to array.length() and returns the number of elements in the hashmap.</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1007/1*klD0AYkQl20cz35_5qk4OA.png" /></figure><p>Hashmaps are a great way to manage key/value pairs and will work in most instances for JS applications. Just be aware that there are instances — in particular if you are integrating your key/value pairs with JSON — where you might want to rely on standard JavaScript objects.</p><p><strong>Sources:</strong></p><p><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map">https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map</a></p><p><a href="https://sunfishempire.wordpress.com/2014/08/19/5-ways-to-use-a-javascript-hashmap/">https://sunfishempire.wordpress.com/2014/08/19/5-ways-to-use-a-javascript-hashmap/</a></p><p><a href="https://adrianmejia.com/data-structures-time-complexity-for-beginners-arrays-hashmaps-linked-lists-stacks-queues-tutorial/">https://adrianmejia.com/data-structures-time-complexity-for-beginners-arrays-hashmaps-linked-lists-stacks-queues-tutorial/</a></p><p><a href="https://www.geeksforgeeks.org/map-vs-object-in-javascript/">https://www.geeksforgeeks.org/map-vs-object-in-javascript/</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7de6df598257" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Android SDK: Manually Setting a DNS for Android Emulator]]></title>
            <link>https://medium.com/@martin.crabtree/android-sdk-manually-setting-a-dns-for-android-emulator-6b14028e9667?source=rss-486964e11fac------2</link>
            <guid isPermaLink="false">https://medium.com/p/6b14028e9667</guid>
            <dc:creator><![CDATA[Martin Crabtree]]></dc:creator>
            <pubDate>Thu, 19 Mar 2020 19:28:08 GMT</pubDate>
            <atom:updated>2020-03-19T19:28:08.761Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/953/1*7LppIDOCaK9Q-9yJ584tng.png" /></figure><h4>DNS… hello?</h4><p>If your virtual device in the Android Emulator is having issues connecting to the internet, below is a potential solution to a problem that I was having with the AVD (Android Virtual Device) Manager that comes with the Android SDK.</p><p>Specifically, when I launched an instance of a device using AVD Manager the device was unable to connect to the internet. As a result, the lovely code that I had written wouldn’t execute since all of the calls to my API timed out.</p><p>Sometimes this issue is caused by the DNS server address used by the emulator’s virtual router. If this seems to be the case for your Android virtual device, this blog outlines what to look for, and a potential fix for the DNS/internet connection issue.</p><h4>What to look for:</h4><h4><strong>1. No internet message</strong></h4><p>Make sure that there’s at least one internet transmission mode active (e.g. the Wireless symbol is visible). Examining your WiFi connection will display the message, “Connected, no internet.”</p><figure><img alt="screenshot no internet connection" src="https://cdn-images-1.medium.com/max/761/1*U_VaIJgO9tbE1TNc2TqZ_g.png" /><figcaption>Confirm: The virtual device is connected to the virtual router but not to the internet.</figcaption></figure><p>The reason that the device has successfully connected is because the Android Emulator used by the AVD Manager creates a virtual router that routes the internet traffic through your computer to the virtual device.</p><h4><strong>2. Error: DNS Bad Config</strong></h4><p>Try opening a webpage on your Android virtual device. If the webpage times out and gives you the error code, “DNS_PROBE_FINISHED_BAD_CONFIG”, there could be an issue with the DNS server address used by the virtual router.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/872/1*etItvxwfQfqUppYsBVEUKQ.png" /><figcaption>Opening a webpage times out and produces a DNS error.</figcaption></figure><h4>It’s Terminal: Fixing the DNS address issue</h4><p>To fix this issue, you need to manually override the DNS server address that is being used by the virtual router.</p><p>The first step is to enter your terminal and navigate to the “emulator” folder in your Android SDK. To check the available virtual devices, type the command “emulator -list-avds”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/707/1*DnQru1CrrN1ryAmu6G7GbA.png" /><figcaption>Getting a list of available virtual devices for the Android Emulator.</figcaption></figure><p>Then run one of the available emulators with the command, “emulator -avd DEVICE_NAME”, followed by the argument “-dns-server 8.8.8.8”. This will launch the emulator and overwrite the default DNS server address, replacing it with the address of the primary DNS server for Google DNS.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/890/1*7dKbKYdUeD5KgHGdRBEKTA.png" /><figcaption>Overwriting the default DNS server address in the virtual router, and then launching the Android Emulator.</figcaption></figure><p>And that’s it! While fairly specific, it is a pretty annoying issue, so I thought I’d provide a quick fix to the world at large.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uKZKHJLp8pHXq57MUWGgNg.png" /><figcaption>Success!</figcaption></figure><h4>Sources:</h4><p><a href="https://developer.android.com/studio/run/emulator-commandline">Start the emulator from the command line | Android Developers</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6b14028e9667" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Using TypeScript with React.js Hooks: Part 1 of 2]]></title>
            <link>https://medium.com/@martin.crabtree/using-typescript-with-react-js-hooks-part-1-of-2-319b3be83046?source=rss-486964e11fac------2</link>
            <guid isPermaLink="false">https://medium.com/p/319b3be83046</guid>
            <dc:creator><![CDATA[Martin Crabtree]]></dc:creator>
            <pubDate>Thu, 12 Mar 2020 21:43:25 GMT</pubDate>
            <atom:updated>2020-03-12T21:43:25.669Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="TypeScript banner" src="https://cdn-images-1.medium.com/max/1024/1*u8pYpXvDIKbtjv0-iUI10Q.png" /></figure><h4>Why TypeScript and React.js?:</h4><p>Using TypeScript in React.js applications can reduce instances of errors in your code, especially when you are planning on updating or expanding your application. One of the major caveats to developing applications using TypeScript is that it does require you to front-load more work in order to define typed variables and objects. On the plus side, this time can be easily recuperated by reducing the time spent debugging and walking through code.</p><p>What is TypeScript:</p><p>Just in case you are interested in writing clean code using React.js hooks, but aren’t too familiar with TypeScript, here’s a quick primer. TypeScript was designed by Microsoft, and is a superset of JavaScript that is compiled into JavaScript. It essentially acts as a shell that allows you to introduce strongly typed objects and variables in JavaScript, and some additional functionality. Even though TypeScript is compiled to JavaScript, you can still use vanilla JavaScript in TypeScript files.</p><p>TypeScript Interfaces:</p><p>One of the primary tools required for implementing TypeScript in React.js hooks are interfaces. An interface serves as a prototype that defines the data-types within an object or function, and also assigns an interface name that can be used to reference objects of that type. Interfaces can also assign other properties to variables such as “read only” and constant properties.</p><figure><img alt="TypeScript interface" src="https://cdn-images-1.medium.com/max/948/1*1vZ-ORR4a4YQ_CAF95ak6A.png" /><figcaption>A baby TS interface in it’s native habitat. Here we define a TS interface named IUserInfo, that contains two attributes of the data-type string.</figcaption></figure><h4>TypeScript With Basic React Hooks:</h4><p>In the remainder of this blog, we’ll walk through two of the most used, and basic React.js hooks — useState() and useEffect() — using TypeScript.</p><h4>Basic TypeScript in a React.js Component:</h4><p>Let’s first look at a basic implementation of TypeScript within a React.js Component. Note that the function type React.FC, denotes the function as a React <strong>F</strong>unction <strong>C</strong>omponent.</p><figure><img alt="A basic React.js component using TypeScript." src="https://cdn-images-1.medium.com/max/1024/1*slVzU3lzlR_W-NxPu_zYeQ.png" /><figcaption>A generic React.js function component written using TypeScript. The attribute “name” set in React.js state can only be assigned as null or a string data-type. The button flips “name” between null and the defaultName variable that is assigned to the string “Trogdor!”</figcaption></figure><h4>TypeScript and useState():</h4><p>The useState() hook is one of the most commonly used hooks in React.js since it allows us to access instances of React state from within a function component. The recent shift away from a declarative programming paradigm in React to a more imperative approach is largely possible because we can now decouple React state from class-based components.</p><figure><img alt="implementing a TS interface within a React function component." src="https://cdn-images-1.medium.com/max/1024/1*qjMrnNCLQrWXUrpFwIJuUQ.png" /><figcaption>Here we declare a TS interface called IUserInfo. When useState is invoked, it establishes that userInfo can only accept a value of null, or an object that matches the attributes in IUserInfo.</figcaption></figure><figure><img alt="full implementation of useState with a typescript interface" src="https://cdn-images-1.medium.com/max/1024/1*SHYEN-CUkIeZOW5P_4pn9w.png" /><figcaption>Here we use the TS interface IUserInfo to determine that the object firstUser is a valid argument to be used with the useState() hook. The button swaps the value of userInfo between null, and the firstUser object.</figcaption></figure><h4>TypeScript and useEffect():</h4><p>The useEffect() hook handles side effects in React.js function components. It essentially takes the place of the componentDidMount and componentDidUpdate lifecycle methods found in class components.</p><figure><img alt="Implementing typescript with the useEffect hook." src="https://cdn-images-1.medium.com/max/1024/1*PuG9FTjq_Tzq9x-vIA6voA.png" /><figcaption>Using useEffect() to populate the React.js state variable apiData with an array after the first render. The dummyFetch function can just as easily make a fetch request to an external API to retrieve data. The benefit of this being, that you’ll get a big fat error if the structure of the incoming data doesn’t match the interface or data-type.</figcaption></figure><h4>More to come…. part 2:</h4><p>In the next blog we will take a look at how to use TypeScript with the useRef() and useReducer() hooks, which are a little trickier to implement.</p><h4>Sources:</h4><ul><li><a href="https://www.typescriptlang.org/docs/handbook/interfaces.html">Documentation - Object Types</a></li><li><a href="https://reactjs.org/">React</a></li></ul><p><a href="https://dev.to/bmcmahen/a-beginners-guide-to-using-typescript-with-react-7m6">https://dev.to/bmcmahen/a-beginners-guide-to-using-typescript-with-react-7m6</a></p><p><a href="https://levelup.gitconnected.com/usetypescript-a-complete-guide-to-react-hooks-and-typescript-db1858d1fb9c">https://levelup.gitconnected.com/usetypescript-a-complete-guide-to-react-hooks-and-typescript-db1858d1fb9c</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=319b3be83046" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Auth0 and React Hooks]]></title>
            <link>https://medium.com/@martin.crabtree/auth0-and-react-hooks-3a791e19e75f?source=rss-486964e11fac------2</link>
            <guid isPermaLink="false">https://medium.com/p/3a791e19e75f</guid>
            <dc:creator><![CDATA[Martin Crabtree]]></dc:creator>
            <pubDate>Wed, 11 Mar 2020 21:01:05 GMT</pubDate>
            <atom:updated>2020-03-11T21:01:05.200Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="A hook, but not a React hook." src="https://cdn-images-1.medium.com/max/1024/1*ce_WyH4CvW-mfggdR7-qNg.jpeg" /><figcaption>Not a React hook.</figcaption></figure><p>The multiple ways that you can handle user credentials in React.js can make implementing authorization/authentication (auth) a confusing process. In this blog we will look at a way to manage auth in React.js by creating a React context to house user credentials which can also be updated with hooks. There are many ways to handle authentication and authorization (auth), but Auth0 provides a fairly simple way to implement auth into your application, while also providing useful dashboard utilities.</p><h4>Auth0:</h4><p>The code below will require an Auth0 account, which can be found at <a href="https://auth0.com/">https://auth0.com/</a>. The account will allow you to create universal and embedded logins for your application.</p><h4>createContext() and useContext():</h4><p>The Context API is now fully supported with React.js, and allows you to create an object that can be accessed by any child component. This means that values stored in state can be universally accessed without creating a local state within a class component, or lifting state through props.</p><p>You can create a new context by using the createContext() function in React. This function creates an object that has two components: a Provider and a Consumer. When React renders a component that has access to this context, it will pull information from the closest matching Context.Provider in the tree. Context.Consumer on the other hand, allows a child function component to receive values from the parent Context.Provider and will return a React node.</p><figure><img alt="Creating a basic React Context" src="https://cdn-images-1.medium.com/max/976/1*qDfdJRWXfLSTzvlVADc4MQ.png" /><figcaption>The basic structure of a React context.</figcaption></figure><h4>Updating the Context with Hooks:</h4><p>React.js 16.8 introduced a series of useful predefined hooks that can be used to access and update the global state being stored in a React context. In this case we will use a Redux-like store context which will be created using createContext(), which will update the local React state by referencing an external reducer that is accessed using the useReducer() hook.</p><h4>useReducer():</h4><p>The useReducer() hook accepts two arguments, a Redux-like reducer function, and an initial state. The returned value of the hook can be destructured into state (current state), and a dispatch function that can be used to update state. More about setting and updating global state can be found in my previous blog: <a href="https://medium.com/@martin.crabtree/react-creating-a-redux-like-global-state-with-the-usecontext-and-usereducer-hooks-89aa2b27dbc5">React: Creating a ‘Redux-like’ Global State with the useContext and useReducer Hooks</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*D2K-wuJ5_AcrMeRDMGmKfA.png" /><figcaption>A basic store context that provides access to global state.</figcaption></figure><h4>Auth0 one way of many:</h4><p>Auth0 is by no means the only way to implement Auth in your application but it does provide a useful dashboard to implement and manage auth.</p><h4>Setting things up on the Auth0 site:</h4><p>In the site setup menu, select: Application Type: Single Page Web Applications.</p><p>Then, after clicking on the Create button, head to the Settings tab. There, you will need to add <a href="http://localhost:3000/?callback">http://localhost:3000/?callback</a> to the Allowed Callback URLs _ field and click on the Save Changes_ button. After doing so, leave this page open, to keep the information accessible.</p><p>In order to safely add authentication with Auth0 to your app, you need to install two npm packages. The first one is dotenv that is used to add local environment variables to your application. IMPORTANT: make sure that .env files are in your git-ignore filter. The other one is auth0-js, which is the official client-side JavaScript SDK from Auth0.</p><p>Create a .env file in the root of your project and include the following variables (using the information from the step above):</p><p><em>REACT_APP_AUTH0_DOMAIN=[ DOMAIN_FROM_AUTH0_SETUP ]</em></p><p><em>REACT_APP_AUTH0_CLIENT_ID=[CLIENT ID]</em></p><p>You will need to create a logic file containing the following code:</p><figure><img alt="auth0 logic file" src="https://cdn-images-1.medium.com/max/1024/1*ZlkVw-rGXgd4zMSC2wnMEQ.png" /><figcaption>Auth0 Logic File</figcaption></figure><p>Now you should have everything that you need to pass between your global state.</p><h4>Sources:</h4><p><a href="https://reactjs.org/docs/context.html">https://reactjs.org/docs/context.html</a></p><p><a href="https://auth0.com/blog/handling-authentication-in-react-with-context-and-hooks/">https://auth0.com/blog/handling-authentication-in-react-with-context-and-hooks/</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3a791e19e75f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[React: Creating an Interactive Canvas Component]]></title>
            <link>https://medium.com/@martin.crabtree/react-creating-an-interactive-canvas-component-e8e88243baf6?source=rss-486964e11fac------2</link>
            <guid isPermaLink="false">https://medium.com/p/e8e88243baf6</guid>
            <dc:creator><![CDATA[Martin Crabtree]]></dc:creator>
            <pubDate>Fri, 28 Feb 2020 22:23:23 GMT</pubDate>
            <atom:updated>2020-02-28T22:23:23.343Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/985/1*ZR2ibxGkF-qavDgx4See2Q.png" /></figure><p>The HTML5 canvas element behaves differently than standard HTML elements, especially when embedded in a React.js application. In this guide, we will take a look at how to set up a basic React.js application that incorporates an interactive HTML5 canvas element, while also compartmentalizing the logic using a custom useCanvas() hook.</p><h4>The Context Element of HTML5 Canvas:</h4><p>Have you ever wondered why you can’t just insert a canvas tag into some HTML code and pass it properties just like most other HTML elements? The reason is because the canvas element is somewhat unique, so when it is invoked, the browser creates a JavaScript object — known as context — that can interact with the HTML5 Canvas API. A context is unique to its parent element, and can be accessed by using the useContext() method.</p><p>When invoking the useContext() method, you have to pass an argument that specifies if it will be used for 2d or 3d (webgl) rendering. The 3d context is used for cases that require hardware accelerated graphics. While, the standard 2d context — which is used for most animations, data visualizations, and image manipulations — will be used in this demo.</p><h4>Giving Canvas a Home:</h4><p>For the purposes of this demo we will be rendering everything in the main App.js component, although the canvas element can be housed in any standard React.js component. The application will render the canvas element, and will draw a heart at the coordinates specified by an onClick() event listener.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/765/1*-K1oJxl5_5NylqnWU7F5Cg.png" /></figure><h4>Creating a useCanvas() Hook:</h4><p>React.js 16.8.0 introduced hooks which allows us to develop reusable functions that interact with state and lifecycle features within a function component, and also without relying on a class. In our case we will be creating a custom hook, that will also utilize some common hooks that are now offered natively in React.js ( useState, useEffect, useRef ).</p><p>The useCanvas() hook needs to: reference the context JavaScript object created by the canvas element; use the context reference to update properties in the Canvas API and then render the updated canvas; and update the context — in this case on mouse events.</p><h4>Referencing Context 2D:</h4><figure><img alt="code for referencing the canvas context(2d)" src="https://cdn-images-1.medium.com/max/734/1*wgtjk0noPZUwA40D02BWlw.png" /><figcaption>A reference to the canvas context(2d) object is assigned to the <strong>ctx</strong> variable that allows us to interact with properties in the HTML5 Canvas API.</figcaption></figure><h4>Updating Canvas API &amp; Rendering Canvas:</h4><figure><img alt="updating context before rendering the canvas" src="https://cdn-images-1.medium.com/max/740/1*whQPOq5rx_dbp11d2CV9dQ.png" /><figcaption>Using the ctx reference variable, we are able to set properties needed to render our canvas background and heart-shaped SVG in this specific instance of the canvas context.</figcaption></figure><h4>What to Draw:</h4><figure><img alt="SVG path for a heart" src="https://cdn-images-1.medium.com/max/719/1*MiHTOvXXsQaLQ7SGvC8wZg.png" /><figcaption>A path to our heart… SVG. Which is then saved to a SVG_PATH variable.</figcaption></figure><h4>Stateful Event Handling:</h4><figure><img alt="useState() saves local coordinate variables, and creates an update function" src="https://cdn-images-1.medium.com/max/572/1*E3tIlqk93Qp8dU8fVEMclg.png" /><figcaption>Utilizing the useState() hook to create a coordinates array in React.js state, that will add new coordinate pairs to the array with the setCoordinates() update function.</figcaption></figure><h4>The useCanvas() Function:</h4><figure><img alt="Final version of the useCanvas() hook, and it’s export variables." src="https://cdn-images-1.medium.com/max/760/1*jmxunueSJyx_8kNnWTU2xg.png" /><figcaption>The useCanvas() hook is ready for export! The useEffect() hook is used because we only want the reference to the canvas context to be initialized after the first rerender. Also we only want to trigger a rerender if the value of the coordinates array changes.</figcaption></figure><h4>Importing and Using the useCanvas() Hook:</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/793/1*Gzdz49b1EdPwvouZ6E-3FA.png" /><figcaption>Adding the useCanvas() hook to our React.js application.</figcaption></figure><h4>Event Handlers:</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/607/1*W6TUOZi4yyPvFqIEJG9V1g.png" /><figcaption>We have two event handlers: handleCanvasClick() will trigger with an onClick() event handler, and handleClearCanvas() will be tied to a button that will wipe the canvas clean by overwriting the coordinates array.</figcaption></figure><h4>Back to the App.js return():</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/664/1*Zr1PAsFmOPvl0ewkEasZYA.png" /><figcaption>Adding some styling, event handlers, and properties derived from the useCanvas() custom hook.</figcaption></figure><h4>The Result:</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/715/1*tQc61zPVEVr-gWMYy1-K3A.png" /><figcaption>It’s a bit late for Valentine’s Day, but I guess it’s the thought that counts?</figcaption></figure><p>To see and/or run the whole project, check out my <a href="https://github.com/TheMartinCrabtree/react-canvas-demo">react-canvas-demo</a> repository on GitHub.</p><h4>References:</h4><p><a href="https://itnext.io/using-react-hooks-with-canvas-f188d6e416c0">https://itnext.io/using-react-hooks-with-canvas-f188d6e416c0</a></p><p><a href="https://www.folio3.com/mobile/blog/how-to-make-use-of-html-5-canvas-with-react/">https://www.folio3.com/mobile/blog/how-to-make-use-of-html-5-canvas-with-react/</a></p><p><a href="https://www.html5canvastutorials.com/tutorials/html5-canvas-element/">https://www.html5canvastutorials.com/tutorials/html5-canvas-element/</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e8e88243baf6" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[React: The Basics of Data Visualization Using Recharts]]></title>
            <link>https://medium.com/@martin.crabtree/react-the-basics-of-data-visualization-using-recharts-14c01102efe3?source=rss-486964e11fac------2</link>
            <guid isPermaLink="false">https://medium.com/p/14c01102efe3</guid>
            <dc:creator><![CDATA[Martin Crabtree]]></dc:creator>
            <pubDate>Sun, 23 Feb 2020 03:30:59 GMT</pubDate>
            <atom:updated>2020-02-23T03:30:59.985Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="2019 NYC Demographic Data" src="https://cdn-images-1.medium.com/max/1024/1*eT5OnHrDyjnJQtDmbEtGow.png" /><figcaption>2019 NYC Demographic Data</figcaption></figure><h4>Pretty Graphs and Other Fancy Stuff</h4><p>With the upcoming election year, we can expect to be inundated with graphs and charts that depict all of the various polling and survey data that is generated over the course of the year. Nowadays the idea of visualizing and interacting with these data in real-time is fairly commonplace, however the concept would have been unheard of in the not-so-distant past.</p><p>Across the internet at large, there is a wealth of different data visualization packages that fits just about every imaginable niche out there. For the purposes of this blog, we will take a look at the Recharts (v1.8.5) data visualization library for React.js, which strikes a nice balance between being user/developer friendly, while offering enough flexibility to offer some real-world functionality. Of course, data visualization is pointless without some data, so in these examples I will be working with 2019 NYC Demographics data, that has been kindly provided to us by NYC Open Data.</p><h4>Recharts: A brief introduction.</h4><p>Recharts draws a lot of its functionality from the D3 data visualization JavaScript library (<a href="https://d3js.org/"><em>https://d3js.org/</em></a>), which is wrapped in React.js components. While some of the features and functionality of the D3 library is lost in Recharts, overall implementation is more simple and still offers a fairly robust suite of graphing/charting options. Like D3, Recharts also offers native implementation and manipulation of SVGs.</p><h4>Getting Started</h4><figure><img alt="Terminal: $ npm install recharts" src="https://cdn-images-1.medium.com/max/427/1*4NJ6FdWrVDUjyB4beVy0hA.png" /></figure><p>To add Recharts to an existing React.js application, simply pay a quick visit to your friendly local Node package manager (npm) by entering the command <strong><em>$ npm install recharts</em></strong> into your terminal. This will give you access to the Recharts library and all of the various components that will be required to render your data.</p><h4>Adding Recharts Components</h4><figure><img alt="import {LineChart, BarChart, Bar, Line, YAxis, CartesianGrid, XAxis, Label} from ‘recharts’;" src="https://cdn-images-1.medium.com/max/1024/1*w_1Ge2wlWE-wQdiaKR6L1g.png" /><figcaption>importing basic Rechart components</figcaption></figure><p>As with any React.js application, a reference to the Recharts library needs to be added in each component. The &lt;ResponsiveContainer /&gt; component is a wrapper that allows the chart to match the dimensions of the parent container (just make sure that the parent has a specified width and height).</p><p>In order to render your data, you must specify a chart type (<em>eg. &lt;LineChart /&gt;</em>), and corresponding visualization methods (<em>eg. &lt;Line /&gt;</em>) which is passed into the chart type as child components. Each chart type can accept multiple data visualization components, which allows you to display multiple lines or bars. There is also a &lt;ComposedChart /&gt; component that can render multiple visualisation types (<em>eg. overlaid bar, line, and area charts</em>).</p><h4>Types of charts supported in Recharts:</h4><figure><img alt="&lt;ComposedChart /&gt;, &lt;AreaChart /&gt;, &lt;BarChart /&gt;, &lt;LineChart /&gt;, &lt;PieChart /&gt;, &lt;RadarChart /&gt;, &lt;RadialBarChart /&gt;, &lt;ScatterChar" src="https://cdn-images-1.medium.com/max/881/1*tkZ0sm4oArViHWjOZOWHRg.png" /></figure><h4>Types of visualization methods supported in Recharts:</h4><figure><img alt="Cartesian: &lt;Area /&gt;, &lt;Bar /&gt;, &lt;Line /&gt;, &lt;Scatter /&gt;, &lt;Funnel /&gt; Polar: &lt;Pie /&gt;, &lt;Radar /&gt;, &lt;RadialBar /&gt;" src="https://cdn-images-1.medium.com/max/940/1*F5TseOjXRag1njGaWt4iig.png" /></figure><p>Of course, random data points without context aren’t very meaningful, so Recharts provides a standard Cartasian grid and Polar layouts. Other layout useful components include: &lt;Legend /&gt;, &lt;XAxis /&gt;, &lt;YAxis /&gt;, &lt;Tooltip /&gt;, &lt;Label /&gt;, &lt;ErrorBar /&gt;, and &lt;Text /&gt;.</p><p>Linking the Rechart components to your dataset is fairly straight forward. The parent chart type, accepts a standard dataset array of objects comprising of key/value pairs (eg. <em>[{ key1: value1, key2: value2}]</em> ). In turn, each visualization component that you pass to the parent chart component has a dataKey value in props, which specifies which key/value pair should be used from the parent component’s dataset.</p><h4>Putting it all Together:</h4><figure><img alt="Line chart component that displays 2019 NYC demographic data for male and female responses." src="https://cdn-images-1.medium.com/max/1024/1*2mRUaZVGXAxRDRnI_KyW-w.png" /><figcaption>Using the &lt;LineChart /&gt; component to display Male and Female respondents (by zipcode) in the 2019 NYC Demographic survey data.</figcaption></figure><figure><img alt="Graph displaying 2019 NYC Demographic Survey male/female respondents in the Bronx Park and Fordham neighborhood." src="https://cdn-images-1.medium.com/max/840/1*Zjz9zUIZW1lCalHrGlzo8g.png" /><figcaption>The result: Using Recharts to display 2019 NYC Demographic Survey male/female respondents in the Bronx Park and Fordham neighborhood (separated by corresponding zip code).</figcaption></figure><p>For more details on the specific properties in each rechart component check out the Recharts API (<a href="http://recharts.org/en-US/api/Pie">http://recharts.org/en-US/api</a>).</p><h4>References:</h4><p><a href="http://recharts.org/en-US/">http://recharts.org/en-US/</a></p><p><a href="https://github.com/recharts/recharts/">https://github.com/recharts/recharts/</a></p><p><a href="https://data.cityofnewyork.us/City-Government/Demographic-Statistics-By-Zip-Code/kku6-nxdu">https://data.cityofnewyork.us/City-Government/Demographic-Statistics-By-Zip-Code/kku6-nxdu</a></p><p><a href="https://www.freecodecamp.org/news/how-to-get-started-with-d3-and-react-c7da74a5bd9f/">https://www.freecodecamp.org/news/how-to-get-started-with-d3-and-react-c7da74a5bd9f/</a></p><p><a href="https://github.com/d3/d3/wiki/Tutorials">https://github.com/d3/d3/wiki/Tutorials</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=14c01102efe3" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[React.js: Using Children Props]]></title>
            <link>https://medium.com/@martin.crabtree/react-js-using-children-props-c83d5b259756?source=rss-486964e11fac------2</link>
            <guid isPermaLink="false">https://medium.com/p/c83d5b259756</guid>
            <dc:creator><![CDATA[Martin Crabtree]]></dc:creator>
            <pubDate>Fri, 14 Feb 2020 03:23:26 GMT</pubDate>
            <atom:updated>2020-02-14T03:23:26.583Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="A picture of a family, for talking about parents and children in React.js" src="https://cdn-images-1.medium.com/max/655/1*IXl1my9kkzuO20lBbbM-WA.png" /></figure><p>The concept of <strong>children props ( written as {props.children})</strong> is far less twisted than it sounds when placed within the context of React.js code. The { props.children } property allows you to create a generic template component that can be modified by the parent when it is invoked. This means that a parent component can pass whatever is needed in the child component, even generated layout features that can then be rendered by the child.</p><h4>The Props and Child: A Tragic Story</h4><p>Once upon a time there was a component that wanted to pass information down to its grandchildren, and any subsequent children in the application hierarchy. This worked well until the family started getting really big and the parent component noticed that many of the children were nearly identical and only needed slightly different information to be passed down through props.</p><p>The most basic implementation of props is to define props in the parent component and pass it down to the next child component in the hierarchy, and then pass the value again through props in the child component, and then again through the grandchild — which is repeated as needed until the passed value arrives in the target component. The process is tedious, error prone, and also makes the code less flexible.</p><h4>{ props.children }: A Better Way</h4><p>Implementation is pretty simple. In the parent component, import the child component, but instead of invoking it with a self-closing tag, use an standard open/closing tag. The information that you want to pass through props — in addition to the standard implementation of passing props — is placed between the opening and closing tags of the child component.</p><figure><img alt="Demo code for a parent component that will utilize { props.children }" src="https://cdn-images-1.medium.com/max/1024/1*ICRn2_JRF5pOrNNWUUeEYw.png" /><figcaption><strong>Implementing &lt;ParentComponent /&gt;</strong>: The &lt;ParentComponent /&gt; defines a paragraph that will be passed down to the &lt;ChildComponent /&gt; as props.</figcaption></figure><figure><img alt="Showing the implementation of { props.children } in a the ChildComponent." src="https://cdn-images-1.medium.com/max/986/1*41oRbOYSa3vtRZpBHfgYDQ.png" /><figcaption><strong>Implementing &lt;ChildComponent /&gt;</strong>: In the ChildComponent. { props.children } will render any data that has been passed down from the parent, allowing the ParentComponent to customize the contents of the ChildComponent.</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/661/1*JlqT6BqkxO8d20EfIYUacg.png" /><figcaption>Here we see that the result of content being passed to a child from the parent component, that is then rendered in the child.</figcaption></figure><h4>Sources:</h4><p><a href="https://stackoverflow.com/questions/49706823/what-is-this-props-children-and-when-you-should-use-it">https://stackoverflow.com/questions/49706823/what-is-this-props-children-and-when-you-should-use-it</a></p><p><a href="https://reactjs.org/docs/composition-vs-inheritance.html">https://reactjs.org/docs/composition-vs-inheritance.html</a></p><p><a href="https://daveceddia.com/pluggable-slots-in-react-components/?utm_campaign=0731slots">https://daveceddia.com/pluggable-slots-in-react-components/?utm_campaign=0731slots</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c83d5b259756" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[React: Creating a ‘Redux-like’ Global State with the useContext and useReducer Hooks]]></title>
            <link>https://medium.com/@martin.crabtree/react-creating-a-redux-like-global-state-with-the-usecontext-and-usereducer-hooks-89aa2b27dbc5?source=rss-486964e11fac------2</link>
            <guid isPermaLink="false">https://medium.com/p/89aa2b27dbc5</guid>
            <dc:creator><![CDATA[Martin Crabtree]]></dc:creator>
            <pubDate>Thu, 13 Feb 2020 03:35:58 GMT</pubDate>
            <atom:updated>2020-02-13T03:35:58.787Z</atom:updated>
            <content:encoded><![CDATA[<p>The introduction of the useContext and useReducer hooks into the React javascript library addresses some of the primary frustrations associated with managing state. For many, this is managing multiple instances of state, and passing information through props to child-components. As an application grows scale and complexity, the process of managing and passing values from state through child-components becomes more complex and error prone, especially if there are significant changes to the application’s architecture.</p><h4>Enter Redux</h4><p>Redux was developed by Dan Abramov and Andrew Clark in order to centralize state, and to make the process of manipulating state more consistent. Central to the Redux architecture is the Store, which serves as the single source of truth for an applications state. Manipulating state is handled through the use of Reducers, which are pure functions that carry out an action and then return an updated version of state.</p><h4>React 16.8.0 Create Your Own Store and Reducers</h4><p>As of React v16.8.0, we now have access to the useContext and useReducer hooks. For our purposes, the primary benefit of the useContext hook is the ability to reference a Redux-like Store that is house within a global context object that can be created using React.createContext.</p><h4>useReducer(reducer, initialArg, init)</h4><p>The useReducer hook accepts two arguments, a reducer function, and an initial value for state.</p><p>A reducer function must be able to accept two arguments; the initial value of state, and an action. The action has two properties; the type of action to take, and a payload of data that should be used to update state. The return value of the reducer function is the updated value of state.</p><p>The return value of useReducer is the current value of state, along with a dispatch method for the reducer. Invoking the dispatch function requires an object to be passed that specifies the type of action to be executed by the reducer function, and (if needed) a payload that contains any values that the reducer requires to update state.</p><h4>Putting it all together:</h4><ol><li>Create a basic reducer for use with useReducer()</li></ol><figure><img alt="Code for creating a simple React reducer." src="https://cdn-images-1.medium.com/max/718/1*0M0Uk8TdnegvuuG2NFHuRA.png" /><figcaption>A basic reducer that updates the username property in the global scope. It accepts two arguments: the current global state; and an action (with the sub properties of type and payload).</figcaption></figure><p>2. Create a store that creates a context object that houses a global state. The store acts as a wrapper and propagates the global state to all of the child components. The state and dispatch properties are provided by useReducer(). Dispatch accesses the reducer, and updates global state, which is then returned in the state property (returned from useReducer).</p><figure><img alt="A basic react store using createContext(), and useReducer()." src="https://cdn-images-1.medium.com/max/739/1*w699P_5W9ZgJYAlw0W7FSw.png" /><figcaption>A basic store React component, created using the createContext() function. This provides all child components with access to a global instance of state, which can be updated using a dispatch function provided by useReducer().</figcaption></figure><p>3. Adding the store to React web application.</p><figure><img alt="Adding a react store component to a web application (in this case within App.js)." src="https://cdn-images-1.medium.com/max/771/1*Ge9dHHlPeqilTmk627pHVw.png" /><figcaption>Importing the Store component into App.js, which wraps around all of the child components in the web application.</figcaption></figure><p>4. Implementing global state in your React application.</p><figure><img alt="Accessing and updating the global state from inside a react component." src="https://cdn-images-1.medium.com/max/1024/1*1_fjQ9gqhjce2PvJenLO9g.png" /><figcaption>useContext() provides a dispatch function that has been passed down from the Store component. The dispatch function accepts two arguments, a type of action, and a payload for updating the global state. The useContext() function will then return an updated global state.</figcaption></figure><h4>Sources</h4><p><a href="https://reactjs.org/">https://reactjs.org/</a></p><p><a href="https://redux.js.org/">https://redux.js.org/</a></p><p><a href="https://www.robinwieruch.de/react-state-usereducer-usestate-usecontext">https://www.robinwieruch.de/react-state-usereducer-usestate-usecontext</a></p><p><a href="https://en.wikipedia.org/wiki/Redux_(JavaScript_library)">https://en.wikipedia.org/wiki/Redux_(JavaScript_library)</a></p><p><a href="https://github.com/facebook/react">https://github.com/facebook/react</a></p><p><a href="https://www.w3schools.com/react/default.asp">https://www.w3schools.com/react/default.asp</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=89aa2b27dbc5" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>