02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

Making Things Dynamic with Redux

Making Things Dynamic with Redux

In this tutorial, we'll explore how to make our React applications more dynamic by using Redux to add counters, combine reducers, and create a more sophisticated reducer structure. By the end, you'll be able to create dynamic components that interact seamlessly with Redux. Let's dive in!

Introduction

In this section, we're going to make things more dynamic by adding counters to our application. We'll be combining reducers to create a more complex structure, and you'll learn how to deal with scenarios where you need multiple reducers in a single store.

Overview of Redux Philosophy

Redux encourages having a single store for the entire application. In this tutorial, we'll look at how to achieve that goal while using multiple reducers to maintain a clean and modular codebase.

Adding Counters Dynamically

To get started, we're going to add counters dynamically to our application. Here's a breakdown of what we'll cover:

  1. Combining Reducers: We'll create a more sophisticated reducer structure by combining multiple reducers.
  2. Managing a Single Store: You'll learn how to manage everything in a single store, even with multiple reducers.
  3. Deep Freezing Objects: We'll look at freezing objects to ensure immutability.
  4. Creating a Dynamic Sketch Board: We'll build a sketch board application to display dynamic content from a data source.

Step-by-Step Guide

  1. Adding Counters

    • We start by creating counters dynamically using Redux. We're going to delete the existing counters and replace them with a method that allows us to add new counters dynamically.
    • The philosophy here is to create an application where counters can be easily added or removed by dispatching actions.
  2. Combining Reducers

    • We use multiple reducers to handle different parts of our state. Instead of using several stores, we combine them into a single store. Redux’s combineReducers function will help us merge different reducer functions into a unified structure.
  3. Deep Freeze Objects

    • In previous tutorials, we discussed how to deep freeze arrays to ensure that they remain immutable. In this tutorial, we'll take a closer look at freezing objects as well.

Example: Adding a Counter

Here's a snippet of the code where we add a new counter:

export const addCounter = () => {
  return {
    type: 'ADD_COUNTER'
  };
};

By using this action creator, we can dispatch the ADD_COUNTER action to our store. This action will modify the existing state by adding a new counter.

Using React Components

We then proceed to make our components dynamic by mapping over an array of counters and creating them in a loop.

const counters = this.props.counters.map((counter, index) => (
  <Counter key={index} value={counter} />
));

This approach makes it easy to dynamically add and display counters in our application.

Final Implementation

By the end of this section, we’ll have a fully functional, dynamic counter application. We'll use a button to add new counters, and every time a button is clicked, a new counter will appear on the screen.

<button onClick={this.addCounter}>Add Counter</button>

We'll then use the store's dispatch method to add new counters, allowing us to interact with the state dynamically.

Conclusion

In this tutorial, you learned how to:

  • Dynamically add counters using Redux.
  • Combine reducers to create a modular state management system.
  • Use deep freezing techniques to maintain immutability.

These concepts are essential for building dynamic, scalable applications with React and Redux. Now that you've learned how to add counters dynamically, you can apply these techniques to other aspects of your application to make them more flexible and responsive.

Let's keep building! In the next lecture, we'll look at how to combine reducers more intricately and create a dynamic checkboard application.

Video Transcript

