{"componentChunkName":"component---src-templates-legacy-tutorial-page-js","path":"/oa/tutorials/react-redux-passwords-app-tutorial-oh4/react-redux-timers-create-new-timers/","result":{"pageContext":{"tutorial":{"id":"T0E6OlR1dG9yaWFsLTE5MQ==","slug":"react-redux-passwords-app-tutorial-oh4","title":"React Redux Timers App Tutorial","previewText":"This tutorial is an introduction to React and Redux.\nThe goal is to create a small app that generates and\nstores a list of timers.\n","heroImagePath":"https://raw.githubusercontent.com/MakeSchool-Tutorials/web-7-react-redux-timers-app/master/cover.png","heroImageFile":{"childImageSharp":{"gatsbyImageData":{"layout":"constrained","placeholder":{"fallback":"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAACXBIWXMAAAsTAAALEwEAmpwYAAAD6ElEQVQ4y42RX0xcRRTGLwu+YKxGk1ZD60N3Gl7qCwoxmmqkaGO0aXzQxNYHHzSxPmliNBqNYrVWU+auRYnWSA1SoYFuNPhQ2vLn3pUWUigiKBQKhYXddZe1bFl22b33zvnMzN1rtm2aOMnkzJw553e+c0YDkZZzRAkAuTVB9AkRrViOiIAoBhRvxIgoZjkiCqKkI8SrMgdA6T9ZSyMi7ToYgE+JCAAhmbVRP7iMdwcSeG8ggZe6w9jdNY83jSiylgMSQsUBeKUYqhXDImkLx/5MOQAJgKh/MUN1wUV65MQ83fbVBG1pnqILsYyqmM7ZzuHQHK6tW9dBNQ+2mnew71TUecuMq7JZy1H2YnwdO0+G8UDrNGaurksXcrYg2cmO7wbFC22jcJRaF6pg0TUL+3v/FjXtC/Th+WX5SN/8kUTH9IoChJbWMJbIqnPrZAq/RTIqZk/rCG6vPy1e7hzD1UxeQSUw8svsKh4+sWDVBRfx/rll1dLBC3GUN02gL5yGt7rm0jIOxpILfPbHYdr8RS/uOnAm1z+XlL5+zXKoGsBqy2QKVT/N2x+ccxXqo8vkaxzH5uZJ9ITT6F7IoC4YxmMdYalQFX2mZRgbPj5tdYxHZc5Mat26vzBDF/r12AreMOK2fG0YSVB50wTdc/QvbG+Zws72GezuiuLxjgVygYQnvh+yjo0syvDLa3m7wpthWeFjqgXR6nBczcoOjC6jtHGcqtrm6KnOK9imm6htn6ba4BINqBkKazCsZnw574iKAqPM+2UFJZJKSQ3t0HDC3tJ8Cc/9GqFdJ+eJcYMqdYOqW6bQv5S1CmOdXcs7FcUMzXEcr23lSGYd1f7P7kfZe7oieLLzClUGTNp6uA/3ft5njcbWlLKpZKbiBkGqZQUVQkiHehDCnem34yvY0RG2nw7Oq5Y3HuyxWkeXFOxazlUmcyRIbqVQLnnxoJ5SOVMA6aMTKdQcn7XvO9TrwWZzDv2nzIMpdcXLgxa3AHKVNo4k8MPFiFKWyORvavMmWDHUtm+Agh4CkAJwKWs5/x92C2iptFlbbF3JWpuKfbeEMW74GDel1Rg3S+T9bApa+Ufd2oYDZ3zHf4+U5QsFmgYXyk5NJ7TKgOlTeYEBmVfq50Ypc7fPA8ntY3rIvQeGpC3xc0Or1E2tvnemTHvwiDpv01WiG/fleWX93p0bUqG5l+mhOxVMD93NuPkiOzJUUii0l3HzUVVQD21k3HzeTTZrGTdeY4GBOxg33mHc+IxxQ5e5khpj3GjzN/TJc5BxI+Jv6JGATYW3s37eL+9V8sy4uY9xY4Rxs4bpoe2MG3N+brzu58bbjBu7/gWOSk/VhPcKJgAAAABJRU5ErkJggg=="},"images":{"fallback":{"src":"/mediabook/static/a69ceb219f4e17b4f09e707a84068da8/5aead/cover.png","srcSet":"/mediabook/static/a69ceb219f4e17b4f09e707a84068da8/e9fba/cover.png 50w,\n/mediabook/static/a69ceb219f4e17b4f09e707a84068da8/15e42/cover.png 100w,\n/mediabook/static/a69ceb219f4e17b4f09e707a84068da8/5aead/cover.png 200w,\n/mediabook/static/a69ceb219f4e17b4f09e707a84068da8/d6138/cover.png 400w","sizes":"(min-width: 200px) 200px, 100vw"},"sources":[{"srcSet":"/mediabook/static/a69ceb219f4e17b4f09e707a84068da8/b79cb/cover.avif 50w,\n/mediabook/static/a69ceb219f4e17b4f09e707a84068da8/6d0de/cover.avif 100w,\n/mediabook/static/a69ceb219f4e17b4f09e707a84068da8/f2685/cover.avif 200w,\n/mediabook/static/a69ceb219f4e17b4f09e707a84068da8/4ff31/cover.avif 400w","type":"image/avif","sizes":"(min-width: 200px) 200px, 100vw"},{"srcSet":"/mediabook/static/a69ceb219f4e17b4f09e707a84068da8/dbc4a/cover.webp 50w,\n/mediabook/static/a69ceb219f4e17b4f09e707a84068da8/d8057/cover.webp 100w,\n/mediabook/static/a69ceb219f4e17b4f09e707a84068da8/2e34e/cover.webp 200w,\n/mediabook/static/a69ceb219f4e17b4f09e707a84068da8/416c3/cover.webp 400w","type":"image/webp","sizes":"(min-width: 200px) 200px, 100vw"}]},"width":200,"height":200}}},"pages":{"nodes":[{"id":"T0E6OlBhZ2UtMjI1Mw==","title":"Timers - Introduction","slug":"react-redux-timers-introduction","sections":{"nodes":[{"id":"T0E6OlNlY3Rpb24tMTE0NTY=","title":"Timers - Introduction","htmlContent":"<p>The goal of the project is to create a web app that stores a list of timers.\nTimers will keep track of elapsed time. It could be used to track time for billing clients, scientific processes, or other uses. Through building this app, you will learn how to use React to manage views, and Redux to manage application state.</p><p>While not a complete product, this app has many features that allow you to learn more about JavaScript, React and Redux. It also provides an opportunity to learn more about vanilla JavaScript and takes a close look at the Date Object.</p><p>The app created in this tutorial could be the basis of a real world\nproduct like <a href=\"http://toggl.com\" target=\"_blank\">Toggl.com</a>!</p>"},{"id":"T0E6OlNlY3Rpb24tMTE0NTc=","title":"Why is this important?","htmlContent":"<p>This gives practice in building web apps using React and getting familiar with the JSX syntax. As you go throughout this course, we'll be building more complex apps, so it's good to have the fundamentals down, which you'll learn in this tutorial!</p>"},{"id":"T0E6OlNlY3Rpb24tMTE0NTg=","title":"Prerequisites","htmlContent":"<p>To follow this tutorial, you should have...</p><ul>\n<li>A basic understanding of JavaScript.</li>\n<li>Familiarity with React and JSX.</li>\n<li>Some understanding of the ES6 version of JavaScript</li>\n</ul>"},{"id":"T0E6OlNlY3Rpb24tMTE0NTk=","title":"Learning Outcomes","htmlContent":"<p>By the end of this tutorial, you should be able to...</p><ol>\n<li>Build React components using JSX syntax</li>\n<li>Manage application state using the Flux pattern</li>\n<li>Use timing methods and objects in JavaScript to accurately track time</li>\n<li>Use the BEM system to create class names for styling</li>\n<li>Define class objects and utilize the OOP paradigm</li>\n</ol>"},{"id":"T0E6OlNlY3Rpb24tMTE0NjA=","title":"Technical Planning","htmlContent":"<ol>\n<li>Build a Timer object</li>\n<li>Define the Actions of a Timer</li>\n<li>Define the Reducers of a Timer</li>\n<li>Allow users to create a Timer</li>\n<li>Allow users to see a list of Timers</li>\n<li>Users should be able to start/stop the clock on their Timers</li>\n<li>Style the app</li>\n<li>Allow Timers to persist</li>\n</ol>"},{"id":"T0E6OlNlY3Rpb24tMTE0NjE=","title":"Using Git/GitHub","htmlContent":"<p>Much like we've done in earlier tutorials, make sure you're committing your code as you complete milestones. At a minimum, you should make a commit whenever the tutorial prompts you.</p>"},{"id":"T0E6OlNlY3Rpb24tMTE0NjI=","title":"Set Up Git/GitHub","htmlContent":"<p>Set up your repo!</p><div class=\"action\">\n<p>\nMake your first commit</p>\n<pre>$ git init\n$ git add .\n$ git commit -m <span class=\"s1\">'project init'</span>\n</pre>\n</div><p>Now go to GitHub and create a public repository, and now associate it as a remote for your local git project and then push to it.</p><div class=\"action\">\n<p>\nPush it!</p>\n<pre>$ git remote add origin GITHUB-REPO-URL\n$ git push origin main -u\n</pre>\n</div>"}]},"next":{"id":"T0E6OlBhZ2UtMjI1NA==","slug":"react-redux-timers-getting-started","title":"Getting Started with React and Redux"},"previous":{"id":"T0E6OlBhZ2UtMjI1NA==","slug":"react-redux-timers-getting-started","title":"Getting Started with React and Redux"}},{"id":"T0E6OlBhZ2UtMjI1NA==","title":"Getting Started with React and Redux","slug":"react-redux-timers-getting-started","sections":{"nodes":[{"id":"T0E6OlNlY3Rpb24tMTE0NjM=","title":"Getting Started with React and Redux","htmlContent":"<p>The goal of this section is to install the tools you will need to complete the\ntutorial.</p>"},{"id":"T0E6OlNlY3Rpb24tMTE0NjQ=","title":"Editor","htmlContent":"<p>You will need a code editor of some kind. I will recommend using <a href=\"https://code.visualstudio.com/download\" target=\"_blank\">VS Code</a> or <a href=\"https://atom.io\" target=\"_blank\">Atom</a>.</p><div class=\"action\">\n<p></p>\n\n<p>Download one of the above editors (or another of your choice) to use in the tutorial</p>\n</div>"},{"id":"T0E6OlNlY3Rpb24tMTE0NjU=","title":"Command Line Tools NPM","htmlContent":"<p>Node package Manager is a tool for managing file dependancies.</p><div class=\"action\">\n<p></p>\n\n<p>Follow the instructions <a href=\"https://www.npmjs.com/package/npm\" target=\"_blank\">here</a> and install the version for your computer system.</p>\n</div>"},{"id":"T0E6OlNlY3Rpb24tMTE0NjY=","title":"Create the boilerplate React App","htmlContent":"<p>The first step is to create a boiler plate react app. React provides\na command line tool: <code>Create React App</code> that will do this for you.</p><div class=\"info\">\n<p></p>\n\n<p>For more background on the <code>Create React App</code> tool, you can read about it <a href=\"https://github.com/facebook/create-react-app\" target=\"_blank\">here</a>.</p>\n</div><!-- --><div class=\"action\">\n<p></p>\n\n<p>Run the commands below to set up your boiler plate react app:</p>\n<pre>$ npx create-react-app tmrz-app\n$ <span class=\"nb\">cd</span> tmrz-app\n$ yarn start\n</pre>\n</div><p><strong>Note:</strong> The first command above may take a few seconds to install all of file\ndependancies. The last command may take a few seconds to start the app for the first time.</p><p>When the the above commands are complete, a tab pointed to <code>localhost</code> should open in your browser and it should look like the below:</p><p><a href=\"https://cdn.jsdelivr.net/gh/MakeSchool-Tutorials/web-7-react-redux-timers-app@549335c706f2ac39b491d59065d65320e7abb511/P01-Getting-Started/assets/react-app.png\" target=\"_blank\">\n          <img src=\"https://cdn.jsdelivr.net/gh/MakeSchool-Tutorials/web-7-react-redux-timers-app@549335c706f2ac39b491d59065d65320e7abb511/P01-Getting-Started/assets/react-app.png\" alt=\"react-app\" title=\"\">\n        </a></p><p>The project is now running on <code>localhost:3000</code>. This local server is\nfor development.</p><p>Editing files in the project should trigger the local server to update.\nIn this way you can see the latest changes immediately in the browser.</p>"},{"id":"T0E6OlNlY3Rpb24tMTE0Njc=","title":"Tour the React App source code","htmlContent":"<p>The boilerplate project contains a few files that are arranged in\nfolders:</p><ul>\n<li>\n<strong><code>src</code></strong>: You'll do your work in the <code>src</code> folder. <em>This folder contains\nall of the React Components.</em>\n\n<ul>\n<li><strong>All of the code you create must be stored in the 'src' folder.</strong></li>\n<li>\n<strong><code>index.js</code>:</strong> defines <code>ReactDOM.render()</code>. This loads and displays the <code>App</code> component. <em>This is the entry point for a react app. You won't need to edit this file.</em>\n</li>\n<li>\n<strong><code>app.js</code>:</strong> contains the root component for the app. At the moment this file defines everything you see when you run the default app.</li>\n</ul>\n</li>\n</ul><p>You will break your app into components and import these components into the <code>App</code> component. Each new component will be defined in a new file in the <code>src</code> directory.</p>"},{"id":"T0E6OlNlY3Rpb24tMTE0Njg=","title":"Redux","htmlContent":"<p><a href=\"https://redux.js.org/\" target=\"_blank\">Redux</a> is a tool for managing application state. Redux is based on the <a href=\"https://facebook.github.io/flux/\" target=\"_blank\">Flux</a> pattern developed by Facebook, which imposes a unidirectional data flow.</p><p><em>Redux is a JavaScript implementation of Flux,</em> and while Redux stands on it's own, it is often used with React.</p><p>This happens so often, that there now exists a library that integrates Redux with React called <strong>React-Redux</strong>.</p><h2>Import, import, import</h2><p>Import Redux and React-Redux.</p><div class=\"action\">\n<p></p>\n\n<p>Run the following to import redux and react-redux:</p>\n<pre>$ npm i --save redux\n$ npm i --save react-redux\n</pre>\n</div><h2>Resources</h2><ul>\n<li>https://github.com/facebook/create-react-app</li>\n</ul>"},{"id":"T0E6OlNlY3Rpb24tMTE0Njk=","title":"Now Commit","htmlContent":"<div class=\"action\">\n<p></p>\n<pre>$ git add .\n$ git commit -m <span class=\"s1\">'create react app with redux and react-redux'</span>\n$ git push\n</pre>\n</div>"}]},"next":{"id":"T0E6OlBhZ2UtMjI1NQ==","slug":"react-redux-timers-timer-object","title":"Timer Object"},"previous":{"id":"T0E6OlBhZ2UtMjI1NQ==","slug":"react-redux-timers-timer-object","title":"Timer Object"}},{"id":"T0E6OlBhZ2UtMjI1NQ==","title":"Timer Object","slug":"react-redux-timers-timer-object","sections":{"nodes":[{"id":"T0E6OlNlY3Rpb24tMTE0NzA=","title":"Timer Object","htmlContent":"<h2>Technical Planning</h2><ol>\n<li>\n<strong>Build a Timer object</strong>\n\n<ol>\n<li>Create a Timer Class</li>\n<li>Assign name, time, and isRunning properties to the class</li>\n</ol>\n</li>\n<li>Define the Actions of a Timer</li>\n<li>Define the Reducers of a Timer</li>\n<li>Allow users to create a Timer</li>\n<li>Allow users to see a list of Timers</li>\n<li>Users should be able to start/stop the clock on their Timers</li>\n<li>Style the app</li>\n<li>Allow Timers to persist</li>\n</ol><h2>Defining the Timer Object</h2><p>The app will create and store Objects that define a timer. Timer objects will have the following properties that will define it:</p><ul>\n<li>\n<strong>name:</strong> String - the name of the timer</li>\n<li>\n<strong>time:</strong> Number - the time in milliseconds</li>\n<li>\n<strong>isRunning:</strong> Boolean - tracks whether the timer is running or paused</li>\n</ul><p>You can add more properties to expand the functionality of the app. For example you might have <code>billable</code>, and rate if the app was used for business.</p><p>JavaScript only has one <code>Object</code> type. It has many ways to create objects. For this tutorial we will be using the class syntax. This will follow the same coding style used for creating React components.</p>"},{"id":"T0E6OlNlY3Rpb24tMTE0NzE=","title":"Creating Classes","htmlContent":"<p>You can define a class using the <code>class</code> keyword. <em>Class names by convention\nalways start with an uppercase letter.</em> You can initialize the properties of a class in it's constructor function.</p><div class=\"action\">\n<p></p>\n\n<p>In your <code>src</code> folder, create a <code>Timer.js</code> file and write the <code>Timer</code> class with the following properties:</p>\n<pre><span class=\"kr\">class</span> <span class=\"nx\">Timer</span> <span class=\"p\">{</span>\n  <span class=\"c1\">// The name property is passed into the constructor and the class is initialized.</span>\n  <span class=\"nx\">constructor</span><span class=\"p\">(</span><span class=\"nx\">name</span><span class=\"p\">)</span> <span class=\"p\">{</span>\n    <span class=\"k\">this</span><span class=\"p\">.</span><span class=\"nx\">name</span> <span class=\"o\">=</span> <span class=\"nx\">name</span><span class=\"p\">;</span>\n    <span class=\"k\">this</span><span class=\"p\">.</span><span class=\"nx\">time</span> <span class=\"o\">=</span> <span class=\"mi\">0</span><span class=\"p\">;</span>\n    <span class=\"k\">this</span><span class=\"p\">.</span><span class=\"nx\">isRunning</span> <span class=\"o\">=</span> <span class=\"kc\">false</span><span class=\"p\">;</span>\n  <span class=\"p\">}</span>\n<span class=\"p\">}</span>\n</pre>\n</div><p>export default Timer;\n```</p><p>Below is an example on how to create an instance of <code>Timer</code>:</p><pre><span class=\"kd\">var</span> <span class=\"nx\">myTimer</span> <span class=\"o\">=</span> <span class=\"k\">new</span> <span class=\"nx\">Timer</span><span class=\"p\">(</span><span class=\"s1\">'Workout'</span><span class=\"p\">);</span>\n</pre><p>Great work, this is just the beginning of <strong>defining class objects to utilize in an OOP paradigm!</strong> We'll build upon this throughout the tutorial.</p><h2>Resources</h2><ul>\n<li>https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes</li>\n</ul>"},{"id":"T0E6OlNlY3Rpb24tMTE0NzI=","title":"Now Commit","htmlContent":"<div class=\"action\">\n<p></p>\n<pre>$ git add .\n$ git commit -m <span class=\"s1\">'added Timer class'</span>\n$ git push\n</pre>\n</div>"}]},"next":{"id":"T0E6OlBhZ2UtMjI1Ng==","slug":"react-redux-timers-defining-actions","title":"React Redux Timers - Defining Actions"},"previous":{"id":"T0E6OlBhZ2UtMjI1Ng==","slug":"react-redux-timers-defining-actions","title":"React Redux Timers - Defining Actions"}},{"id":"T0E6OlBhZ2UtMjI1Ng==","title":"React Redux Timers - Defining Actions","slug":"react-redux-timers-defining-actions","sections":{"nodes":[{"id":"T0E6OlNlY3Rpb24tMTE0NzM=","title":"React Redux Timers - Defining Actions","htmlContent":"<h2>Technical Planning</h2><ol>\n<li><del>Build a Timer object</del></li>\n<li>\n<strong>Define the Actions of a Timer</strong>\n\n<ol>\n<li>Review how actions and action creators work in Flux and Redux</li>\n<li>Define what we want a Timer to be able to do</li>\n<li>For each action, define a Redux action and action creator</li>\n</ol>\n</li>\n<li>Define the Reducers of a Timer</li>\n<li>Allow users to create a Timer</li>\n<li>Allow users to see a list of Timers</li>\n<li>Users should be able to start/stop the clock on their Timers</li>\n<li>Style the app</li>\n<li>Allow Timers to persist</li>\n</ol><h2>Defining Actions</h2><p>The Flux pattern and Redux require that\nyou define <strong>actions</strong> that describe how data can be modified. A few key points around actions:</p><ol>\n<li><strong>Data can only be modified through actions.</strong></li>\n<li>Each action should be defined as a <code>const</code> with string value.</li>\n<li>Each action needs an <strong>action creator</strong>\n</li>\n</ol><p>But wait, we just covered actions, what's an action creator? An action creator is a <em>function that returns an object with a <strong>type</strong>, and a <strong>payload</strong> when a value needs to accompany the action.</em></p><p>For example, a <code>SELECT_BOOK</code> action creator might look like this:</p><pre><span class=\"kr\">export</span> <span class=\"kr\">const</span> <span class=\"nx\">selectBook</span> <span class=\"o\">=</span> <span class=\"p\">(</span><span class=\"nx\">index</span><span class=\"p\">)</span> <span class=\"p\">=&gt;</span> <span class=\"p\">{</span>\n  <span class=\"k\">return</span> <span class=\"p\">{</span>\n    <span class=\"nx\">type</span><span class=\"o\">:</span> <span class=\"nx\">SELECT_BOOK</span><span class=\"p\">,</span>\n    <span class=\"nx\">payload</span><span class=\"o\">:</span> <span class=\"p\">{</span> <span class=\"nx\">index</span> <span class=\"p\">}</span>\n  <span class=\"p\">}</span>\n<span class=\"p\">}</span>\n</pre>"},{"id":"T0E6OlNlY3Rpb24tMTE0NzQ=","title":"Timer Actions","htmlContent":"<p>Let's start by defining the actions before making the app!</p><p>The app needs to do the following things with timers:</p><ul>\n<li>Add a new timer</li>\n<li>Toggle a timer (start/stop)</li>\n<li>Select a timer</li>\n</ul><p>First we need a place to store all these actions:</p><div class=\"action\">\n<p></p>\n\n<p>Create the folder <code>src/actions</code>, and then add this file: <code>src/actions/index.js</code>.</p>\n</div><p>All actions are going to be defined within <code>src/actions/index.js</code>.</p><p>Let's build each of these out, action by action:</p>"},{"id":"T0E6OlNlY3Rpb24tMTE0NzU=","title":"Add Timer","htmlContent":"<p><code>ADD_TIMER</code> - Creating a timer will add a new timer object to an array of timer objects held by the store.</p><div class=\"action\">\n<p></p>\n\n<p>Add the following to <code>src/actions/index.js</code>:</p>\n<pre><span class=\"kr\">export</span> <span class=\"kr\">const</span> <span class=\"nx\">NEW_TIMER</span> <span class=\"o\">=</span> <span class=\"s1\">'NEW_TIMER'</span>\n<span class=\"kr\">export</span> <span class=\"kr\">const</span> <span class=\"nx\">addTimer</span> <span class=\"o\">=</span> <span class=\"p\">(</span><span class=\"nx\">name</span><span class=\"p\">)</span> <span class=\"p\">=&gt;</span> <span class=\"p\">{</span>\n  <span class=\"k\">return</span> <span class=\"p\">{</span>\n    <span class=\"nx\">type</span><span class=\"o\">:</span> <span class=\"nx\">NEW_TIMER</span><span class=\"p\">,</span>\n    <span class=\"nx\">payload</span><span class=\"o\">:</span> <span class=\"p\">{</span> <span class=\"nx\">name</span> <span class=\"p\">}</span>\n  <span class=\"p\">}</span>\n<span class=\"p\">}</span>\n</pre>\n</div>"},{"id":"T0E6OlNlY3Rpb24tMTE0NzY=","title":"Toggle Timer","htmlContent":"<p><code>TOGGLE_TIMER</code> - Starts or stops a timer. This requires the\nindex of the timer.</p><div class=\"action\">\n<p></p>\n\n<p>Add the following to <code>src/actions/index.js</code>:</p>\n<pre><span class=\"kr\">export</span> <span class=\"kr\">const</span> <span class=\"nx\">TOGGLE_TIMER</span> <span class=\"o\">=</span> <span class=\"s1\">'TOGGLE_TIMER'</span>\n<span class=\"kr\">export</span> <span class=\"kr\">const</span> <span class=\"nx\">toggleTimer</span> <span class=\"o\">=</span> <span class=\"p\">(</span><span class=\"nx\">index</span><span class=\"p\">)</span> <span class=\"p\">=&gt;</span> <span class=\"p\">{</span>\n  <span class=\"k\">return</span> <span class=\"p\">{</span>\n    <span class=\"nx\">type</span><span class=\"o\">:</span> <span class=\"nx\">TOGGLE_TIMER</span><span class=\"p\">,</span>\n    <span class=\"nx\">payload</span><span class=\"o\">:</span> <span class=\"p\">{</span> <span class=\"nx\">index</span> <span class=\"p\">}</span>\n  <span class=\"p\">}</span>\n<span class=\"p\">}</span>\n</pre>\n</div>"},{"id":"T0E6OlNlY3Rpb24tMTE0Nzc=","title":"Select Timer","htmlContent":"<p><code>SELECT_TIMER</code> - Selects a timer. Selecting a timer will\ndisplay details about that timer. This requires the index\nof the timer.</p><div class=\"action\">\n<p></p>\n\n<p>Add the following to <code>src/actions/index.js</code>:</p>\n<pre><span class=\"kr\">export</span> <span class=\"kr\">const</span> <span class=\"nx\">SELECT_TIMER</span> <span class=\"o\">=</span> <span class=\"s1\">'SELECT_TIMER'</span>\n<span class=\"kr\">export</span> <span class=\"kr\">const</span> <span class=\"nx\">selectTimer</span> <span class=\"o\">=</span> <span class=\"p\">(</span><span class=\"nx\">index</span><span class=\"p\">)</span> <span class=\"p\">=&gt;</span> <span class=\"p\">{</span>\n  <span class=\"k\">return</span> <span class=\"p\">{</span>\n    <span class=\"nx\">type</span><span class=\"o\">:</span> <span class=\"nx\">SELECT_TIMER</span><span class=\"p\">,</span>\n    <span class=\"nx\">payload</span><span class=\"o\">:</span> <span class=\"p\">{</span> <span class=\"nx\">index</span> <span class=\"p\">}</span>\n  <span class=\"p\">}</span>\n<span class=\"p\">}</span>\n</pre>\n</div>"},{"id":"T0E6OlNlY3Rpb24tMTE0Nzg=","title":"Why make a variable for each action name?","htmlContent":"<p>These action names are all stored as strings. You'll be referencing them in more than one place in your app. By defining the vairable the value appears only in a single location and can easily be changed. Exporting the variable from this file allows the code editor to catch errors and prevents us from making spelling mistakes. </p><p>We're now well on our way to <strong>building React components and managing state using the Flux pattern</strong>, but we've got some work to do before we're finished.</p>"},{"id":"T0E6OlNlY3Rpb24tMTE0Nzk=","title":"Now Commit","htmlContent":"<div class=\"action\">\n<p></p>\n<pre>$ git add .\n$ git commit -m <span class=\"s1\">'added Timer actions'</span>\n$ git push\n</pre>\n</div>"}]},"next":{"id":"T0E6OlBhZ2UtMjI1Nw==","slug":"react-redux-timers-define-reducer","title":"Define Reducer"},"previous":{"id":"T0E6OlBhZ2UtMjI1Nw==","slug":"react-redux-timers-define-reducer","title":"Define Reducer"}},{"id":"T0E6OlBhZ2UtMjI1Nw==","title":"Define Reducer","slug":"react-redux-timers-define-reducer","sections":{"nodes":[{"id":"T0E6OlNlY3Rpb24tMTE0ODA=","title":"Define Reducer","htmlContent":"<h2>Technical Planning</h2><ol>\n<li><del>Build a Timer object</del></li>\n<li><del>Define the Actions of a Timer</del></li>\n<li>\n<strong>Define the Reducers of a Timer</strong>\n\n<ol>\n<li>Determine how many reducers we need in order to accomplish our goals</li>\n<li>Build out a reducer for a selected timer</li>\n<li>Build out a reducer for an array of timers</li>\n</ol>\n</li>\n<li>Allow users to create a Timer</li>\n<li>Allow users to see a list of Timers</li>\n<li>Users should be able to start/stop the clock on their Timers</li>\n<li>Style the app</li>\n<li>Allow Timers to persist</li>\n</ol><p>Next we need to build a <strong>reducer</strong> to handle changes in state. Before we build them, let's briefly revisit what application state will look like.</p>"},{"id":"T0E6OlNlY3Rpb24tMTE0ODE=","title":"Store Review","htmlContent":"<p>The <strong>store</strong> is a JavaScript Object with properties and values. </p><p>You could say each property on the store object is a 'pieces' of your overall application state. </p><p>For this app we will put an array of timers on one piece of state, and the index of the selected timer on another piece of state. </p><p>With this arrangement the store would look like this:</p><pre><span class=\"p\">{</span>\n  <span class=\"nx\">timers</span><span class=\"o\">:</span> <span class=\"p\">[</span> <span class=\"nx\">Timer1</span><span class=\"p\">,</span> <span class=\"nx\">Timer2</span><span class=\"p\">,</span> <span class=\"nx\">Timer3</span><span class=\"p\">,</span> <span class=\"p\">...</span> <span class=\"p\">],</span>\n  <span class=\"nx\">selectedTimer</span><span class=\"o\">:</span> <span class=\"mi\">0</span>\n<span class=\"p\">}</span>\n</pre><p>Values are assigned to each piece of state through a reducer. A reducer is a function that is responsible for making chnages to state. </p><p>You'll need a reducer for each piece of state. Above we've defined two properties: <code>timers</code> and <code>selectedTimer</code> so we need a reducer for each. </p><div class=\"action\">\n<p></p>\n\n<p>First create a folder to hold the reducers: <code>src/reducers</code></p>\n</div>"},{"id":"T0E6OlNlY3Rpb24tMTE0ODI=","title":"Selected Timer Reducer","htmlContent":"<p>The selectedTimer Reducer will need to set the <code>selectedTimer</code> to the\nvalue passed in the <code>payload</code> of the <code>SELECT_TIMER</code> action.</p><div class=\"action\">\n<p></p>\n\n<p>Create a new file <code>src/reducers/selectedTimerReducer.js</code> with the following code:</p>\n<pre><span class=\"c1\">// Import the SELECT_TIMER action</span>\n<span class=\"kr\">import</span> <span class=\"p\">{</span> <span class=\"nx\">SELECT_TIMER</span> <span class=\"p\">}</span> <span class=\"nx\">from</span> <span class=\"s1\">'../actions'</span><span class=\"p\">;</span>\n\n<span class=\"kr\">const</span> <span class=\"nx\">selectedTimerReducer</span> <span class=\"o\">=</span> <span class=\"p\">(</span><span class=\"nx\">state</span> <span class=\"o\">=</span> <span class=\"kc\">null</span><span class=\"p\">,</span> <span class=\"nx\">action</span><span class=\"p\">)</span> <span class=\"p\">=&gt;</span> <span class=\"p\">{</span>\n  <span class=\"k\">switch</span> <span class=\"p\">(</span><span class=\"nx\">action</span><span class=\"p\">.</span><span class=\"nx\">type</span><span class=\"p\">)</span> <span class=\"p\">{</span>\n    <span class=\"c1\">// On the SELECT_TIMER action, set the selectedTimer to the value passed in the payload</span>\n    <span class=\"k\">case</span> <span class=\"nx\">SELECT_TIMER</span><span class=\"o\">:</span>\n      <span class=\"k\">return</span> <span class=\"nx\">state</span> <span class=\"o\">=</span> <span class=\"nx\">action</span><span class=\"p\">.</span><span class=\"nx\">payload</span><span class=\"p\">.</span><span class=\"nx\">index</span><span class=\"p\">;</span>\n\n    <span class=\"k\">default</span><span class=\"o\">:</span>\n      <span class=\"k\">return</span> <span class=\"nx\">state</span><span class=\"p\">;</span>\n  <span class=\"p\">}</span>\n<span class=\"p\">}</span>\n\n<span class=\"kr\">export</span> <span class=\"k\">default</span> <span class=\"nx\">selectedTimerReducer</span><span class=\"p\">;</span>\n</pre>\n</div>"},{"id":"T0E6OlNlY3Rpb24tMTE0ODM=","title":"References and Redux","htmlContent":"<p>The Timers Reducer has a bit more work to get up and running, since it's responsible for managing the array of Timer objects. Before we get started though, it is <em>critical</em> to understand the difference between passing by reference vs. value.</p><h2>JavaScript Array Manipulation</h2><p>Primitives (strings, numbers, booleans) are always stored as values, and when assigned, those values are <strong>copied</strong> (a new value is created).</p><p>Objects, Arrays, and Functions are <strong>not copied when assigned to variables.</strong> Instead a reference to the original is assigned. <em>When assigning an array to another variable both variables will reference the same array!</em></p><p>Check out the example <a href=\"https://repl.it/@MitchellHudson/Array-equivalency\" target=\"_blank\">here</a> to see this with real code!</p><p><strong>Understanding this is important to understanding JavaScript and Redux.</strong></p><p><strong>Redux requires a reducer to return a new object.</strong> If a reducer returns the same object, Redux will not see a change and will not update views.</p><div class=\"info\">\n<p></p>\n\n<p>Why? Redux needs to see that the object has been changed. Objects are passed by reference, modifying an object doesn't create a new reference.</p>\n</div><p><strong>Your reducers must return a new copy of <code>state</code> if <code>state</code> is an Object or Array.</strong></p>"},{"id":"T0E6OlNlY3Rpb24tMTE0ODQ=","title":"Copying vs Mutating an Array in Redux","htmlContent":"<p>Many array methods mutate the original array. Some methods create a copy. You'll want to use these methods in your reducers.</p><h2>Copy an array</h2><p>Copy an array with the spread operator <code>...</code>:</p><pre><span class=\"kr\">const</span> <span class=\"nx\">array</span> <span class=\"o\">=</span> <span class=\"p\">[</span><span class=\"mi\">1</span><span class=\"p\">,</span><span class=\"mi\">2</span><span class=\"p\">,</span><span class=\"mi\">4</span><span class=\"p\">]</span>\n<span class=\"kr\">const</span> <span class=\"nx\">newArray</span> <span class=\"o\">=</span> <span class=\"p\">[...</span><span class=\"nx\">array</span><span class=\"p\">]</span> \n</pre><p>Copy an array with <code>slice()</code></p><pre><span class=\"kr\">const</span> <span class=\"nx\">array</span> <span class=\"o\">=</span> <span class=\"p\">[</span><span class=\"mi\">1</span><span class=\"p\">,</span><span class=\"mi\">2</span><span class=\"p\">,</span><span class=\"mi\">4</span><span class=\"p\">]</span>\n<span class=\"kr\">const</span> <span class=\"nx\">newArray</span> <span class=\"o\">=</span> <span class=\"nx\">array</span><span class=\"p\">.</span><span class=\"nx\">slice</span><span class=\"p\">()</span>\n</pre><h2>Add a new item</h2><p>Copy your array and add a new item with the spread operator:</p><pre><span class=\"c1\">// Creates a new array</span>\n<span class=\"kd\">var</span> <span class=\"nx\">newState</span> <span class=\"o\">=</span> <span class=\"p\">[...</span><span class=\"nx\">state</span><span class=\"p\">,</span> <span class=\"nx\">newItem</span><span class=\"p\">];</span>\n</pre><p>Add a new item by copying the array with <code>Array.slice()</code> then adding a new item.</p><pre><span class=\"c1\">// Creates a copy of state</span>\n<span class=\"kd\">var</span> <span class=\"nx\">newState</span> <span class=\"o\">=</span> <span class=\"nx\">state</span><span class=\"p\">.</span><span class=\"nx\">slice</span><span class=\"p\">();</span>\n<span class=\"nx\">newState</span><span class=\"p\">.</span><span class=\"nx\">push</span><span class=\"p\">(</span><span class=\"nx\">newItem</span><span class=\"p\">);</span>\n</pre><h2>Insert a new item</h2><p>Inserts a new item at index.</p><pre><span class=\"kd\">var</span> <span class=\"nx\">newState</span> <span class=\"o\">=</span> <span class=\"p\">[...</span><span class=\"nx\">state</span><span class=\"p\">.</span><span class=\"nx\">slice</span><span class=\"p\">(</span><span class=\"mi\">0</span><span class=\"p\">,</span> <span class=\"nx\">index</span><span class=\"p\">),</span> <span class=\"nx\">newItem</span><span class=\"p\">,</span> <span class=\"p\">...</span><span class=\"nx\">state</span><span class=\"p\">.</span><span class=\"nx\">slice</span><span class=\"p\">(</span><span class=\"nx\">index</span><span class=\"p\">)];</span>\n</pre><p>If these topics are new for you, try some of these practice problems:</p><ol>\n<li>Create a new array and fill it with element using the spread operator.</li>\n<li>Create a new array with the elements from state starting at 0 to index - 1</li>\n<li>Inserts newItem at index.</li>\n<li>Inserts the items from state starting at index after newItem.</li>\n</ol><h2>Copy an object in an array</h2><p>Remember if you are modifying an object in an array you need to create a copy of that object!</p><p>Imagine state is an array of Objects with name and count <code>{name:\"\", count:0}</code>. You want to increase count for the object at index.</p><pre><span class=\"kd\">var</span> <span class=\"nx\">newState</span> <span class=\"o\">=</span> <span class=\"nx\">state</span><span class=\"p\">.</span><span class=\"nx\">map</span><span class=\"p\">((</span><span class=\"nx\">item</span><span class=\"p\">,</span> <span class=\"nx\">i</span><span class=\"p\">)</span> <span class=\"p\">=&gt;</span> <span class=\"p\">{</span>\n  <span class=\"k\">if</span> <span class=\"p\">(</span><span class=\"nx\">i</span> <span class=\"o\">===</span> <span class=\"nx\">index</span><span class=\"p\">)</span> <span class=\"p\">{</span>\n    <span class=\"c1\">// Returns a new object with the values of item, and overwrites count with new value</span>\n    <span class=\"k\">return</span> <span class=\"p\">{...</span><span class=\"nx\">item</span><span class=\"p\">,</span> <span class=\"nx\">count</span><span class=\"o\">:</span> <span class=\"nx\">item</span><span class=\"p\">.</span><span class=\"nx\">count</span> <span class=\"o\">+</span> <span class=\"mi\">1</span><span class=\"p\">};</span>\n  <span class=\"p\">}</span>                          \n  <span class=\"k\">return</span> <span class=\"nx\">item</span><span class=\"p\">;</span>\n<span class=\"p\">})</span>\n</pre><p>If these topics are new for you, try some of these practice problems:</p><ol>\n<li>Use map to generate a new Array</li>\n<li>Inside map look at each item and match it to index</li>\n<li>Return a new Object where count has been incremented</li>\n<li>Or, return the original item</li>\n</ol>"},{"id":"T0E6OlNlY3Rpb24tMTE0ODU=","title":"Timers Reducer","htmlContent":"<p>Alright, now we're ready to define a reducer for timers.</p><p><strong>Remember: Redux requires a reducer to return a new object.</strong></p><div class=\"action\">\n<p></p>\n\n<p>Create a new file: <code>src/reducers/timersReducer.js</code> with the following code:</p>\n<pre><span class=\"c1\">// Import all of our actions</span>\n<span class=\"kr\">import</span> <span class=\"p\">{</span> <span class=\"nx\">NEW_TIMER</span><span class=\"p\">,</span> <span class=\"nx\">TOGGLE_TIMER</span> <span class=\"p\">}</span> <span class=\"nx\">from</span> <span class=\"s1\">'../actions'</span><span class=\"p\">;</span>\n<span class=\"kr\">import</span> <span class=\"nx\">Timer</span> <span class=\"nx\">from</span> <span class=\"s1\">'../Timer'</span>\n\n<span class=\"kr\">const</span> <span class=\"nx\">timersReducer</span> <span class=\"o\">=</span> <span class=\"p\">(</span><span class=\"nx\">state</span> <span class=\"o\">=</span> <span class=\"p\">[],</span> <span class=\"nx\">action</span><span class=\"p\">)</span> <span class=\"p\">=&gt;</span> <span class=\"p\">{</span>\n  <span class=\"k\">switch</span> <span class=\"p\">(</span><span class=\"nx\">action</span><span class=\"p\">.</span><span class=\"nx\">type</span><span class=\"p\">)</span> <span class=\"p\">{</span>\n    <span class=\"k\">case</span> <span class=\"nx\">NEW_TIMER</span><span class=\"o\">:</span>\n      <span class=\"c1\">// Add a new timer, return a copy of state</span>\n      <span class=\"kr\">const</span> <span class=\"nx\">name</span> <span class=\"o\">=</span> <span class=\"nx\">action</span><span class=\"p\">.</span><span class=\"nx\">payload</span><span class=\"p\">.</span><span class=\"nx\">name</span> <span class=\"o\">?</span> <span class=\"nx\">action</span><span class=\"p\">.</span><span class=\"nx\">payload</span><span class=\"p\">.</span><span class=\"nx\">name</span> <span class=\"o\">:</span> <span class=\"sb\">`Timer </span><span class=\"si\">${</span><span class=\"nx\">state</span><span class=\"p\">.</span><span class=\"nx\">length</span><span class=\"si\">}</span><span class=\"sb\">`</span>\n      <span class=\"k\">return</span> <span class=\"p\">[...</span><span class=\"nx\">state</span><span class=\"p\">,</span> <span class=\"k\">new</span> <span class=\"nx\">Timer</span><span class=\"p\">(</span><span class=\"nx\">name</span><span class=\"p\">)]</span>\n\n    <span class=\"k\">case</span> <span class=\"nx\">TOGGLE_TIMER</span><span class=\"o\">:</span>\n      <span class=\"c1\">// Invert the isRunning property of timer at index, return a copy of state</span>\n      <span class=\"kr\">const</span> <span class=\"nx\">newState</span> <span class=\"o\">=</span> <span class=\"nx\">state</span><span class=\"p\">.</span><span class=\"nx\">map</span><span class=\"p\">((</span><span class=\"nx\">timer</span><span class=\"p\">,</span> <span class=\"nx\">index</span><span class=\"p\">)</span> <span class=\"p\">=&gt;</span> <span class=\"p\">{</span>\n        <span class=\"k\">if</span> <span class=\"p\">(</span><span class=\"nx\">action</span><span class=\"p\">.</span><span class=\"nx\">payload</span><span class=\"p\">.</span><span class=\"nx\">index</span> <span class=\"o\">===</span> <span class=\"nx\">index</span><span class=\"p\">)</span> <span class=\"p\">{</span>\n          <span class=\"k\">return</span> <span class=\"p\">{...</span><span class=\"nx\">timer</span><span class=\"p\">,</span> <span class=\"nx\">isRunning</span><span class=\"o\">:</span> <span class=\"o\">!</span><span class=\"nx\">timer</span><span class=\"p\">.</span><span class=\"nx\">isRunning</span><span class=\"p\">}</span>\n        <span class=\"p\">}</span>\n        <span class=\"k\">return</span> <span class=\"nx\">timer</span>\n      <span class=\"p\">})</span>\n      <span class=\"k\">return</span> <span class=\"nx\">newState</span>\n\n    <span class=\"k\">default</span><span class=\"o\">:</span>\n      <span class=\"k\">return</span> <span class=\"nx\">state</span><span class=\"p\">;</span>\n  <span class=\"p\">}</span>\n<span class=\"p\">}</span>\n\n<span class=\"kr\">export</span> <span class=\"k\">default</span> <span class=\"nx\">timersReducer</span><span class=\"p\">;</span>\n</pre>\n</div>"},{"id":"T0E6OlNlY3Rpb24tMTE0ODY=","title":"Combine and Export Reducers","htmlContent":"<p>While you have a reducer for each piece of state you need a root reducer to bring them all together. <a href=\"https://redux.js.org/api/combinereducers\" target=\"_blank\">combineReducers</a> is used for this. </p><div class=\"action\">\n<p></p>\n\n<p>Add a file <code>src/reducers/index.js</code> and add the following:</p>\n<pre><span class=\"kr\">import</span> <span class=\"p\">{</span> <span class=\"nx\">combineReducers</span> <span class=\"p\">}</span> <span class=\"nx\">from</span> <span class=\"s1\">'redux'</span><span class=\"p\">;</span>\n\n<span class=\"kr\">import</span> <span class=\"nx\">timerReducer</span> <span class=\"nx\">from</span> <span class=\"s1\">'./timersReducer'</span><span class=\"p\">;</span>\n<span class=\"kr\">import</span> <span class=\"nx\">selectedTimerReducer</span> <span class=\"nx\">from</span> <span class=\"s1\">'./selectedTimerReducer'</span><span class=\"p\">;</span>\n\n<span class=\"kr\">export</span> <span class=\"k\">default</span> <span class=\"nx\">combineReducers</span><span class=\"p\">({</span>\n  <span class=\"nx\">timers</span><span class=\"o\">:</span> <span class=\"nx\">timerReducer</span><span class=\"p\">,</span>              <span class=\"c1\">// array</span>\n  <span class=\"nx\">selectedTimer</span><span class=\"o\">:</span> <span class=\"nx\">selectedTimerReducer</span><span class=\"p\">,</span> <span class=\"c1\">// int/number</span>\n<span class=\"p\">});</span>\n</pre>\n</div><p>Import the reducers at the top of <code>App.js</code>. You also need <a href=\"https://redux.js.org/api/createstore\" target=\"_blank\">createStore</a> from <code>redux</code> and <a href=\"https://react-redux.js.org/api/provider\" target=\"_blank\">Provider</a> from <code>react-redux</code>.</p><div class=\"action\">\n<p></p>\n\n<p>Add the following to the top of <code>App.js</code>:</p>\n<pre><span class=\"p\">...</span>\n\n<span class=\"kr\">import</span> <span class=\"p\">{</span> <span class=\"nx\">createStore</span> <span class=\"p\">}</span> <span class=\"nx\">from</span> <span class=\"s1\">'redux'</span><span class=\"p\">;</span>\n<span class=\"kr\">import</span> <span class=\"p\">{</span> <span class=\"nx\">Provider</span> <span class=\"p\">}</span> <span class=\"nx\">from</span> <span class=\"s1\">'react-redux'</span><span class=\"p\">;</span>\n\n<span class=\"kr\">import</span> <span class=\"nx\">reducers</span> <span class=\"nx\">from</span> <span class=\"s1\">'./reducers'</span><span class=\"p\">;</span>\n</pre>\n<p>Then create the store below all the import statements</p>\n<pre><span class=\"p\">...</span>\n<span class=\"kr\">const</span> <span class=\"nx\">store</span> <span class=\"o\">=</span> <span class=\"nx\">createStore</span><span class=\"p\">(</span><span class=\"nx\">reducers</span><span class=\"p\">);</span>\n</pre>\n</div><p>Finally, define the <code>Provider</code> component, which makes the Redux store available to all nested components.</p><div class=\"action\">\n<p></p>\n\n<p>Update your <code>App</code> component in <code>App.js</code>:</p>\n<pre><span class=\"kd\">function</span> <span class=\"nx\">App</span><span class=\"p\">()</span> <span class=\"p\">{</span>\n  <span class=\"k\">return</span> <span class=\"p\">(</span>\n    <span class=\"o\">&lt;</span><span class=\"nx\">Provider</span> <span class=\"nx\">store</span><span class=\"o\">=</span><span class=\"p\">{</span><span class=\"nx\">store</span><span class=\"p\">}</span><span class=\"o\">&gt;</span>\n      <span class=\"o\">&lt;</span><span class=\"nx\">h1</span><span class=\"o\">&gt;</span><span class=\"nx\">TMRZ</span><span class=\"o\">&lt;</span><span class=\"err\">/h1&gt;</span>\n    <span class=\"o\">&lt;</span><span class=\"err\">/Provider&gt;</span>\n  <span class=\"p\">);</span>\n<span class=\"p\">}</span>\n</pre>\n</div><p>Your app should display the the heading: TMRZ. This doesn't look like much but you've actually done a lot laying down redux infastructure. The Actions and Reducers are ready to manage state in this application. </p><p>Building out our reducers gave us some good practice with getting <strong>further acquainted with the Flux pattern!</strong> And now we're ready to move on and build out our React Components!</p><h2>Resources</h2><ul>\n<li>https://redux.js.org/recipes/structuring-reducers/immutable-update-patterns</li>\n</ul>"},{"id":"T0E6OlNlY3Rpb24tMTE0ODc=","title":"Now Commit","htmlContent":"<div class=\"action\">\n<p></p>\n<pre>$ git add .\n$ git commit -m <span class=\"s1\">'added Timer reducers'</span>\n$ git push\n</pre>\n</div>"}]},"next":{"id":"T0E6OlBhZ2UtMjI1OA==","slug":"react-redux-timers-create-new-timers","title":"Create New Timers"},"previous":{"id":"T0E6OlBhZ2UtMjI1OA==","slug":"react-redux-timers-create-new-timers","title":"Create New Timers"}},{"id":"T0E6OlBhZ2UtMjI1OA==","title":"Create New Timers","slug":"react-redux-timers-create-new-timers","sections":{"nodes":[{"id":"T0E6OlNlY3Rpb24tMTE0ODg=","title":"Create New Timers","htmlContent":"<p>So far we have been working on getting Redux and React up and\nrunning and no work has gone into components, which is the heart of React!</p><p>With actions and reducers in place you can now create\ncontainers/components that display state from the store and\nsend actions to update the store. Let's get those working!</p><h2>Technical Planning</h2><ol>\n<li><del>Build a Timer object</del></li>\n<li><del>Define the Actions of a Timer</del></li>\n<li><del>Define the Reducers of a Timer</del></li>\n<li>\n<strong>Allow users to create a Timer</strong>\n\n<ol>\n<li>Create a <code>new-timer</code> component</li>\n<li>Allow that component to use the addTimer action</li>\n<li>Allow for input of a name for the timer</li>\n<li>Save the name of the timer</li>\n<li>Build a button to allow the user to add a timer</li>\n</ol>\n</li>\n<li>Allow users to see a list of Timers</li>\n<li>Users should be able to start/stop the clock on their Timers</li>\n<li>Style the app</li>\n<li>Allow Timers to persist</li>\n</ol>"},{"id":"T0E6OlNlY3Rpb24tMTE0ODk=","title":"New Timer Component","htmlContent":"<p>The first component we'll build is for creating new timers. The new timer component should have the following:</p><ul>\n<li>A field to input a name</li>\n<li>A button to save the timer</li>\n</ul><div class=\"info\">\n<p></p>\n\n<p>The above field that will allow users to input a name is a <strong>controlled component</strong>. This is when \"the React component that renders [an input] also controls what happens in that form on subsequent user input.\"</p>\n\n<p>You can read more about controlled components <a href=\"https://reactjs.org/docs/forms.html#controlled-components\" target=\"_blank\">here</a></p>\n</div><p>Keep in mind that we won't see timers until the Timer list component is created, which will be built in the next chapter.</p><div class=\"action\">\n<p></p>\n\n<p>Create a new folder in <code>src</code> called <code>components</code>. Now within this folder, create a <code>NewTimer.js</code> file which will house our new-timer component</p>\n\n<p>Fill in <code>/src/components/NewTimer.js</code> with the following starter code</p>\n<pre><span class=\"kr\">import</span> <span class=\"nx\">React</span><span class=\"p\">,</span> <span class=\"p\">{</span> <span class=\"nx\">useState</span> <span class=\"p\">}</span> <span class=\"nx\">from</span> <span class=\"s1\">'react'</span>\n<span class=\"kr\">import</span> <span class=\"p\">{</span> <span class=\"nx\">useDispatch</span> <span class=\"p\">}</span> <span class=\"nx\">from</span> <span class=\"s1\">'react-redux'</span>\n\n<span class=\"c1\">// We need to import our action to add a new timer</span>\n<span class=\"kr\">import</span> <span class=\"p\">{</span> <span class=\"nx\">addTimer</span> <span class=\"p\">}</span> <span class=\"nx\">from</span> <span class=\"s1\">'../actions'</span>\n\n<span class=\"kr\">export</span> <span class=\"k\">default</span> <span class=\"kd\">function</span> <span class=\"nx\">NewTimer</span><span class=\"p\">()</span> <span class=\"p\">{</span>\n    <span class=\"kr\">const</span> <span class=\"p\">[</span> <span class=\"nx\">name</span><span class=\"p\">,</span> <span class=\"nx\">setName</span> <span class=\"p\">]</span> <span class=\"o\">=</span> <span class=\"nx\">useState</span><span class=\"p\">(</span><span class=\"s1\">''</span><span class=\"p\">)</span>\n    <span class=\"kr\">const</span> <span class=\"nx\">dispatch</span> <span class=\"o\">=</span> <span class=\"nx\">useDispatch</span><span class=\"p\">()</span>\n\n    <span class=\"k\">return</span> <span class=\"p\">(</span>\n        <span class=\"o\">&lt;</span><span class=\"nx\">div</span><span class=\"o\">&gt;</span>\n</pre>\n</div>\n<pre>    <span class=\"p\">&lt;/</span><span class=\"nt\">div</span><span class=\"p\">&gt;</span>\n)\n</pre><p>}\n```</p><p>You now have a basic <code>NewTimer</code> component structure, but we have no idea how to render it! Remember we want to have the following:</p><ul>\n<li>A field to input a name</li>\n<li>A button to save the timer</li>\n</ul><p>Let's build that out in the render function:</p><div class=\"action\">\n<p></p>\n\n<p>Fill in the <code>render</code> method in <code>/src/components/NewTimer.js</code>, as well as adding a few other ancillary methods and an export statement:</p>\n<pre><span class=\"kr\">export</span> <span class=\"k\">default</span> <span class=\"kd\">function</span> <span class=\"nx\">NewTimer</span><span class=\"p\">()</span> <span class=\"p\">{</span>\n    <span class=\"kr\">const</span> <span class=\"p\">[</span> <span class=\"nx\">name</span><span class=\"p\">,</span> <span class=\"nx\">setName</span> <span class=\"p\">]</span> <span class=\"o\">=</span> <span class=\"nx\">useState</span><span class=\"p\">(</span><span class=\"s1\">''</span><span class=\"p\">)</span>\n    <span class=\"kr\">const</span> <span class=\"nx\">dispatch</span> <span class=\"o\">=</span> <span class=\"nx\">useDispatch</span><span class=\"p\">()</span>\n\n    <span class=\"k\">return</span> <span class=\"p\">(</span>\n        <span class=\"o\">&lt;</span><span class=\"nx\">div</span><span class=\"o\">&gt;</span>\n            <span class=\"o\">&lt;</span><span class=\"nx\">input</span>\n                <span class=\"nx\">type</span><span class=\"o\">=</span><span class=\"s1\">'text'</span>\n                <span class=\"nx\">placeholder</span><span class=\"o\">=</span><span class=\"s2\">\"New Timer Name\"</span>\n                <span class=\"nx\">name</span><span class=\"o\">=</span><span class=\"s2\">\"name\"</span>\n                <span class=\"nx\">value</span><span class=\"o\">=</span><span class=\"p\">{</span><span class=\"nx\">name</span><span class=\"p\">}</span>\n                <span class=\"nx\">onChange</span><span class=\"o\">=</span><span class=\"p\">{(</span><span class=\"nx\">e</span><span class=\"p\">)</span> <span class=\"p\">=&gt;</span> <span class=\"nx\">setName</span><span class=\"p\">(</span><span class=\"nx\">e</span><span class=\"p\">.</span><span class=\"nx\">target</span><span class=\"p\">.</span><span class=\"nx\">value</span><span class=\"p\">)}</span><span class=\"o\">/&gt;</span>\n            <span class=\"o\">&lt;</span><span class=\"nx\">button</span>\n                <span class=\"nx\">onClick</span><span class=\"o\">=</span><span class=\"p\">{()</span> <span class=\"p\">=&gt;</span> <span class=\"nx\">dispatch</span><span class=\"p\">(</span><span class=\"nx\">addTimer</span><span class=\"p\">(</span><span class=\"nx\">name</span><span class=\"p\">))}</span>\n            <span class=\"o\">&gt;</span><span class=\"nx\">Save</span><span class=\"o\">&lt;</span><span class=\"err\">/button&gt;</span>\n        <span class=\"o\">&lt;</span><span class=\"err\">/div&gt;</span>\n    <span class=\"p\">)</span>\n<span class=\"p\">}</span>\n</pre>\n</div><ul>\n<li>UseDispatch - will give you access to the Redux store and allow your component to send actions when you need to update the store.</li>\n<li>useState - is used to create a value that will be available across multiple renders of this component. </li>\n</ul><p>First React component in the bag! Great work on <strong>building a react component using JSX syntax</strong>, as well as getting more practice <strong>managing application state</strong>. But we can't stop here, let's build another component!</p>"},{"id":"T0E6OlNlY3Rpb24tMTE0OTA=","title":"Now Commit","htmlContent":"<div class=\"action\">\n<p></p>\n<pre>$ git add .\n$ git commit -m <span class=\"s1\">'added new timer input'</span>\n$ git push\n</pre>\n</div>"}]},"next":{"id":"T0E6OlBhZ2UtMjI1OQ==","slug":"react-redux-timers-list-timers","title":"List Timers"},"previous":{"id":"T0E6OlBhZ2UtMjI1OQ==","slug":"react-redux-timers-list-timers","title":"List Timers"}},{"id":"T0E6OlBhZ2UtMjI1OQ==","title":"List Timers","slug":"react-redux-timers-list-timers","sections":{"nodes":[{"id":"T0E6OlNlY3Rpb24tMTE0OTE=","title":"List Timers","htmlContent":"<h2>Technical Planning</h2><ol>\n<li><del>Build a Timer object</del></li>\n<li><del>Define the Actions of a Timer</del></li>\n<li><del>Define the Reducers of a Timer</del></li>\n<li><del>Allow users to create a Timer</del></li>\n<li>\n<strong>Allow users to see a list of Timers</strong>\n\n<ol>\n<li>Create a <code>list-timers</code> component that will house a list of timers</li>\n<li>Create a <code>timer-view</code> component to describe what a timer looks like</li>\n<li>Within the <code>timer-view</code>, allow for display of a name, time, and start/stop button</li>\n<li>Start/stop button should change text when clicked for now</li>\n<li>Display your progress so far in the browser</li>\n</ol>\n</li>\n<li>Users should be able to start/stop the clock on their Timers</li>\n<li>Style the app</li>\n<li>Allow Timers to persist</li>\n</ol><p>The list of timers will display all of the timers you have created. Each timer displayed in the list will need to display the following:</p><ul>\n<li>Name</li>\n<li>Time</li>\n<li>Start/Stop - A button to start or stop that timer.</li>\n</ul><p>The Timer list itself should display the following:</p><ul>\n<li>A list of timers in the store.</li>\n</ul>"},{"id":"T0E6OlNlY3Rpb24tMTE0OTI=","title":"List-Timers - Boilerplate","htmlContent":"<div class=\"action\">\n<p></p>\n\n<p>Create a new file <code>src/ListTimers.js</code> with the following boilerplate code:</p>\n<pre><span class=\"kr\">import</span> <span class=\"nx\">React</span> <span class=\"nx\">from</span> <span class=\"s1\">'react'</span>\n<span class=\"kr\">import</span> <span class=\"p\">{</span> <span class=\"nx\">useSelector</span> <span class=\"p\">}</span> <span class=\"nx\">from</span> <span class=\"s1\">'react-redux'</span>\n<span class=\"kr\">import</span> <span class=\"p\">{</span> <span class=\"nx\">selectTimer</span> <span class=\"p\">}</span> <span class=\"nx\">from</span> <span class=\"s1\">'../actions'</span>\n\n<span class=\"kr\">export</span> <span class=\"k\">default</span> <span class=\"kd\">function</span> <span class=\"nx\">ListTimers</span><span class=\"p\">()</span> <span class=\"p\">{</span>\n\n    <span class=\"k\">return</span> <span class=\"p\">(</span>\n        <span class=\"o\">&lt;</span><span class=\"nx\">div</span><span class=\"o\">&gt;</span>\n            <span class=\"p\">{</span><span class=\"cm\">/* render timers here */</span><span class=\"p\">}</span>\n        <span class=\"o\">&lt;</span><span class=\"err\">/div&gt;</span>\n    <span class=\"p\">)</span>\n<span class=\"p\">}</span>\n</pre>\n</div><p>While this implementation doesn't render the timers (yet), it does set up the basic elements required for container/component to interface with Redux!</p>"},{"id":"T0E6OlNlY3Rpb24tMTE0OTM=","title":"The Timer list","htmlContent":"<p>To render the list of timers you need to get the timers array from the store. To do this use: <code>useSelector()</code>. Pass this method a function that receives state and returns the piece of state you need. </p><p>Add the code below. This should get the <code>timers</code> piece of your state, and then map it to a JSX block that will be displayed by the component. </p><pre><span class=\"kr\">export</span> <span class=\"k\">default</span> <span class=\"kd\">function</span> <span class=\"nx\">ListTimers</span><span class=\"p\">()</span> <span class=\"p\">{</span>\n    <span class=\"kr\">const</span> <span class=\"nx\">timers</span> <span class=\"o\">=</span> <span class=\"nx\">useSelector</span><span class=\"p\">(</span><span class=\"nx\">state</span> <span class=\"p\">=&gt;</span> <span class=\"nx\">state</span><span class=\"p\">.</span><span class=\"nx\">timers</span><span class=\"p\">)</span>\n\n    <span class=\"k\">return</span> <span class=\"p\">(</span>\n        <span class=\"o\">&lt;</span><span class=\"nx\">div</span><span class=\"o\">&gt;</span>\n            <span class=\"p\">{</span><span class=\"nx\">timers</span><span class=\"p\">.</span><span class=\"nx\">map</span><span class=\"p\">((</span><span class=\"nx\">timer</span><span class=\"p\">,</span> <span class=\"nx\">i</span><span class=\"p\">)</span> <span class=\"p\">=&gt;</span> <span class=\"p\">{</span>\n                <span class=\"c1\">// Here the render method maps `this.props.timers` to:</span>\n                <span class=\"k\">return</span> <span class=\"p\">(</span>\n                    <span class=\"o\">&lt;</span><span class=\"nx\">div</span><span class=\"o\">&gt;</span>\n                        <span class=\"o\">&lt;</span><span class=\"nx\">h2</span><span class=\"o\">&gt;</span><span class=\"p\">{</span><span class=\"nx\">timer</span><span class=\"p\">.</span><span class=\"nx\">name</span><span class=\"p\">}</span><span class=\"o\">&lt;</span><span class=\"err\">/h2&gt;</span>\n                        <span class=\"o\">&lt;</span><span class=\"nx\">h1</span><span class=\"o\">&gt;</span><span class=\"p\">{</span><span class=\"nx\">timer</span><span class=\"p\">.</span><span class=\"nx\">time</span><span class=\"p\">}</span><span class=\"o\">&lt;</span><span class=\"err\">/h1&gt;</span>\n                        <span class=\"o\">&lt;</span><span class=\"nx\">button</span><span class=\"o\">&gt;</span><span class=\"nx\">Start</span><span class=\"o\">&lt;</span><span class=\"err\">/button&gt;</span>\n                    <span class=\"o\">&lt;</span><span class=\"err\">/div&gt;</span>\n                <span class=\"p\">)</span>\n            <span class=\"p\">})}</span>\n        <span class=\"o\">&lt;</span><span class=\"err\">/div&gt;</span>\n    <span class=\"p\">)</span>\n<span class=\"p\">}</span>\n</pre><p>The timer list could be implemented as a single component. This simple approach works but doesn't use React's Component architecture to your advantage.</p><p>A better approach is to make a component from the block inside map: </p><pre><span class=\"o\">&lt;</span><span class=\"nx\">div</span><span class=\"o\">&gt;</span>\n  <span class=\"o\">&lt;</span><span class=\"nx\">h2</span><span class=\"o\">&gt;</span><span class=\"p\">{</span><span class=\"nx\">timer</span><span class=\"p\">.</span><span class=\"nx\">name</span><span class=\"p\">}</span><span class=\"o\">&lt;</span><span class=\"err\">/h2&gt;</span>\n  <span class=\"o\">&lt;</span><span class=\"nx\">h1</span><span class=\"o\">&gt;</span><span class=\"p\">{</span><span class=\"nx\">timer</span><span class=\"p\">.</span><span class=\"nx\">time</span><span class=\"p\">}</span><span class=\"o\">&lt;</span><span class=\"err\">/h1&gt;</span>\n  <span class=\"o\">&lt;</span><span class=\"nx\">button</span><span class=\"o\">&gt;</span><span class=\"nx\">Start</span><span class=\"o\">&lt;</span><span class=\"err\">/button&gt;</span>\n<span class=\"o\">&lt;</span><span class=\"err\">/div&gt;</span>\n</pre>\n<div class=\"solution\">\n<p></p>\n\n<p>To make it completely functional you will have to\nadd styles, possibly some more markup, the time will need to be formatted, and the button will require some logic and a click handler. What does this scream to us?</p>\n\n<p><strong>BLOAT!!</strong></p>\n\n<p>With these required additions this component would become far less manageable. In addition there would be a lot of logic, for things like formatting time, that is not core goal of this component, which is displaying a list of timers.</p>\n</div><p>It's always important to break components down into smaller, more easily manageable parts as opposed to inflating them unnecessarily.</p>"},{"id":"T0E6OlNlY3Rpb24tMTE0OTQ=","title":"Timer View Component","htmlContent":"<p>A better approach is to create a component that is responsible for displaying a single timer. Let's build this out first before we finish up the Timer List.</p><div class=\"action\">\n<p></p>\n\n<p>Create a file: <code>src/TimerView.js</code> with the following code:</p>\n<pre><span class=\"kr\">import</span> <span class=\"nx\">React</span><span class=\"p\">,</span> <span class=\"p\">{</span>  <span class=\"p\">}</span> <span class=\"nx\">from</span> <span class=\"s1\">'react'</span>\n<span class=\"kr\">import</span> <span class=\"p\">{</span> <span class=\"nx\">useDispatch</span> <span class=\"p\">}</span> <span class=\"nx\">from</span> <span class=\"s1\">'react-redux'</span>\n<span class=\"c1\">// Import our toggleTimer action</span>\n<span class=\"kr\">import</span> <span class=\"p\">{</span> <span class=\"nx\">toggleTimer</span> <span class=\"p\">}</span> <span class=\"nx\">from</span> <span class=\"s1\">'../actions'</span>\n\n<span class=\"kr\">export</span> <span class=\"k\">default</span> <span class=\"kd\">function</span> <span class=\"nx\">TimerView</span><span class=\"p\">(</span><span class=\"nx\">props</span><span class=\"p\">)</span> <span class=\"p\">{</span>\n  <span class=\"c1\">// Extract these specific props to use in the component</span>\n  <span class=\"kr\">const</span> <span class=\"p\">{</span> <span class=\"nx\">index</span><span class=\"p\">,</span> <span class=\"nx\">timer</span> <span class=\"p\">}</span> <span class=\"o\">=</span> <span class=\"nx\">props</span>\n  <span class=\"kr\">const</span> <span class=\"nx\">dispatch</span> <span class=\"o\">=</span> <span class=\"nx\">useDispatch</span><span class=\"p\">()</span>\n\n  <span class=\"k\">return</span> <span class=\"p\">(</span>\n    <span class=\"o\">&lt;</span><span class=\"nx\">div</span><span class=\"o\">&gt;</span>\n      <span class=\"o\">&lt;</span><span class=\"nx\">h2</span><span class=\"o\">&gt;</span><span class=\"p\">{</span><span class=\"nx\">timer</span><span class=\"p\">.</span><span class=\"nx\">name</span><span class=\"p\">}</span><span class=\"o\">&lt;</span><span class=\"err\">/h2&gt;</span>\n      <span class=\"o\">&lt;</span><span class=\"nx\">h1</span><span class=\"o\">&gt;</span><span class=\"p\">{</span><span class=\"nx\">timer</span><span class=\"p\">.</span><span class=\"nx\">time</span><span class=\"p\">}</span><span class=\"o\">&lt;</span><span class=\"err\">/h1&gt;</span>\n      <span class=\"o\">&lt;</span><span class=\"nx\">button</span>\n        <span class=\"nx\">onClick</span><span class=\"o\">=</span><span class=\"p\">{()</span> <span class=\"p\">=&gt;</span> <span class=\"nx\">dispatch</span><span class=\"p\">(</span><span class=\"nx\">toggleTimer</span><span class=\"p\">(</span><span class=\"nx\">index</span><span class=\"p\">))}</span>\n      <span class=\"o\">&gt;</span>\n        <span class=\"p\">{</span><span class=\"nx\">timer</span><span class=\"p\">.</span><span class=\"nx\">isRunning</span> <span class=\"o\">?</span> <span class=\"s2\">\"Stop\"</span> <span class=\"o\">:</span> <span class=\"s2\">\"Start\"</span><span class=\"p\">}</span>\n      <span class=\"o\">&lt;</span><span class=\"err\">/button&gt;</span>\n    <span class=\"o\">&lt;</span><span class=\"err\">/div&gt;</span>\n  <span class=\"p\">)</span>\n<span class=\"p\">}</span>\n</pre>\n</div><p>Notice: <code>TimerView</code> takes a <code>Timer</code> object as a prop: <code>timer={timer}</code>. The name and time properties could then be accessed as: <code>props.timer.name</code> and <code>props.timer.time</code> within <code>TimerView</code>. However, we'll introduce a shorthand to more easily access these properties so we don't need a long string every time.</p><p>From here, the render method in <code>src/components/ListTimers.js</code> can be simplified!</p><div class=\"action\">\n<p></p>\n\n<p>Update the imports and fill in the <code>render</code> method for <code>src/components/ListTimers.js</code> with the following:</p>\n<pre><span class=\"kr\">import</span> <span class=\"nx\">TimerView</span> <span class=\"nx\">from</span> <span class=\"s1\">'./TimerView'</span>\n<span class=\"p\">...</span>\n<span class=\"kr\">export</span> <span class=\"k\">default</span> <span class=\"kd\">function</span> <span class=\"nx\">ListTimers</span><span class=\"p\">()</span> <span class=\"p\">{</span>\n    <span class=\"kr\">const</span> <span class=\"nx\">timers</span> <span class=\"o\">=</span> <span class=\"nx\">useSelector</span><span class=\"p\">(</span><span class=\"nx\">state</span> <span class=\"p\">=&gt;</span> <span class=\"nx\">state</span><span class=\"p\">.</span><span class=\"nx\">timers</span><span class=\"p\">)</span>\n\n    <span class=\"k\">return</span> <span class=\"p\">(</span>\n        <span class=\"o\">&lt;</span><span class=\"nx\">div</span><span class=\"o\">&gt;</span>\n            <span class=\"p\">{</span><span class=\"nx\">timers</span><span class=\"p\">.</span><span class=\"nx\">map</span><span class=\"p\">((</span><span class=\"nx\">timer</span><span class=\"p\">,</span> <span class=\"nx\">i</span><span class=\"p\">)</span> <span class=\"p\">=&gt;</span> <span class=\"p\">{</span>\n                <span class=\"c1\">// Here the render method maps `this.props.timers` to:</span>\n                <span class=\"k\">return</span> <span class=\"p\">(</span>\n                    <span class=\"o\">&lt;</span><span class=\"nx\">TimerView</span> \n                        <span class=\"nx\">key</span><span class=\"o\">=</span><span class=\"p\">{</span><span class=\"sb\">`timer-</span><span class=\"si\">${</span><span class=\"nx\">i</span><span class=\"si\">}</span><span class=\"sb\">`</span><span class=\"p\">}</span> \n                        <span class=\"nx\">timer</span><span class=\"o\">=</span><span class=\"p\">{</span><span class=\"nx\">timer</span><span class=\"p\">}</span> \n                        <span class=\"nx\">index</span><span class=\"o\">=</span><span class=\"p\">{</span><span class=\"nx\">i</span><span class=\"p\">}</span> \n                    <span class=\"o\">/&gt;</span>\n                <span class=\"p\">)</span>\n            <span class=\"p\">})}</span>\n        <span class=\"o\">&lt;</span><span class=\"err\">/div&gt;</span>\n    <span class=\"p\">)</span>\n<span class=\"p\">}</span>\n</pre>\n</div>"},{"id":"T0E6OlNlY3Rpb24tMTE0OTU=","title":"Putting it All Together","htmlContent":"<p>At this stage, we can now create timers and have them appear, as well as press a start/stop button. Let's update <code>App.js</code> to actually show our timers so we have something to look at!</p><div class=\"action\">\n<p></p>\n\n<p>Update <code>App.js</code> to import our <code>NewTimer</code> and <code>ListTimers</code> components and then put them in the <code>Provider</code></p>\n<pre><span class=\"kr\">import</span> <span class=\"nx\">React</span> <span class=\"nx\">from</span> <span class=\"s1\">'react'</span><span class=\"p\">;</span>\n<span class=\"kr\">import</span> <span class=\"s1\">'./App.css'</span><span class=\"p\">;</span>\n<span class=\"kr\">import</span> <span class=\"p\">{</span> <span class=\"nx\">createStore</span> <span class=\"p\">}</span> <span class=\"nx\">from</span> <span class=\"s1\">'redux'</span><span class=\"p\">;</span>\n<span class=\"kr\">import</span> <span class=\"p\">{</span> <span class=\"nx\">Provider</span> <span class=\"p\">}</span> <span class=\"nx\">from</span> <span class=\"s1\">'react-redux'</span><span class=\"p\">;</span>\n<span class=\"kr\">import</span> <span class=\"nx\">reducers</span> <span class=\"nx\">from</span> <span class=\"s1\">'./reducers'</span><span class=\"p\">;</span>\n<span class=\"c1\">// import new components</span>\n<span class=\"kr\">import</span> <span class=\"nx\">NewTimer</span> <span class=\"nx\">from</span> <span class=\"s1\">'./components/NewTimer'</span>\n<span class=\"kr\">import</span> <span class=\"nx\">ListTimers</span> <span class=\"nx\">from</span> <span class=\"s1\">'./components/ListTimers'</span>\n\n<span class=\"kr\">const</span> <span class=\"nx\">store</span> <span class=\"o\">=</span> <span class=\"nx\">createStore</span><span class=\"p\">(</span><span class=\"nx\">reducers</span><span class=\"p\">);</span>\n\n<span class=\"kd\">function</span> <span class=\"nx\">App</span><span class=\"p\">()</span> <span class=\"p\">{</span>\n  <span class=\"k\">return</span> <span class=\"p\">(</span>\n    <span class=\"o\">&lt;</span><span class=\"nx\">Provider</span> <span class=\"nx\">store</span><span class=\"o\">=</span><span class=\"p\">{</span><span class=\"nx\">store</span><span class=\"p\">}</span><span class=\"o\">&gt;</span>\n      <span class=\"o\">&lt;</span><span class=\"nx\">h1</span><span class=\"o\">&gt;</span><span class=\"nx\">TMRZ</span><span class=\"o\">&lt;</span><span class=\"err\">/h1&gt;</span>\n      <span class=\"p\">{</span><span class=\"cm\">/* Display the new components */</span><span class=\"p\">}</span>\n      <span class=\"o\">&lt;</span><span class=\"nx\">NewTimer</span> <span class=\"o\">/&gt;</span>\n      <span class=\"o\">&lt;</span><span class=\"nx\">ListTimers</span> <span class=\"o\">/&gt;</span>\n    <span class=\"o\">&lt;</span><span class=\"err\">/Provider&gt;</span>\n  <span class=\"p\">);</span>\n<span class=\"p\">}</span>\n\n<span class=\"kr\">export</span> <span class=\"k\">default</span> <span class=\"nx\">App</span><span class=\"p\">;</span>\n</pre>\n</div><p>Currently the app is working but needs some styles. </p><p>A common pattern for styling react Components is to create a stylesheet for each component and import those styles into each component. </p><p>This method has it's pros and cons. </p><p>Pros: It makes the components portable. You can copy the styles .css file along with the component .js file together and move them into a new project. </p><p>Cons: Using a .css for each component you will have lots of css styles floating around making it easy to have duplicate class names or other selectors. </p><p>You will use this strategy for this project. To address the concerns above you'll assign the root element of each component a css class name that matches the component name. </p><h3>App Styles</h3><p>Open App.css and delete all of the styles. Add these styles: </p><pre><span class=\"p\">.</span><span class=\"nc\">App</span> <span class=\"p\">{</span>\n  <span class=\"k\">display</span><span class=\"p\">:</span> <span class=\"kc\">flex</span><span class=\"p\">;</span>\n  <span class=\"k\">flex-direction</span><span class=\"p\">:</span> <span class=\"kc\">column</span><span class=\"p\">;</span>\n  <span class=\"k\">align-items</span><span class=\"p\">:</span> <span class=\"kc\">center</span><span class=\"p\">;</span>\n<span class=\"p\">}</span>\n</pre><p>Open App.js and import thise style sheet by adding this to the top of the page:</p><pre><span class=\"kr\">import</span> <span class=\"s1\">'./App.css'</span><span class=\"p\">;</span>\n</pre><p>Next add a root element that will get the css class name: <code>App</code></p><pre><span class=\"o\">&lt;</span><span class=\"nx\">Provider</span> <span class=\"nx\">store</span><span class=\"o\">=</span><span class=\"p\">{</span><span class=\"nx\">store</span><span class=\"p\">}</span><span class=\"o\">&gt;</span>\n  <span class=\"o\">&lt;</span><span class=\"nx\">div</span> <span class=\"nx\">className</span><span class=\"o\">=</span><span class=\"s2\">\"App\"</span><span class=\"o\">&gt;</span>\n    <span class=\"o\">&lt;</span><span class=\"nx\">h1</span><span class=\"o\">&gt;</span><span class=\"nx\">TMRZ</span><span class=\"o\">&lt;</span><span class=\"err\">/h1&gt;</span>\n    <span class=\"p\">...</span>\n  <span class=\"o\">&lt;</span><span class=\"err\">/div&gt;</span>\n<span class=\"o\">&lt;</span><span class=\"err\">/Provider&gt;</span>\n</pre><p>With this in place the style should arrange all the children of <code>div.App</code> in a column centered in the page. </p><h3>NewTimer Styles</h3><p>The NewTimer component has container with two children. Give the container element a class name that matches the component name: </p><pre><span class=\"kr\">export</span> <span class=\"k\">default</span> <span class=\"kd\">function</span> <span class=\"nx\">NewTimer</span><span class=\"p\">()</span> <span class=\"p\">{</span>\n    <span class=\"p\">...</span>\n    <span class=\"k\">return</span> <span class=\"p\">(</span>\n        <span class=\"o\">&lt;</span><span class=\"nx\">div</span> <span class=\"nx\">className</span><span class=\"o\">=</span><span class=\"s2\">\"NewTimer\"</span><span class=\"o\">&gt;</span>\n            <span class=\"p\">...</span>\n        <span class=\"o\">&lt;</span><span class=\"err\">/div&gt;</span>\n    <span class=\"p\">)</span>\n<span class=\"p\">}</span>\n</pre><p>Make a new file: <code>NewTimer.css</code></p><pre><span class=\"p\">.</span><span class=\"nc\">NewTimer</span> <span class=\"p\">{</span>\n    <span class=\"k\">display</span><span class=\"p\">:</span> <span class=\"kc\">flex</span><span class=\"p\">;</span>\n    <span class=\"k\">justify-content</span><span class=\"p\">:</span> <span class=\"kc\">center</span><span class=\"p\">;</span>\n<span class=\"p\">}</span>\n\n<span class=\"p\">.</span><span class=\"nc\">NewTimer</span> <span class=\"o\">&gt;</span> <span class=\"nt\">input</span> <span class=\"p\">{</span>\n    <span class=\"k\">font-size</span><span class=\"p\">:</span> <span class=\"mi\">1</span><span class=\"kt\">rem</span><span class=\"p\">;</span>\n    <span class=\"k\">padding</span><span class=\"p\">:</span> <span class=\"mf\">0.25</span><span class=\"kt\">rem</span> <span class=\"mf\">0.5</span><span class=\"kt\">rem</span><span class=\"p\">;</span>\n    <span class=\"k\">border-radius</span><span class=\"p\">:</span> <span class=\"mf\">0.5</span><span class=\"kt\">rem</span> <span class=\"mi\">0</span> <span class=\"mi\">0</span> <span class=\"mf\">0.5</span><span class=\"kt\">rem</span><span class=\"p\">;</span>\n    <span class=\"k\">border</span><span class=\"p\">:</span> <span class=\"mi\">1</span><span class=\"kt\">px</span> <span class=\"kc\">solid</span><span class=\"p\">;</span>\n    <span class=\"k\">margin</span><span class=\"p\">:</span> <span class=\"mi\">0</span><span class=\"p\">;</span>\n<span class=\"p\">}</span>\n\n<span class=\"p\">.</span><span class=\"nc\">NewTimer</span> <span class=\"o\">&gt;</span> <span class=\"nt\">button</span> <span class=\"p\">{</span>\n    <span class=\"k\">font-size</span><span class=\"p\">:</span> <span class=\"mi\">1</span><span class=\"kt\">rem</span><span class=\"p\">;</span>\n    <span class=\"k\">padding</span><span class=\"p\">:</span> <span class=\"mf\">0.25</span><span class=\"kt\">rem</span> <span class=\"mf\">0.5</span><span class=\"kt\">rem</span><span class=\"p\">;</span>\n    <span class=\"k\">border-radius</span><span class=\"p\">:</span> <span class=\"mi\">0</span> <span class=\"mf\">0.5</span><span class=\"kt\">rem</span> <span class=\"mf\">0.5</span><span class=\"kt\">rem</span> <span class=\"mi\">0</span><span class=\"p\">;</span>\n    <span class=\"k\">border</span><span class=\"p\">:</span> <span class=\"mi\">1</span><span class=\"kt\">px</span> <span class=\"kc\">solid</span> <span class=\"mh\">#000</span><span class=\"p\">;</span>\n    <span class=\"k\">margin</span><span class=\"p\">:</span> <span class=\"mi\">0</span><span class=\"p\">;</span>\n    <span class=\"k\">background-color</span><span class=\"p\">:</span> <span class=\"mh\">#000</span><span class=\"p\">;</span>\n    <span class=\"k\">color</span><span class=\"p\">:</span> <span class=\"mh\">#fff</span><span class=\"p\">;</span>\n    <span class=\"k\">font-weight</span><span class=\"p\">:</span> <span class=\"kc\">bold</span><span class=\"p\">;</span>\n<span class=\"p\">}</span>\n</pre><p>Notice we used the child selector (&gt;) here. This keeps these styles scoped to the parent class name on the parent element. </p><p>Import the styles at the top of <code>NewTimer.js</code></p><pre><span class=\"kr\">import</span> <span class=\"s1\">'./NewTimer.css'</span>\n</pre><h3>TimerView Styles</h3><p>The TimerView component has a three child elements: Name, Time, and Start/Stop button. Let's arrange these in a row with the time in the center and the other elements at either end of the row. </p><p>Open <code>TimerView.js</code> and add a class name on the root element. </p><pre><span class=\"kr\">export</span> <span class=\"k\">default</span> <span class=\"kd\">function</span> <span class=\"nx\">TimerView</span><span class=\"p\">(</span><span class=\"nx\">props</span><span class=\"p\">)</span> <span class=\"p\">{</span>\n  <span class=\"p\">...</span>\n  <span class=\"k\">return</span> <span class=\"p\">(</span>\n    <span class=\"o\">&lt;</span><span class=\"nx\">div</span> <span class=\"nx\">className</span><span class=\"o\">=</span><span class=\"s2\">\"TimerView\"</span><span class=\"o\">&gt;</span>\n      <span class=\"p\">...</span>\n    <span class=\"o\">&lt;</span><span class=\"err\">/div&gt;</span>\n  <span class=\"p\">)</span>\n<span class=\"p\">}</span>\n</pre><p>Make a new CSS file: <code>TimerView.css</code></p><pre><span class=\"p\">.</span><span class=\"nc\">TimerView</span> <span class=\"p\">{</span>\n    <span class=\"k\">width</span><span class=\"p\">:</span> <span class=\"mi\">320</span><span class=\"kt\">px</span><span class=\"p\">;</span>\n    <span class=\"k\">display</span><span class=\"p\">:</span> <span class=\"kc\">flex</span><span class=\"p\">;</span>\n    <span class=\"k\">flex-direction</span><span class=\"p\">:</span> <span class=\"kc\">row</span><span class=\"p\">;</span>\n    <span class=\"k\">justify-content</span><span class=\"p\">:</span> <span class=\"kc\">space-between</span><span class=\"p\">;</span>\n    <span class=\"k\">align-items</span><span class=\"p\">:</span> <span class=\"kc\">baseline</span><span class=\"p\">;</span>\n<span class=\"p\">}</span>\n\n<span class=\"p\">.</span><span class=\"nc\">TimerView</span> <span class=\"o\">&gt;</span> <span class=\"nt\">h1</span> <span class=\"p\">{</span>\n    <span class=\"k\">flex</span><span class=\"p\">:</span><span class=\"mi\">1</span><span class=\"p\">;</span>\n    <span class=\"k\">margin</span><span class=\"p\">:</span> <span class=\"mi\">1</span><span class=\"kt\">rem</span><span class=\"p\">;</span>\n    <span class=\"k\">text-align</span><span class=\"p\">:</span> <span class=\"kc\">left</span><span class=\"p\">;</span>\n    <span class=\"k\">font-variant-numeric</span><span class=\"p\">:</span> <span class=\"n\">tabular-nums</span><span class=\"p\">;</span>\n<span class=\"p\">}</span>\n\n<span class=\"p\">.</span><span class=\"nc\">TimerView</span> <span class=\"o\">&gt;</span> <span class=\"nt\">h2</span> <span class=\"p\">{</span>\n    <span class=\"k\">flex</span><span class=\"p\">:</span> <span class=\"mi\">1</span><span class=\"p\">;</span>\n<span class=\"p\">}</span>\n\n<span class=\"p\">.</span><span class=\"nc\">TimerView</span> <span class=\"o\">&gt;</span> <span class=\"nt\">button</span> <span class=\"p\">{</span>\n    <span class=\"k\">padding</span><span class=\"p\">:</span> <span class=\"mf\">0.25</span><span class=\"kt\">rem</span> <span class=\"mf\">0.5</span><span class=\"kt\">rem</span><span class=\"p\">;</span>\n    <span class=\"k\">border-radius</span><span class=\"p\">:</span> <span class=\"mf\">0.5</span><span class=\"kt\">rem</span><span class=\"p\">;</span>\n    <span class=\"k\">border</span><span class=\"p\">:</span> <span class=\"mi\">1</span><span class=\"kt\">px</span> <span class=\"kc\">solid</span> <span class=\"mh\">#000</span><span class=\"p\">;</span>\n    <span class=\"k\">background-color</span><span class=\"p\">:</span> <span class=\"mh\">#000</span><span class=\"p\">;</span>\n    <span class=\"k\">color</span><span class=\"p\">:</span> <span class=\"mh\">#fff</span><span class=\"p\">;</span>\n    <span class=\"k\">font-size</span><span class=\"p\">:</span> <span class=\"mi\">1</span><span class=\"kt\">rem</span><span class=\"p\">;</span>\n    <span class=\"k\">font-weight</span><span class=\"p\">:</span> <span class=\"kc\">bold</span><span class=\"p\">;</span>\n    <span class=\"k\">width</span><span class=\"p\">:</span> <span class=\"mi\">4</span><span class=\"kt\">rem</span><span class=\"p\">;</span>\n<span class=\"p\">}</span>\n</pre><p>Here you styled each of the three children: h1, h2, and button of the parent <code>div.TimerView</code>. </p><p>Import your style sheet at the top of <code>TimerView.js</code></p><pre><span class=\"kr\">import</span> <span class=\"s1\">'./TimerView.css'</span>\n</pre>"},{"id":"T0E6OlNlY3Rpb24tMTE0OTY=","title":"Product So far","htmlContent":"<p>Now go to your browser, and you should see something like the following when you load the page. It's not pretty (we'll fix that later) but Make sure you can create a timer and that it looks like the below screenshots:</p><p><strong>Home Screen (tiny new timer button at the bottom)</strong>\n<a href=\"https://cdn.jsdelivr.net/gh/MakeSchool-Tutorials/web-7-react-redux-timers-app@549335c706f2ac39b491d59065d65320e7abb511/P06-List-Timers/assets/home.png\" target=\"_blank\">\n          <img src=\"https://cdn.jsdelivr.net/gh/MakeSchool-Tutorials/web-7-react-redux-timers-app@549335c706f2ac39b491d59065d65320e7abb511/P06-List-Timers/assets/home.png\" alt=\"home\" title=\"\">\n        </a></p><p><strong>Creating a new timer named \"foo\"</strong>\n<a href=\"https://cdn.jsdelivr.net/gh/MakeSchool-Tutorials/web-7-react-redux-timers-app@549335c706f2ac39b491d59065d65320e7abb511/P06-List-Timers/assets/new-timer.png\" target=\"_blank\">\n          <img src=\"https://cdn.jsdelivr.net/gh/MakeSchool-Tutorials/web-7-react-redux-timers-app@549335c706f2ac39b491d59065d65320e7abb511/P06-List-Timers/assets/new-timer.png\" alt=\"new timer\" title=\"\">\n        </a></p><p>Currently our timers won't start when we press them, but the start/stop button should still change text when you press it</p><div class=\"action\">\n<p></p>\n\n<p>Make sure your star/stop button changes text when you press it. If it doesn't go back through this chapter and make sure everything matches up. Now is a great time to practice your debugging skills!</p>\n</div><p>Whew, that was a lot, but we've now <strong>built another two React components with JSX syntax,</strong> and also got to see <strong>data/state be passed using the Flux pattern!</strong> Now if only our timers actually...timed anything! We'll fix that in the next chapter but first...</p>"},{"id":"T0E6OlNlY3Rpb24tMTE0OTc=","title":"Now Commit","htmlContent":"<div class=\"action\">\n<p></p>\n<pre>$ git add .\n$ git commit -m <span class=\"s1\">'added list of timers'</span>\n$ git push\n</pre>\n</div>"},{"id":"T0E6OlNlY3Rpb24tMTE0OTg=","title":"Stretch Challenge","htmlContent":"<div class=\"challenge\">\n<p></p>\n\n<p>What if we want to delete a timer? Add functionality so that we can delete a timer from the list.</p>\n</div>"}]},"next":{"id":"T0E6OlBhZ2UtMjI2MA==","slug":"react-redux-timers-keeping-time","title":"Keeping Time"},"previous":{"id":"T0E6OlBhZ2UtMjI2MA==","slug":"react-redux-timers-keeping-time","title":"Keeping Time"}},{"id":"T0E6OlBhZ2UtMjI2MA==","title":"Keeping Time","slug":"react-redux-timers-keeping-time","sections":{"nodes":[{"id":"T0E6OlNlY3Rpb24tMTE0OTk=","title":"Keeping Time","htmlContent":"<p>So we have everything displaying, but the timers still aren't running. It's about time we fixed that and actually gave the start/stop buttons something to do!</p><h2>Technical Planning</h2><ol>\n<li><del>Build a Timer object</del></li>\n<li><del>Define the Actions of a Timer</del></li>\n<li><del>Define the Reducers of a Timer</del></li>\n<li><del>Allow users to create a Timer</del></li>\n<li><del>Allow users to see a list of Timers</del></li>\n<li>\n<strong>Users should be able to start/stop the clock on their Timers</strong>\n\n<ol>\n<li>Learn how to properly track the delta time for each timer</li>\n<li>Create an Update action that updates the time for a given timer</li>\n<li>Insure your reducer knows how to properly handle your update action</li>\n<li>Tell the store to run Update so that the view can display the accurate time</li>\n</ol>\n</li>\n<li>Style the app</li>\n<li>Allow Timers to persist</li>\n</ol>"},{"id":"T0E6OlNlY3Rpb24tMTE1MDA=","title":"Update Action","htmlContent":"<p>Currently we have all these actions associated with timers, but nothing that tells them how to update the time on the views. We need to build out an update action so that the timers can make sure to regularly update their time. This action Object only needs a type, and the payload should be the number of milliseconds since the last update.</p><div class=\"action\">\n<p></p>\n\n<p>In <code>/src/actions/index.js</code>, add the following UPDATE action, and action creator:</p>\n<pre><span class=\"p\">...</span>\n<span class=\"kr\">export</span> <span class=\"kr\">const</span> <span class=\"nx\">UPDATE</span> <span class=\"o\">=</span> <span class=\"s2\">\"UPDATE\"</span>\n\n<span class=\"kr\">export</span> <span class=\"kr\">const</span> <span class=\"nx\">update</span> <span class=\"o\">=</span> <span class=\"p\">(</span><span class=\"nx\">deltaTime</span><span class=\"p\">)</span> <span class=\"p\">=&gt;</span> <span class=\"p\">{</span>\n  <span class=\"k\">return</span> <span class=\"p\">{</span>\n    <span class=\"nx\">type</span><span class=\"o\">:</span> <span class=\"nx\">UPDATE</span><span class=\"p\">,</span>\n    <span class=\"nx\">payload</span><span class=\"o\">:</span> <span class=\"p\">{</span> <span class=\"nx\">deltaTime</span> <span class=\"p\">}</span>\n  <span class=\"p\">}</span>\n<span class=\"p\">}</span>\n\n<span class=\"p\">...</span>\n</pre>\n</div>"},{"id":"T0E6OlNlY3Rpb24tMTE1MDE=","title":"Quick Aside on Delta Time","htmlContent":"<p>Wow hang on, what's <code>deltaTime</code>? You're going to be using <code>deltaTime</code> to help calculate what time the timer should be displaying.</p><p>The CPU is in charge of handling a <em>lot</em> of stuff, so while keeping time sounds simple, it's hard for the CPU to do it accurately given all the other processes it has on its plate.</p><p>This means something like the following code won't keep an accurate sense of time:</p><pre><span class=\"kd\">var</span> <span class=\"nx\">secs</span> <span class=\"o\">=</span> <span class=\"mi\">0</span>\n<span class=\"nx\">setInterval</span><span class=\"p\">(()</span> <span class=\"p\">=&gt;</span> <span class=\"p\">{</span>\n  <span class=\"nx\">secs</span> <span class=\"o\">+=</span> <span class=\"mi\">1</span>\n  <span class=\"nx\">console</span><span class=\"p\">.</span><span class=\"nx\">log</span><span class=\"p\">(</span><span class=\"nx\">secs</span><span class=\"p\">)</span>\n  <span class=\"c1\">// do something every ~second</span>\n<span class=\"p\">},</span> <span class=\"mi\">1000</span><span class=\"p\">)</span>\n</pre><p>You can actually see this happen yourself in this <a href=\"https://repl.it/@MitchellHudson/setInterval-delta-time\" target=\"_blank\">example</a>.</p><div class=\"info\">\n<p></p>\n\n<p>The <code>setInterval</code> method provides a callback that repeats forever\nafter a designated interval:\n<code>setInterval(callback, milliseconds)</code></p>\n\n<p>If you wanted to just do a one time callback, you would use <code>setTimeOut</code>, which takes the same parameters</p>\n</div><p>A better method is to <em>keep track of the time that has elapsed\nin milliseconds by getting the difference in in time between\ncallbacks from <code>setInterval</code>.</em> This called <strong>delta time.</strong></p><p>The method below gives a very accurate representation\nof time:</p><pre><span class=\"kd\">var</span> <span class=\"nx\">time</span> <span class=\"o\">=</span> <span class=\"mi\">0</span>\n<span class=\"kd\">var</span> <span class=\"nx\">lastUpdate</span> <span class=\"o\">=</span> <span class=\"nb\">Date</span><span class=\"p\">.</span><span class=\"nx\">now</span><span class=\"p\">()</span>\n\n<span class=\"nx\">setInterval</span><span class=\"p\">(()</span> <span class=\"p\">=&gt;</span> <span class=\"p\">{</span>\n  <span class=\"kd\">var</span> <span class=\"nx\">now</span> <span class=\"o\">=</span> <span class=\"nb\">Date</span><span class=\"p\">.</span><span class=\"nx\">now</span><span class=\"p\">()</span>\n  <span class=\"kd\">var</span> <span class=\"nx\">dt</span> <span class=\"o\">=</span> <span class=\"nx\">now</span> <span class=\"o\">-</span> <span class=\"nx\">lastUpdate</span>\n  <span class=\"nx\">time</span> <span class=\"o\">+=</span> <span class=\"nx\">dt</span>\n  <span class=\"nx\">lastUpdate</span> <span class=\"o\">=</span> <span class=\"nx\">now</span>\n  <span class=\"nx\">console</span><span class=\"p\">.</span><span class=\"nx\">log</span><span class=\"p\">(</span><span class=\"nx\">count</span><span class=\"p\">,</span> <span class=\"nx\">dt</span><span class=\"p\">,</span> <span class=\"nx\">time</span><span class=\"p\">)</span>\n<span class=\"p\">},</span> <span class=\"mi\">1000</span><span class=\"p\">)</span>\n</pre><p>Here time is calculated as such:</p><ol>\n<li>Getting the current time in ms (<code>Date.now()</code>)</li>\n<li>Finding the difference in time since the last update (<code>now - lastUpdate</code>). This is delta time and is the number of milliseconds since the last update.</li>\n<li>Updating the time by the delta (<code>time += dt</code>)</li>\n<li>Updating <code>lastUpdated</code> to be the current time (<code>lastUpdate = now</code>)</li>\n</ol><div class=\"info\">\n<p></p>\n\n<p>The <code>Date</code> object creates a <code>Date</code> instance which represents a single moment in time. <code>Dates</code> are stored as an integer value equal to the number of seconds since January 1, 1970 (the <a href=\"https://en.wikipedia.org/wiki/Unix_time\" target=\"_blank\">Unix Epoch</a>).</p>\n\n<p>The Date Object provides a long list of methods that allow you to use for wide range purposes.</p>\n</div>"},{"id":"T0E6OlNlY3Rpb24tMTE1MDI=","title":"Update Reducer","htmlContent":"<p>Armed with this new knowledge on timing, intervals, and dates in JavaScript, let's update our <code>timers-reducer.js</code> file to handle our new Update action:</p><div class=\"action\">\n<p></p>\n\n<p>Update <code>src/reducers/timersReducer.js</code> to handle the new actions 'UPDATE' action.</p>\n\n<p>Import the <code>UPDATE</code> at the top:</p>\n<pre><span class=\"kr\">import</span> <span class=\"p\">{</span> <span class=\"nx\">NEW_TIMER</span><span class=\"p\">,</span> <span class=\"nx\">TOGGLE_TIMER</span><span class=\"p\">,</span> <span class=\"nx\">UPDATE</span> <span class=\"p\">}</span> <span class=\"nx\">from</span> <span class=\"s1\">'../actions'</span><span class=\"p\">;</span>\n</pre>\n<p>Then add another case to your switch statement:</p>\n<pre><span class=\"kr\">const</span> <span class=\"nx\">timerReducer</span> <span class=\"o\">=</span> <span class=\"p\">(</span><span class=\"nx\">state</span> <span class=\"o\">=</span> <span class=\"p\">[],</span> <span class=\"nx\">action</span><span class=\"p\">)</span> <span class=\"p\">=&gt;</span> <span class=\"p\">{</span>\n  <span class=\"k\">switch</span> <span class=\"p\">(</span><span class=\"nx\">action</span><span class=\"p\">.</span><span class=\"nx\">type</span><span class=\"p\">)</span> <span class=\"p\">{</span>\n\n    <span class=\"k\">case</span> <span class=\"nx\">UPDATE</span><span class=\"o\">:</span>\n      <span class=\"k\">return</span> <span class=\"nx\">state</span><span class=\"p\">.</span><span class=\"nx\">map</span><span class=\"p\">((</span><span class=\"nx\">timer</span><span class=\"p\">)</span> <span class=\"p\">=&gt;</span> <span class=\"p\">{</span>\n        <span class=\"k\">if</span> <span class=\"p\">(</span><span class=\"nx\">timer</span><span class=\"p\">.</span><span class=\"nx\">isRunning</span><span class=\"p\">)</span> <span class=\"p\">{</span>\n          <span class=\"nx\">timer</span> <span class=\"o\">=</span> <span class=\"p\">{</span> <span class=\"p\">...</span><span class=\"nx\">timer</span><span class=\"p\">,</span> <span class=\"nx\">time</span><span class=\"o\">:</span> <span class=\"nx\">timer</span><span class=\"p\">.</span><span class=\"nx\">time</span> <span class=\"o\">+=</span> <span class=\"nx\">action</span><span class=\"p\">.</span><span class=\"nx\">payload</span><span class=\"p\">.</span><span class=\"nx\">deltaTime</span> <span class=\"p\">}</span>\n        <span class=\"p\">}</span>\n        <span class=\"k\">return</span> <span class=\"nx\">timer</span>\n      <span class=\"p\">})</span>\n    <span class=\"p\">...</span>\n  <span class=\"p\">}</span>\n<span class=\"p\">}</span>\n</pre>\n</div><p>The above creates a new array of timers. For all timers where the <code>isRunning</code> property is <code>true</code> it makes a new timer Object where <code>time</code> is the old <code>time</code> plus the <code>deltaTime</code> on the payload.</p>"},{"id":"T0E6OlNlY3Rpb24tMTE1MDM=","title":"Update the View","htmlContent":"<p>In some cases (such as our current one) we will want to dispatch actions to the store without sending them from a view. To do this, you need to <em>update the store using a <code>setInterval</code> callback.</em></p><p>Within our callback, we should call <code>store.dispatch(action)</code>, where <code>action</code> is an action object with a type. Following best practices, we should <em>call an action creator to generate the action Object!</em> Good thing we just built that!</p><div class=\"action\">\n<p></p>\n\n<p>Update <code>App.js</code> to update the timers every 50ms:</p>\n<pre><span class=\"p\">...</span>\n<span class=\"c1\">// Import the update action for use</span>\n<span class=\"kr\">import</span> <span class=\"p\">{</span> <span class=\"nx\">update</span> <span class=\"p\">}</span> <span class=\"nx\">from</span> <span class=\"s1\">'./actions'</span>\n<span class=\"p\">...</span>\n\n<span class=\"kr\">const</span> <span class=\"nx\">store</span> <span class=\"o\">=</span> <span class=\"nx\">createStore</span><span class=\"p\">(</span><span class=\"nx\">reducers</span><span class=\"p\">);</span>\n\n<span class=\"kd\">let</span> <span class=\"nx\">lastUpdateTime</span> <span class=\"o\">=</span> <span class=\"nb\">Date</span><span class=\"p\">.</span><span class=\"nx\">now</span><span class=\"p\">()</span>\n<span class=\"nx\">setInterval</span><span class=\"p\">(()</span> <span class=\"p\">=&gt;</span> <span class=\"p\">{</span>\n  <span class=\"kr\">const</span> <span class=\"nx\">now</span> <span class=\"o\">=</span> <span class=\"nb\">Date</span><span class=\"p\">.</span><span class=\"nx\">now</span><span class=\"p\">()</span>\n  <span class=\"kr\">const</span> <span class=\"nx\">deltaTime</span> <span class=\"o\">=</span> <span class=\"nx\">now</span> <span class=\"o\">-</span> <span class=\"nx\">lastUpdateTime</span>\n  <span class=\"nx\">lastUpdateTime</span> <span class=\"o\">=</span> <span class=\"nx\">now</span>\n  <span class=\"nx\">store</span><span class=\"p\">.</span><span class=\"nx\">dispatch</span><span class=\"p\">(</span><span class=\"nx\">update</span><span class=\"p\">(</span><span class=\"nx\">deltaTime</span><span class=\"p\">))</span>\n<span class=\"p\">},</span> <span class=\"mi\">50</span><span class=\"p\">)</span>\n\n<span class=\"p\">...</span>\n</pre>\n</div><p>Open your browser and see if the timers update their time when you click Start. Do they stop when you press Stop?</p><p>Great work so far, but something is still missing...</p>"},{"id":"T0E6OlNlY3Rpb24tMTE1MDQ=","title":"Formatting Time","htmlContent":"<p>Timers can be hard for people to read if they're only displaying the time in milliseconds. Let's fix this so that they show in a proper <code>hour:minute:seconds.milliseconds</code> format:</p><div class=\"action\">\n<p></p>\n\n<p>Create a new folder in <code>/src</code> called <code>/utils</code>. Create a new file <code>/src/utils/index.js</code> and add the following time formatting function:</p>\n<pre><span class=\"kr\">export</span> <span class=\"kr\">const</span> <span class=\"nx\">formatTime</span> <span class=\"o\">=</span> <span class=\"p\">(</span><span class=\"nx\">time</span><span class=\"p\">)</span> <span class=\"p\">=&gt;</span> <span class=\"p\">{</span>\n    <span class=\"kr\">const</span> <span class=\"nx\">ms</span> <span class=\"o\">=</span> <span class=\"nb\">Math</span><span class=\"p\">.</span><span class=\"nx\">round</span><span class=\"p\">(</span><span class=\"nx\">time</span> <span class=\"o\">/</span> <span class=\"mi\">100</span><span class=\"p\">)</span> <span class=\"o\">%</span> <span class=\"mi\">10</span>\n    <span class=\"kr\">const</span> <span class=\"nx\">secs</span> <span class=\"o\">=</span> <span class=\"nb\">Math</span><span class=\"p\">.</span><span class=\"nx\">floor</span><span class=\"p\">(</span><span class=\"nx\">time</span> <span class=\"o\">/</span> <span class=\"mi\">1000</span><span class=\"p\">)</span> <span class=\"o\">%</span> <span class=\"mi\">60</span>\n    <span class=\"kr\">const</span> <span class=\"nx\">mins</span> <span class=\"o\">=</span> <span class=\"nb\">Math</span><span class=\"p\">.</span><span class=\"nx\">floor</span><span class=\"p\">(</span><span class=\"nx\">time</span> <span class=\"o\">/</span> <span class=\"mi\">1000</span> <span class=\"o\">/</span> <span class=\"mi\">60</span><span class=\"p\">)</span> <span class=\"o\">%</span> <span class=\"mi\">60</span>\n    <span class=\"kr\">const</span> <span class=\"nx\">hrs</span> <span class=\"o\">=</span> <span class=\"nb\">Math</span><span class=\"p\">.</span><span class=\"nx\">floor</span><span class=\"p\">(</span><span class=\"nx\">time</span> <span class=\"o\">/</span> <span class=\"mi\">1000</span> <span class=\"o\">/</span> <span class=\"mi\">1000</span> <span class=\"o\">/</span> <span class=\"mi\">60</span><span class=\"p\">)</span> <span class=\"o\">%</span> <span class=\"mi\">60</span>\n    <span class=\"k\">return</span> <span class=\"sb\">`</span><span class=\"si\">${</span><span class=\"nx\">hrs</span><span class=\"si\">}</span><span class=\"sb\">:</span><span class=\"si\">${</span><span class=\"nx\">mins</span><span class=\"si\">}</span><span class=\"sb\">:</span><span class=\"si\">${</span><span class=\"nx\">secs</span><span class=\"si\">}</span><span class=\"sb\">.</span><span class=\"si\">${</span><span class=\"nx\">ms</span><span class=\"si\">}</span><span class=\"sb\">`</span>\n<span class=\"p\">}</span>\n</pre>\n<p>Now use that function in <code>/src/components/TimerView.js</code> to display the time correctly. Remember to import the util:</p>\n\n<p>Import <code>formatTime</code> at the top:</p>\n<pre><span class=\"kr\">import</span> <span class=\"p\">{</span> <span class=\"nx\">formatTime</span> <span class=\"p\">}</span> <span class=\"nx\">from</span> <span class=\"s1\">'../utils'</span><span class=\"p\">;</span>\n</pre>\n<p>Then use <code>formatTime</code> to format the time: </p>\n<pre><span class=\"p\">...</span>\n<span class=\"o\">&lt;</span><span class=\"nx\">h1</span><span class=\"o\">&gt;</span><span class=\"p\">{</span><span class=\"nx\">formatTime</span><span class=\"p\">(</span><span class=\"nx\">timer</span><span class=\"p\">.</span><span class=\"nx\">time</span><span class=\"p\">)}</span><span class=\"o\">&lt;</span><span class=\"err\">/h1&gt;</span>\n<span class=\"p\">...</span>\n</pre>\n</div>"},{"id":"T0E6OlNlY3Rpb24tMTE1MDU=","title":"Product So Far","htmlContent":"<p>Your timers should now look like something like the following:</p><p><a href=\"https://cdn.jsdelivr.net/gh/MakeSchool-Tutorials/web-7-react-redux-timers-app@549335c706f2ac39b491d59065d65320e7abb511/P07-Keeping-Time/assets/format-timers.png\" target=\"_blank\">\n          <img src=\"https://cdn.jsdelivr.net/gh/MakeSchool-Tutorials/web-7-react-redux-timers-app@549335c706f2ac39b491d59065d65320e7abb511/P07-Keeping-Time/assets/format-timers.png\" alt=\"formatted-timers\" title=\"\">\n        </a></p><p>Alright! We've successfully <strong>used timing methods and objects in JavaScript to accurately track time!</strong> And because of it, our timers now work! This is great! They still look a little ugly though, let's spruce them up a bit.</p>"},{"id":"T0E6OlNlY3Rpb24tMTE1MDY=","title":"Now Commit","htmlContent":"<div class=\"action\">\n<p></p>\n<pre>$ git add .\n$ git commit -m <span class=\"s1\">'timers running'</span>\n$ git push\n</pre>\n</div>"},{"id":"T0E6OlNlY3Rpb24tMTE1MDc=","title":"Stretch Challenges","htmlContent":"<div class=\"challenge\">\n<p></p>\n\n<ol>\n<li>Our <code>formatTime</code> function uses a lot of repetitive code. Can you refactor it so that it's written in a way that is more in line with the DRY principle?</li>\n<li>Being able to start/stop a timer is great, but what about being able reset a timer so that we can use it again? Implement a reset button for your timers that resets the time. Reset should stop the timer and then set it back to <code>00:00:00.0</code>\n</li>\n</ol>\n</div>"}]},"next":{"id":"T0E6OlBhZ2UtMjI2MQ==","slug":"react-redux-timers-persisting-timers","title":"Persisting Timers"},"previous":{"id":"T0E6OlBhZ2UtMjI2MQ==","slug":"react-redux-timers-persisting-timers","title":"Persisting Timers"}},{"id":"T0E6OlBhZ2UtMjI2MQ==","title":"Persisting Timers","slug":"react-redux-timers-persisting-timers","sections":{"nodes":[{"id":"T0E6OlNlY3Rpb24tMTE1MDg=","title":"Persisting Timers","htmlContent":"<h2>Technical Planning</h2><ol>\n<li><del>Build a Timer object</del></li>\n<li><del>Define the Actions of a Timer</del></li>\n<li><del>Define the Reducers of a Timer</del></li>\n<li><del>Allow users to create a Timer</del></li>\n<li><del>Allow users to see a list of Timers</del></li>\n<li><del>Users should be able to start/stop the clock on their Timers</del></li>\n<li><del>Style the app</del></li>\n<li>\n<strong>Allow Timers to persist</strong>\n\n<ol>\n<li>Load and save state between the Store and Local Storage</li>\n<li>Throttle the save to local storage to improve performance</li>\n</ol>\n</li>\n</ol><p>Our timers are working, they're styled and looking fly, but they still don't quite do one thing yet: persist! If you refresh the page, or open a new tab on <code>localhost</code>, the timers disappear.</p><p>In this last chapter, we'll fix that by persisting the timers with local storage.</p>"},{"id":"T0E6OlNlY3Rpb24tMTE1MDk=","title":"Overview","htmlContent":"<p>Before you begin, let's take inventory of a few things:</p><ol>\n<li>The Redux store is a JavaScript Object.</li>\n<li>Local storage only allows for strings.</li>\n<li>Therefore, <em>if we can convert the Redux store to JSON, it would be possible to convert the store to local storage and back.</em>\n</li>\n</ol><p>JSON is JavaScript Object Notation. It's a string format that represents a JavasScript object. You can use it as a JS object in most cases and JS objects will convert to JSON most of the time. </p><p>When does it not work? If you're Object contains values that things other than: Strings, Numbers, Arrays, or Objects. What else is left? functions and classes! An Object that contains a function can't be converted to JSON. </p>"},{"id":"T0E6OlNlY3Rpb24tMTE1MTA=","title":"Save and Retrieve the store","htmlContent":"<p>First step is to define a key to be used with local storage. Next we need to define two methods to load and save state from local storage.</p><p>Since it's not related to a a specific component, action, or reducer, our <code>utils</code> folder feels like a good place to put this.</p><div class=\"action\">\n<p></p>\n\n<p>Add the following to the top of the <code>/src/utils/index.js</code> file:</p>\n<pre><span class=\"kr\">const</span> <span class=\"nx\">TMRZ_STATE</span> <span class=\"o\">=</span> <span class=\"s2\">\"TMRZ_STATE\"</span>\n\n<span class=\"c1\">// Load State</span>\n<span class=\"kr\">export</span> <span class=\"kr\">const</span> <span class=\"nx\">loadState</span> <span class=\"o\">=</span> <span class=\"p\">()</span> <span class=\"p\">=&gt;</span> <span class=\"p\">{</span>\n  <span class=\"k\">try</span> <span class=\"p\">{</span>\n  <span class=\"c1\">// Grab the state from local storage</span>\n    <span class=\"kr\">const</span> <span class=\"nx\">serializedState</span> <span class=\"o\">=</span> <span class=\"nx\">localStorage</span><span class=\"p\">.</span><span class=\"nx\">getItem</span><span class=\"p\">(</span><span class=\"nx\">TMRZ_STATE</span><span class=\"p\">)</span>\n    <span class=\"k\">if</span> <span class=\"p\">(</span><span class=\"nx\">serializedState</span> <span class=\"o\">===</span> <span class=\"kc\">null</span><span class=\"p\">)</span> <span class=\"p\">{</span>\n      <span class=\"k\">return</span> <span class=\"kc\">undefined</span>\n    <span class=\"p\">}</span>\n    <span class=\"c1\">// convert the string into JSON for the Redux store</span>\n    <span class=\"k\">return</span> <span class=\"nx\">JSON</span><span class=\"p\">.</span><span class=\"nx\">parse</span><span class=\"p\">(</span><span class=\"nx\">serializedState</span><span class=\"p\">)</span>\n  <span class=\"p\">}</span> <span class=\"k\">catch</span><span class=\"p\">(</span><span class=\"nx\">err</span><span class=\"p\">)</span> <span class=\"p\">{</span>\n    <span class=\"k\">return</span> <span class=\"kc\">undefined</span>\n  <span class=\"p\">}</span>\n<span class=\"p\">}</span>\n\n<span class=\"c1\">// Save State</span>\n<span class=\"kr\">export</span> <span class=\"kr\">const</span> <span class=\"nx\">saveState</span> <span class=\"o\">=</span> <span class=\"p\">(</span><span class=\"nx\">state</span><span class=\"p\">)</span> <span class=\"p\">=&gt;</span> <span class=\"p\">{</span>\n  <span class=\"k\">try</span> <span class=\"p\">{</span>\n    <span class=\"c1\">// convert the state from JSON, into a string</span>\n    <span class=\"kr\">const</span> <span class=\"nx\">serializedState</span> <span class=\"o\">=</span> <span class=\"nx\">JSON</span><span class=\"p\">.</span><span class=\"nx\">stringify</span><span class=\"p\">(</span><span class=\"nx\">state</span><span class=\"p\">)</span>\n    <span class=\"c1\">// save the state to local storage</span>\n    <span class=\"nx\">localStorage</span><span class=\"p\">.</span><span class=\"nx\">setItem</span><span class=\"p\">(</span><span class=\"nx\">TMRZ_STATE</span><span class=\"p\">,</span> <span class=\"nx\">serializedState</span><span class=\"p\">)</span>\n  <span class=\"p\">}</span> <span class=\"k\">catch</span><span class=\"p\">(</span><span class=\"nx\">err</span><span class=\"p\">)</span> <span class=\"p\">{</span>\n    <span class=\"nx\">console</span><span class=\"p\">.</span><span class=\"nx\">log</span><span class=\"p\">(</span><span class=\"s2\">\"Error saving data\"</span><span class=\"p\">)</span>\n  <span class=\"p\">}</span>\n<span class=\"p\">}</span>\n<span class=\"p\">...</span>\n</pre>\n</div><p>To persist state subscribe to changes from the <code>store</code> in <code>App.js</code>. </p><div class=\"action\">\n<p></p>\n\n<p>Import the methods from <code>/util</code> and add the following code to <code>App.js</code>. <strong>note:</strong> you need to replace the existing definition of <code>store</code> with the following:</p>\n<pre><span class=\"kr\">import</span> <span class=\"p\">{</span> <span class=\"nx\">loadState</span><span class=\"p\">,</span> <span class=\"nx\">saveState</span> <span class=\"p\">}</span> <span class=\"nx\">from</span> <span class=\"s1\">'./utils'</span>\n\n<span class=\"p\">...</span>\n\n<span class=\"kr\">const</span> <span class=\"nx\">persistedState</span> <span class=\"o\">=</span> <span class=\"nx\">loadState</span><span class=\"p\">()</span>\n<span class=\"kr\">const</span> <span class=\"nx\">store</span> <span class=\"o\">=</span> <span class=\"nx\">createStore</span><span class=\"p\">(</span><span class=\"nx\">reducers</span><span class=\"p\">,</span> <span class=\"nx\">persistedState</span><span class=\"p\">)</span>\n<span class=\"nx\">store</span><span class=\"p\">.</span><span class=\"nx\">subscribe</span><span class=\"p\">(()</span> <span class=\"p\">=&gt;</span> <span class=\"p\">{</span>\n  <span class=\"nx\">saveState</span><span class=\"p\">(</span><span class=\"nx\">store</span><span class=\"p\">.</span><span class=\"nx\">getState</span><span class=\"p\">())</span>\n<span class=\"p\">})</span>\n</pre>\n</div>"},{"id":"T0E6OlNlY3Rpb24tMTE1MTE=","title":"Needs More Throttle","htmlContent":"<p>In this arrangement <code>saveState()</code> is being called each time the store changes. Since the timers update the store every 50ms, this could cause performance issues if we're writing to local storage every 50ms as well. We can solve this with a <strong>throttling method</strong>. This is a method that limits the calls to other methods over time.</p><p>It might be possible to improve performance by throttling calls to <code>saveState()</code>. <a href=\"https://lodash.com/\" target=\"_blank\">Lodash</a> has a throttle method we can use to help us out.</p><div class=\"action\">\n<p></p>\n\n<p>Install Lodash via npm:</p>\n<pre>$ npm i --save lodash\n</pre>\n</div><p>Lodash has many methods. By default we will get the whole library, but let's just import only the part that we need, in this case <code>throttle</code>.</p><div class=\"action\">\n<p></p>\n\n<p>import lodash's throttle method into <code>App.js</code></p>\n<pre><span class=\"kr\">import</span> <span class=\"nx\">throttle</span> <span class=\"nx\">from</span> <span class=\"s1\">'lodash/throttle'</span>\n</pre>\n</div><p>Finally, let's tell our store to save state every 1000ms using Lodash's throttle method:</p><div class=\"action\">\n<p></p>\n\n<p>Replace the current <code>store.subscribe</code> call with the following in <code>App.js</code>:</p>\n<pre><span class=\"nx\">store</span><span class=\"p\">.</span><span class=\"nx\">subscribe</span><span class=\"p\">(</span><span class=\"nx\">throttle</span><span class=\"p\">(()</span> <span class=\"p\">=&gt;</span> <span class=\"p\">{</span>\n  <span class=\"nx\">saveState</span><span class=\"p\">(</span><span class=\"nx\">store</span><span class=\"p\">.</span><span class=\"nx\">getState</span><span class=\"p\">())</span>\n<span class=\"p\">},</span> <span class=\"mi\">1000</span><span class=\"p\">));</span>\n</pre>\n</div><p>Now the timers are updating every 50ms, but state is only being written to local store every 1000ms.</p><p>Try refreshing the tab or opening a new tab on <code>localhost</code> and see your timers persist!</p><p>Congrats! We got some practice managing local storage in the context of <strong>managing application state through the Flux pattern!</strong> Not to mention some pretty sweet timers to boot.</p><p>As you go through this FEW course, try to tie the concepts you learned here into the course material. The foundational work here on building React apps will be used to make bigger and bolder apps down the road!</p>"},{"id":"T0E6OlNlY3Rpb24tMTE1MTI=","title":"Now Commit","htmlContent":"<div class=\"action\">\n<p></p>\n<pre>$ git add .\n$ git commit -m <span class=\"s1\">'persisting timers'</span>\n$ git push\n</pre>\n</div>"},{"id":"T0E6OlNlY3Rpb24tMTE1MTM=","title":"Further challenges","htmlContent":"<p>This app is small in scope but covers a wide range of technologies and has room to expand. Here are some ideas that you can explore. </p><ul>\n<li>The styles and uder interface could use improvements. Each peice of the interface is built as a component and each component has it's own stylesheet. </li>\n<li>The state of each timer is on state and available to each component. It's possible to use this to change the appearance of each component. Imagine the start/stop button appears Green when it says \"Start\" and red when says \"stop\".</li>\n<li>Timers have a title and a time. It might be good if they had description below the title. To do this you'll need to modify the application state and interface. \n\n<ul>\n<li>Add a new input for the description</li>\n<li>The Timer class stores the title and time you'll need to expand this to support a description property. </li>\n<li>When creating a new Timer you're calling the NEW_TIMER/addTimer action you'll need to pass the description through here. </li>\n<li>A timer is created in the timerReducer modify this to add the new description. </li>\n</ul>\n</li>\n<li>It might be useful to have a reset button to reset a timer back to 0 for some uses.\n\n<ul>\n<li>Add a button to the TimerView</li>\n<li>Create a new action RESET_TIMER and action creator function resetTimer.You'll need the index of the timer. Follow the pattern used by the start/stop buttons. </li>\n<li>Dispatch this action from your button.</li>\n<li>Handle thie action in your timersReducer. </li>\n</ul>\n</li>\n<li>The times on all of the timers is data and reprents how much time was measured in a range of categories. You can create a new component to display this. Imagine a graph or a pie/donut chart showing the amount of time spent on each category. </li>\n</ul>"},{"id":"T0E6OlNlY3Rpb24tMTE1MTQ=","title":"Feedback and Review - 2 minutes","htmlContent":"<p><strong>We promise this won't take longer than 2 minutes!</strong></p><p>Please take a moment to rate your understanding of learning outcomes from this tutorial, and how we can improve it via our <a href=\"https://goo.gl/forms/uytCya0slBpsOXPf2\" target=\"_blank\">tutorial feedback form</a></p><p>This allows us to get feedback on how well the students are grasping the learning outcomes, and tells us where we can improve the tutorial experience.</p>"}]},"next":null,"previous":null}]}},"page":{"id":"T0E6OlBhZ2UtMjI1OA==","title":"Create New Timers","slug":"react-redux-timers-create-new-timers","sections":{"nodes":[{"id":"T0E6OlNlY3Rpb24tMTE0ODg=","title":"Create New Timers","htmlContent":"<p>So far we have been working on getting Redux and React up and\nrunning and no work has gone into components, which is the heart of React!</p><p>With actions and reducers in place you can now create\ncontainers/components that display state from the store and\nsend actions to update the store. Let's get those working!</p><h2>Technical Planning</h2><ol>\n<li><del>Build a Timer object</del></li>\n<li><del>Define the Actions of a Timer</del></li>\n<li><del>Define the Reducers of a Timer</del></li>\n<li>\n<strong>Allow users to create a Timer</strong>\n\n<ol>\n<li>Create a <code>new-timer</code> component</li>\n<li>Allow that component to use the addTimer action</li>\n<li>Allow for input of a name for the timer</li>\n<li>Save the name of the timer</li>\n<li>Build a button to allow the user to add a timer</li>\n</ol>\n</li>\n<li>Allow users to see a list of Timers</li>\n<li>Users should be able to start/stop the clock on their Timers</li>\n<li>Style the app</li>\n<li>Allow Timers to persist</li>\n</ol>"},{"id":"T0E6OlNlY3Rpb24tMTE0ODk=","title":"New Timer Component","htmlContent":"<p>The first component we'll build is for creating new timers. The new timer component should have the following:</p><ul>\n<li>A field to input a name</li>\n<li>A button to save the timer</li>\n</ul><div class=\"info\">\n<p></p>\n\n<p>The above field that will allow users to input a name is a <strong>controlled component</strong>. This is when \"the React component that renders [an input] also controls what happens in that form on subsequent user input.\"</p>\n\n<p>You can read more about controlled components <a href=\"https://reactjs.org/docs/forms.html#controlled-components\" target=\"_blank\">here</a></p>\n</div><p>Keep in mind that we won't see timers until the Timer list component is created, which will be built in the next chapter.</p><div class=\"action\">\n<p></p>\n\n<p>Create a new folder in <code>src</code> called <code>components</code>. Now within this folder, create a <code>NewTimer.js</code> file which will house our new-timer component</p>\n\n<p>Fill in <code>/src/components/NewTimer.js</code> with the following starter code</p>\n<pre><span class=\"kr\">import</span> <span class=\"nx\">React</span><span class=\"p\">,</span> <span class=\"p\">{</span> <span class=\"nx\">useState</span> <span class=\"p\">}</span> <span class=\"nx\">from</span> <span class=\"s1\">'react'</span>\n<span class=\"kr\">import</span> <span class=\"p\">{</span> <span class=\"nx\">useDispatch</span> <span class=\"p\">}</span> <span class=\"nx\">from</span> <span class=\"s1\">'react-redux'</span>\n\n<span class=\"c1\">// We need to import our action to add a new timer</span>\n<span class=\"kr\">import</span> <span class=\"p\">{</span> <span class=\"nx\">addTimer</span> <span class=\"p\">}</span> <span class=\"nx\">from</span> <span class=\"s1\">'../actions'</span>\n\n<span class=\"kr\">export</span> <span class=\"k\">default</span> <span class=\"kd\">function</span> <span class=\"nx\">NewTimer</span><span class=\"p\">()</span> <span class=\"p\">{</span>\n    <span class=\"kr\">const</span> <span class=\"p\">[</span> <span class=\"nx\">name</span><span class=\"p\">,</span> <span class=\"nx\">setName</span> <span class=\"p\">]</span> <span class=\"o\">=</span> <span class=\"nx\">useState</span><span class=\"p\">(</span><span class=\"s1\">''</span><span class=\"p\">)</span>\n    <span class=\"kr\">const</span> <span class=\"nx\">dispatch</span> <span class=\"o\">=</span> <span class=\"nx\">useDispatch</span><span class=\"p\">()</span>\n\n    <span class=\"k\">return</span> <span class=\"p\">(</span>\n        <span class=\"o\">&lt;</span><span class=\"nx\">div</span><span class=\"o\">&gt;</span>\n</pre>\n</div>\n<pre>    <span class=\"p\">&lt;/</span><span class=\"nt\">div</span><span class=\"p\">&gt;</span>\n)\n</pre><p>}\n```</p><p>You now have a basic <code>NewTimer</code> component structure, but we have no idea how to render it! Remember we want to have the following:</p><ul>\n<li>A field to input a name</li>\n<li>A button to save the timer</li>\n</ul><p>Let's build that out in the render function:</p><div class=\"action\">\n<p></p>\n\n<p>Fill in the <code>render</code> method in <code>/src/components/NewTimer.js</code>, as well as adding a few other ancillary methods and an export statement:</p>\n<pre><span class=\"kr\">export</span> <span class=\"k\">default</span> <span class=\"kd\">function</span> <span class=\"nx\">NewTimer</span><span class=\"p\">()</span> <span class=\"p\">{</span>\n    <span class=\"kr\">const</span> <span class=\"p\">[</span> <span class=\"nx\">name</span><span class=\"p\">,</span> <span class=\"nx\">setName</span> <span class=\"p\">]</span> <span class=\"o\">=</span> <span class=\"nx\">useState</span><span class=\"p\">(</span><span class=\"s1\">''</span><span class=\"p\">)</span>\n    <span class=\"kr\">const</span> <span class=\"nx\">dispatch</span> <span class=\"o\">=</span> <span class=\"nx\">useDispatch</span><span class=\"p\">()</span>\n\n    <span class=\"k\">return</span> <span class=\"p\">(</span>\n        <span class=\"o\">&lt;</span><span class=\"nx\">div</span><span class=\"o\">&gt;</span>\n            <span class=\"o\">&lt;</span><span class=\"nx\">input</span>\n                <span class=\"nx\">type</span><span class=\"o\">=</span><span class=\"s1\">'text'</span>\n                <span class=\"nx\">placeholder</span><span class=\"o\">=</span><span class=\"s2\">\"New Timer Name\"</span>\n                <span class=\"nx\">name</span><span class=\"o\">=</span><span class=\"s2\">\"name\"</span>\n                <span class=\"nx\">value</span><span class=\"o\">=</span><span class=\"p\">{</span><span class=\"nx\">name</span><span class=\"p\">}</span>\n                <span class=\"nx\">onChange</span><span class=\"o\">=</span><span class=\"p\">{(</span><span class=\"nx\">e</span><span class=\"p\">)</span> <span class=\"p\">=&gt;</span> <span class=\"nx\">setName</span><span class=\"p\">(</span><span class=\"nx\">e</span><span class=\"p\">.</span><span class=\"nx\">target</span><span class=\"p\">.</span><span class=\"nx\">value</span><span class=\"p\">)}</span><span class=\"o\">/&gt;</span>\n            <span class=\"o\">&lt;</span><span class=\"nx\">button</span>\n                <span class=\"nx\">onClick</span><span class=\"o\">=</span><span class=\"p\">{()</span> <span class=\"p\">=&gt;</span> <span class=\"nx\">dispatch</span><span class=\"p\">(</span><span class=\"nx\">addTimer</span><span class=\"p\">(</span><span class=\"nx\">name</span><span class=\"p\">))}</span>\n            <span class=\"o\">&gt;</span><span class=\"nx\">Save</span><span class=\"o\">&lt;</span><span class=\"err\">/button&gt;</span>\n        <span class=\"o\">&lt;</span><span class=\"err\">/div&gt;</span>\n    <span class=\"p\">)</span>\n<span class=\"p\">}</span>\n</pre>\n</div><ul>\n<li>UseDispatch - will give you access to the Redux store and allow your component to send actions when you need to update the store.</li>\n<li>useState - is used to create a value that will be available across multiple renders of this component. </li>\n</ul><p>First React component in the bag! Great work on <strong>building a react component using JSX syntax</strong>, as well as getting more practice <strong>managing application state</strong>. But we can't stop here, let's build another component!</p>"},{"id":"T0E6OlNlY3Rpb24tMTE0OTA=","title":"Now Commit","htmlContent":"<div class=\"action\">\n<p></p>\n<pre>$ git add .\n$ git commit -m <span class=\"s1\">'added new timer input'</span>\n$ git push\n</pre>\n</div>"}]},"next":{"id":"T0E6OlBhZ2UtMjI1OQ==","slug":"react-redux-timers-list-timers","title":"List Timers"},"previous":{"id":"T0E6OlBhZ2UtMjI1OQ==","slug":"react-redux-timers-list-timers","title":"List Timers"}}}},"staticQueryHashes":[]}