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

Building a Redux Data Structure and Best Practices for Action Types

Building a Redux Data Structure and Best Practices for Action Types

When working with Redux, structuring your data properly is key to managing complex applications. This tutorial will guide you through the process of building a solid Redux data structure while also covering some best practices for organizing action types.

Understanding Your Redux Data Structure

The Redux data structure allows you complete control over what your data will look like. As you define your data structure, remember that the goal is to make it easy to maintain and extend as your application grows.

Good Practices for Data Structuring

  1. Splitting and Structuring File Types: One way to improve your Redux data structure is by splitting and organizing different parts of your application into logical files. For example, separating action types and action creators into different files can make the code easier to maintain and debug.

  2. Creating Helper Functions for Actions: Helper functions can simplify the process of dispatching actions and managing state. By creating these functions, you can reduce redundancy and improve the maintainability of your code.

  3. Extracting Properties and Avoiding Excessive Redux Code: Keep the Redux-related logic as minimal as possible within your core application code. This helps to prevent over-coupling your application logic with Redux, which makes your components more reusable.

Using Context and Provider in React with Redux

Redux can work alongside React's context and provider mechanisms. Here's a breakdown of how you can utilize these features:

  • React Context: Context provides a way to pass data through the component tree without having to manually pass props at every level. It's especially useful when dealing with Redux since it can help simplify state management.
  • Redux Provider: The Provider component makes the Redux store available to your entire React application, ensuring that your components can connect to the store and read the current state or dispatch actions.

In this tutorial, we will learn how to use both React context and the Redux provider to pass data seamlessly through our application.

Extracting Action Types into a Separate File

Organizing your action types into separate files is a simple yet powerful practice. By doing this, you reduce the chances of introducing errors due to typos, and it becomes much easier to maintain your code when adding or updating action types.

Step-by-Step: Extracting Action Types

  1. Create an Empty Directory for Your Store

    • Begin by creating a directory named store.
    • Inside this directory, add a new file for your action types, for example, types.js.
  2. Define Your Action Types

    • In types.js, add all your action types as constants:
      export const ADD_TODO = 'ADD_TODO';
      export const REMOVE_TODO = 'REMOVE_TODO';
      // Add more action types as needed
      
  3. Import Action Types Where Needed

    • Instead of hardcoding strings in your components or action creators, import your action types from the types.js file:
      import { ADD_TODO } from './store/types';
      

By separating your action types, you make your application more modular, and you avoid the risk of inconsistent action names across different parts of your app.

Creating Actions in a Separate File

Another best practice is to move your action creators to a separate file. This makes it easier to manage actions and maintain a clean structure.

Step-by-Step: Creating Action Files

  1. Create an actions.js File

    • In the store directory, create a new file called actions.js.
  2. Define Action Creators

    • Add your action creators to actions.js:
      import { ADD_TODO } from './types';
      
      export const addTodo = (todo) => ({
        type: ADD_TODO,
        payload: todo,
      });
      
  3. Import Actions Where Needed

    • Import your action creators in the relevant components or middleware:
      import { addTodo } from './store/actions';
      

Wrapping Up: Best Practices for Redux

  • Keep Redux-Related Code Separate: Keep Redux-related code separated from your UI components as much as possible. This makes your components more reusable and easier to test.
  • Avoid Overplanning: Start small with your data structure and break it down only as needed. Overplanning can lead to unnecessary complexity.
  • Use Modular Updates: Keep your updates modular. It is often easier to rewrite a few components as your project scales rather than over-engineering from the start.

By following these practices, you'll have a well-structured, maintainable Redux store that can scale with your application's growth.

Next Steps

In the next lecture, we'll take the next steps in organizing our actions by extracting them into their own files and creating a more modular structure for both bound and unbound actions.

Video Transcript

