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

Creating Utility Modules

Organizing your code effectively is essential for scalable and maintainable React applications. In this tutorial, you’ll learn how to extract reusable utility functions and implement idiomatic JavaScript practices. Here's a step-by-step guide based on the lesson:


Why Utility Modules Matter

Utility modules separate generic, reusable code from environment-specific logic. This ensures flexibility, reduces clutter in your components, and improves maintainability. Utility modules can be reused across client and server environments, making them a cornerstone of idiomatic JavaScript.


Steps to Create a Utility Module

  1. Identify Reusable Logic:
    Review your existing codebase and pinpoint functions or logic that are not tied to a specific environment. In this case, we extracted formatting logic from the client.js file.

    const formattedData = {
      labels: ['Label1', 'Label2'],
      datasets: [{ label: 'Dataset1', data: [10, 20] }]
    };
    
  2. Create a Utility Folder and File:
    Inside your src folder, create a utils directory to store reusable functions. Add a file named arrayToChart.js.

    // src/utils/arrayToChart.js
    export function arrayToChart(array) {
      const formattedData = { /* Transform logic */ };
      return formattedData;
    }
    
  3. Refactor the Component:
    Import your new utility function into the component and simplify the component logic.

    import { arrayToChart } from './utils/arrayToChart';
    const data = arrayToChart(rawData);
    
  4. Test the Module:
    Ensure the functionality remains intact. Run the application and validate that the charts display correctly.


Idiomatic JavaScript Tips

  • Modular Design: Write modules that can work across different environments (client, server).
  • Avoid Hardcoding: Keep your utility functions dynamic, passing in data as parameters.
  • Reusable Exports: Use named exports for multiple utilities in a single module.

Why Take This Approach?

Creating utility modules reduces redundancy, improves code readability, and prepares your application for future scalability. These practices empower developers to create clean and efficient React applications with robust data visualization.

💡 Continue building reusable components and mastering Chart.js integration with our full course:
React Data Visualization Course.

Video Transcript