00:01This whole next section is about making
00:03things more dynamic.
00:04And with that said, we're gonna be adding
00:06counters dynamically into our application.
00:09We're going com. We're going to combine reducers
00:12to create a more complex reducer structure with Redux.
00:16And also to show you how do you deal with the scenarios
00:18where you have supposedly a few stores.
00:21So the philosophy behind Redux is to have only one store.
00:24So we're gonna see how do we create
00:26that one store using multiple reducers.
00:29We're also gonna look at how to deep freeze objects.
00:32We saw a raise in the previous section.
00:34And then this section we'll take a, a stab at objects
00:37and we're also gonna build out a, uh, a checkboard,
00:40you could say, kind of a sketch board application
00:43through dynamic content that will come from our data source.
00:47So with that said, let's jump right into it
00:48and let's go into our first lecture
00:50where we're gonna be adding our counters to be dynamic
00:52and we're gonna go back into react.
00:54We're going to use our reducers
00:57and we're gonna be mapping our components
00:59to create our components dynamically.
01:02Alright, let's make things a little bit more complicated.
01:05And the next step is I want
01:06to add our counter in dynamically.
01:09Well, 'cause I wanna have maybe just for the sake really of,
01:11of showcasing um, how do you connect all these webs together
01:16and to kind of show how to create dynamic elements
01:18and react as well if we're already going through this path.
01:21So for one, I'm just gonna go ahead
01:23and delete two of our three counters or even, let's go ahead
01:27and just get rid of all of our counters.
01:29Now what I'm gonna do next is
01:31because I got rid of all of my counters, I definitely need
01:34to have a way to add new counters in
01:36that is a given, a mandatory.
01:38And as such, let's go ahead and start that uh, process.
01:42So let's go ahead and we're gonna export a new constant
01:46and I'm gonna call it add counter.
01:49And I'm gonna call this add counter.
01:52And let's go ahead and add a new case.
01:54Let's make that our first case, not
01:56that the order matters add counter.
02:00And for our out counter, what are we actually gonna do?
02:02Well what we're technically going
02:03to do is we're gonna return our existing array
02:07by creating a new array.
02:08And I'm gonna put inside of it our existing array, the whole
02:13spiel and adding a new position.
02:15And that's it. That is gonna be my adding counter.
02:18Now let's go ahead and also test this out.
02:20So inside of my store itself, I also wanna make sure
02:23that our initial state by the way,
02:25does not equal that anymore.
02:26But what we could do is just go ahead here in
02:29our initialization.
02:31I'm just gonna go ahead and add three by default
02:34as we're testing things out.
02:35So to do that again, I'm gonna go ahead
02:37and call in our new counter action.
02:42So let's go ahead and pull in that variable
02:45and then I'm just gonna go ahead and dispatch a new event.
02:49Um, a new action.
02:51And I probably wanna do that three times
02:53because I want to actually put inside three counters.
02:56So let's go ahead and do that.
02:58And instead of calling step up,
03:00we're gonna call the add counter.
03:06And let's go ahead and also change our description.
03:09So, um, starting up with three counters.
03:14So let's go ahead and say this
03:15and go back into our terminal to see if we have any errors.
03:18So we'll see that we have your one failure
03:20and let's go ahead and that is, well, we don't even have
03:23to go into it 'cause we know that it's
03:24coming from the app itself.
03:26Um, and that is because we didn't actually Oh what? No.
03:30Oh yeah, yeah, yeah. Because now we have a few lines.
03:32I've done it again. Who would've thunk? Okay, here you go.
03:36So let's uh, open and close a curly bracket for that.
03:39And now we could have a semicolon here
03:41'cause now we have multiple items.
03:43Okay, so let's go ahead
03:44and check to see if we have any errors.
03:46Beautiful. We have everything passed.
03:48That's exactly what we hope for it to happen.
03:51So now let's go ahead and implement it within our actual
03:54application itself.
03:55And to do that I'm gonna go into my app itself
03:58and literally I'm just gonna toss your somewhere a button
04:00just for the sake of our presentation right now.
04:04So let's just have your A button
04:05and I'll just call this add.
04:07And what else do we want to do?
04:09We need it to have an on click.
04:12And for our on click, we want to make sure
04:15that we're calling, let's say this dot add counter.
04:18Uh, we don't really care within that function to keep scope
04:22'cause we're not referring to any properties at this point
04:24because literally we're gonna add a new one.
04:26It doesn't really matter how many exist already.
04:28So let's go ahead and create that method really quickly.
04:31Add counter. And for our out counter, literally
04:35what we're going to do is again, we're going
04:37to need to call our store.
04:38We don't have a reference to the store currently,
04:40so we're gonna have to fetch in our store.
04:43So let me go ahead and just paste that right in there.
04:46And instead of step up
04:47and step down, what we actually want
04:50to call is our add counter
04:53and let's put that right in there.
04:55And last but not least, we could grab our store dispatch,
05:00add counter, boom and let's place that right in there.
05:06And last but not least, we also need to make sure
05:08that we're actually displaying the counters that exist.
05:11So to do that, let's go ahead.
05:13So for our data object itself, I'm literally,
05:15what I'm gonna do is I'm gonna create here a new data map
05:19running through cycling through each item
05:21inside of our array.
05:23And each item has a value and it also has an index.
05:27And we're gonna go ahead here
05:29and literally return that same counter that we had
05:32that wasn't dynamic only.
05:34We're gonna make a couple of small changes.
05:36So for one, lemme just go ahead and delete all this.
05:38So literally what's happening here is I created a i I I'm
05:42basically returning the return value from mapping our array.
05:46Now if you haven't worked with Map Before Map,
05:49what it does is it actually cycles
05:50through each item in our array to create a new array per se.
05:54And in our case, we're returning each time a new counter
05:58and, and its data will be this data value,
06:00which is RV itself, which is the value itself
06:03and our index, which we wanna put for our index.
06:07Last but not least, if we run this,
06:08we're gonna get a small error and we want to fix that
06:11and that error is involved.
06:12First of all, this error is
06:14that we are not referring to our store.
06:16So that's not the error I was intending.
06:18So let's go ahead and say that.
06:20But the real error that we're gonna encounter is
06:22that when we add items, it's gonna tell us, Hey, by the way,
06:24you should really have a key to each item
06:27that is dynamically created, that comes
06:29through some sort of an iteration.
06:31So to address that, all we have to do is also
06:33besides having our key, which is a property
06:35that our counter expects, uh, sorry, the index,
06:38which is a property our counter exists.
06:40I'm gonna use that same index
06:42'cause it is unique within the scope of this div that we,
06:46even if we were using other arrays in other areas,
06:48it would still be unique
06:50'cause within this area it would be unique,
06:52which is what's important itself.
06:54And now that we have a unique key for each item,
06:56then when I go back inside of here
06:58and I actually click on to add counters,
07:01our counters should be able to add without any problem.
07:04And as we concatenate
07:06and add, you'll see that the values above grow as well.
07:09So now we've created a very, very dynamic component
07:13and we're ready to move on to talking about more core things
07:17that you want to think about while you're developing
07:19with Redux and React.
07:22In this lecture we made our add counter
07:25to be really dynamic on contrary to what it was so far.
07:28In the next lecture, we're gonna combine reducers with Redux
07:32to create, to start the process of creating a checkboard.

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!