00:00When you're building your redux data structure,
00:02you really have complete control over what
00:04that data structure will be.
00:06And in the following section we're gonna talk about both of,
00:09uh, just general good ideas of
00:11how could you structure your data if it's from splitting
00:14and structuring your file types, if it's by
00:18creating helper functions for your actions
00:20and bound actions, extracting different properties
00:23and making sure that you're not,
00:24that you don't have too much redux related stuff
00:28inside of your application.
00:29And the second half of this se of this section,
00:31we're also gonna talk about the re act reduc
00:34and look at how to use its provider.
00:36We're gonna see how to u how to pass data
00:39through the context, which is a built in kind
00:42of like hidden feature in react.
00:44And last but not least, we're gonna see how
00:45to use the connect method.
00:46So holistically by the end of this section,
00:49you're gonna have a really good picture
00:50of the different ways and the different data strategies of
00:53how do you wanna approach your data
00:55and integrate it with React.
00:57So let's start with our first lecture,
00:58which was splitting out our action
01:00types into their own files.
01:02And what we're gonna do is we're gonna understand inside
01:04of this lecture what are action types.
01:07We've already worked with them quite a lot.
01:09We saw those actions, which are literally objects.
01:12And we're also gonna take a look at those objects
01:14and see how could we structure them in a way
01:16where we're not repeating ourselves too often
01:18and talk about a good data strategy
01:20and organizational strategy.
01:22So let's jump right into it as our application grows
01:25and our data source grows as well,
01:27we'll definitely wanna organize things.
01:30So let's go ahead and start doing that.
01:31Um, and I started off by creating an
01:33empty directory called store.
01:35And in it, what I wanna do is I wanna place in
01:38all of our types.
01:40So lemme go ahead and save that.
01:42So literally what I've done is I've put into our store
01:45literally all the four current existing constants.
01:48So I'm just gonna go ahead and delete them.
01:51And instead of that, what I'm gonna do is I'm gonna import
01:55them into the file, save them as every single one of them
02:00as that as t from store types.
02:06Now these types relate to action types really,
02:08but it's quite explicit the types.
02:10And there's only one type of type in this environment.
02:13So we could keep it to types.
02:15With that said, now that we are using it our,
02:18we extracted out our types, we're gonna reduce the chances
02:21of us typing in something incorrectly
02:24or needing to repeat our imports too often.
02:27So what I want to do next is I wanna literally go ahead
02:30and update every single place
02:32where I was using our constants to go
02:35through our um, t for types.
02:39And I'm gonna go ahead and save that
02:41and I'm probably gonna need to do the exact same thing.
02:43osu, within our actual, any place
02:47where we actually have actions.
02:48So in our board there is no action,
02:51but we do have an action happening inside of our component
02:55counter where we're calling the step up and step down.
02:59Which technically what I'm gonna do instead is I'm gonna go
03:02ahead and not load that from the store anymore
03:05because instead of loading it from the store, I'm gonna want
03:08to import it from
03:13Store types.
03:14Guys, all the rest could remain the same
03:16'cause we've imported the same properties, the same, uh,
03:19actions you could say that we wanted to import.
03:22Now if we just check to see that, uh,
03:24we don't have any errors, so we still have a few errors,
03:27so we probably wanna fix this.
03:29So first of all, let's see what is our errors?
03:31Let's start from the first one.
03:32Uh, first of all, we're,
03:33we might be getting errors from literally testing in our
03:37test areas themselves.
03:38'cause I'm trying to import all these stuff
03:41and it's not actually there anymore.
03:43So let me go ahead and also make sure
03:45that I'm loading it from the right place.
03:47So I want to import all of that,
03:49but this time around I want to import it from our store
03:51folder and in it our types.
03:54So let's go ahead and save that
03:55and check to see if we still have errors
03:57and we can see that we passed all of our tests.
04:00Okay, so we're in a good spot. So that's step number one.
04:03We extracted out our types
04:04and obviously if you had a lot of different types,
04:07you could also break this down even further.
04:09Creating a folder here and structuring different types
04:12of literally structure out only as you need.
04:14I personally like going through this convention
04:16where you start from store and set your types
04:19and then as you go through, if you need more,
04:21then just break it down further.
04:23Even if it means that you have to rewrite a few things,
04:25which sometimes is just more convenient.
04:28I'm not o into like over planning, over strategizing.
04:31Um, and, and in this kind of philosophy
04:33and this kind of environment, it actually makes it very easy
04:36to make modular updates.
04:38With that said, we're far from done
04:40because we still have a lot of problems
04:42that our application currently has a lot of ties in, tie in
04:46with our store.
04:47There's some places we have no way to avoid it.
04:50For example, uh, we have your also this add counter.
04:53So add counter, where's it importing it?
04:56It's importing it from the wrong place.
04:58So let's also import our add counter from the right place.
05:03If already we see it,
05:08Ugh, I actually deleted it by mistake.
05:13There we go from
05:18store types.
05:22Beautiful. Now
05:24what we're doing here is basically we extracted out,
05:26for the most part, most of our act, uh,
05:30types itself we extracted,
05:31but we're still retyping it over and over.
05:33And that is because we're actually creating our actions
05:36and also the dispatch of those actions within our component,
05:40which say we kind of wanna avoid,
05:42especially if we could avoid it.
05:45So what I wanna do in our next lecture is extract out our
05:49actions and create a separate file for our actions.
05:55In this lecture, we kind
05:56of took our first stab at organizing our
05:58actions in the next lecture.
06:00We're gonna continue down that path
06:02as we continue focusing on actions, talking about bound
06:06and unbound actions.

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!