00:00Welcome back to a brand new
00:02and incredibly exciting section, where in it we're gonna
00:06actually create, modify, improve our react component
00:10to make it incredibly reusable,
00:12and it's actually gonna act as the chart js
00:17nonofficial component that could be used by anyone really
00:21that wants to add chart js into their react applications.
00:26So with that said, we're gonna also learn about the,
00:28the importance of organizing your code
00:31and whatever could be idiomatic, which we'll talk about
00:34that in this lecture.
00:36We want to extract out and put it into a utility model
00:40or extract it into something
00:43that is not specifically connected to a client or a server.
00:46With that said, we're also in this chapter gonna learn how
00:50to add random colors,
00:51but in that process we're gonna learn about the overall idea
00:54of searching for things that exist instead of trying
00:57to create them yourself.
00:59We're also gonna take a look at how do we improve
01:01and clean our memory leaks
01:03that currently exist in our react component
01:06and also dive deep into the world
01:08of properties within react.
01:10Last but not least, we're gonna take a nice foray
01:13exploration of the options
01:15that are available within chart js.
01:18With that said, let's jump right into our first lecture
01:20where we'll be creating a utility module.
01:24And the most important thing I want you to get out
01:26of this lecture is the concept of idiomatic JavaScript.
01:30And the idea itself is incredibly simple.
01:33The idea itself is let's create as many JavaScript scripts
01:36as we can that could be reusable
01:39and not include into them anything
01:41that defines them into being a client side
01:43or server side or anything else.
01:45And that way creating components
01:47or more accurately creating modules
01:50that are then reusable both on the client and the server.
01:53And in this case, we're gonna take a look at creating a
01:56utility function and extract it out into a separate file
02:00and use it instead
02:01of having it baked into the client making it unusable
02:05for anyone else besides our client.
02:07So it's really a, a lesson about logic placement.
02:11Let's jump right into it. Let's see it in action.
02:14Welcome back to new section that this time
02:18around our section depends on the last chapter, CSV.
02:23Make sure to copy the files from chapter
02:25three into chapter four.
02:26We're not starting from zero, actually, we're starting from
02:30where we left off in the last section.
02:32With that said, let's jump right into the code
02:34and let's talk about what we're going to be doing.
02:38So let's start off talking about why
02:41our client JS right now is rubbish and not good at all.
02:45Now the reason is very simple. It's a double one.
02:47One is that we have very specific functionality
02:50that in most probability we'll wanna use again on different
02:54objects cluttering up our client js.
02:57That's a first problem. The
02:58Second problem is, is that we have here allot
03:00of functionality that doesn't relate directly
03:03to client only specific things,
03:06because in reality,
03:08if we were building this application on the server
03:10as a server side application, most
03:12of the logic here could work on the server as well.
03:15And this type of thinking process
03:17of improving your code is called idiomatic JavaScript,
03:22where you want to remove all code
03:24that doesn't really d depend on a specific environment
03:28and make it standalone
03:30so you could then use it in different types of environments.
03:34With that said, let's go ahead and do that.
03:35And to do that, I'm gonna go into my source folder
03:38and create a new folder in there called utils.
03:41Now it's called utils because it's not a component.
03:44And it also tells me this is not a react thing,
03:46this is a general utility and it's not the client
03:49or a client folder because this
03:51doesn't relate only to the client.
03:52It could be any source, it could be used by anyone.
03:55We're not gonna put here anything
03:57specifically client related.
03:58With that said, let's go ahead and create a file.
04:00And I'm just gonna call this a for array two chart,
04:05A to chart js.
04:07Now I'm gonna go ahead and save it. And there we go.
04:09So we created ourself our A to chart file
04:12and I'm gonna go ahead
04:13and grab all of the things
04:15that are more generic, generic.
04:18So for that sake, it's really everything here
04:21where we're formatting and preparing the actual data itself.
04:25So if I go ahead and cut it out, paste it right inside here,
04:28but really what I want
04:29to do is I wanna place it in a function so users could call,
04:33uh, and and use it.
04:34So I'll, I'll call it a two C for A two C.
04:39Well, that's a good name I think.
04:40So I'm gonna go ahead and just paste it right in here.
04:43And let's just fix a little bit of the formatting
04:45and then correct all the things that need to be corrected.
04:48First of all, we're gonna get an array, okay?
04:50So we don't have any data to anymore.
04:52And instead that we have your A for our array secondary,
04:56we have to also return the value.
04:58What is the value that we're returning?
05:00It's that formatted data.
05:01So we wanna make sure OTO to send our formatted data.
05:06Um, and let's just double check if there's anything else we
05:09need to change in here.
05:11I do believe that all of this is good.
05:14So the next thing that I'm gonna do is I'm gonna go ahead,
05:16ahead and I'm gonna export this function,
05:19but I'm not gonna export it as a default, contrary to
05:22before, among the reasons I'm not gonna do it
05:24because I might have more functions, more helpers inside
05:28of this utility that helps us convert charts,
05:31arrays into charts.
05:32So now that we have it exported in my client itself,
05:36let me show you how to use it.
05:37First of all, let me change this into raw data
05:40and I'm gonna go ahead and o to import our helper, which is
05:46import, and it's located, first of all, let's give it a name
05:50and let's call it A to C.
05:52That works for us as well,
05:54but okay, I'm gonna show you how
05:55to do it. It's actually not that way.
05:56Exactly. We have to actually do some more steps.
05:59So one is we're inside of our directory of source.
06:04That's good. And then we want to go into utils
06:06and then we want to get a to chart.
06:09Beautiful. Now, this is not beautiful
06:11because we don't have your a default contrary to
06:14what we were doing previously.
06:16So what we wanna do is we wanna put it in a curly bracket
06:20and what that says, I could put here basically listing out
06:23with a comma all the things I want to take.
06:25In this case, I only want to fetch out the A two C function.
06:29Now that I have the A two C function, I could go ahead here
06:32and delete this format of data
06:34and instead of that formatted on the go
06:38by sending in the raw data into the component
06:40and look how much smaller and more compact our structure is.
06:44And only thing that were things that were very specific
06:46to this area are in the client itself.
06:49While all generic logic
06:51and all logic that we could extract out, we've extracted out
06:54and put it inside of our A to two chart.
06:57And lemme just go ahead and make sure that I saved both
06:59of them so we could test it out
07:01and see if it actually is working.
07:05And if everything worked, which it did,
07:06then everything will remain exactly the same as it was
07:09'cause nothing actually changed.
07:12In the next lecture, I owe you that homework assignment
07:15of figuring out how to change colors here
07:17so we could actually have variants in the colors
07:20of our charts.
07:23In this lecture, we really opened your possibilities
07:26and ideas for idiomatic JavaScript,
07:28but more importantly, organizing your code.
07:31The more organized your code is, the better
07:34applications you're gonna be creating in the next lecture.
07:37Now that we have organized code, I wanted
07:39to spend a few minutes talking about adding modules instead
07:42of creating stuff yourself,
07:44leaning on the open community to try to find things.
07:46In this case, we're gonna use random colors.

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!