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

Building ES6 React Classes

Tutorial: Importing JSON Files with Webpack

In this lesson, we learned how to configure Webpack to import JSON files into a ReactJS application. Below is the step-by-step guide:


Why Use Webpack for JSON Imports?

  • ES6 modules don’t natively support importing JSON files.
  • Webpack solves this by using loaders to preprocess JSON files for integration into the application.

Steps to Import JSON Files

  1. Create a JSON File:

    • In your project’s src folder, create a JSON file:
      Example: data.json
      {
          "name": "Ben",
          "age": 30
      }
      
  2. Install the JSON Loader:

    • Open your terminal and install the JSON loader as a development dependency:
      npm install --save-dev json-loader
      
  3. Update Webpack Configuration:

    • In the webpack.config.js file, add the JSON loader to the module.rules section:
      module.exports = {
          module: {
              rules: [
                  {
                      test: /\.json$/,
                      loader: 'json-loader'
                  }
              ]
          }
      };
      
  4. Import JSON Data in Your Code:

    • Use the ES6 import syntax to include the JSON file:
      import data from './data.json';
      console.log(data.name); // Output: Ben
      
  5. Start the Webpack Dev Server:

    • Run the development server:
      npm start
      
  6. Verify in the Browser:

    • Open your browser and check the console to confirm the JSON data is loaded.

Additional Notes

  • The JSON loader is a workaround as ES6 does not natively support JSON imports.
  • Modern JavaScript tools like Vite or Next.js now handle JSON imports natively, making Webpack-specific loaders less common in 2024.

Next Steps

Now that JSON data is successfully integrated into the application, the next step is to use this data in ES6-based React components. Stay tuned for the next lesson!

Video Transcript

00:01Our data is ready and imported,
00:02and it's time for us to move into our main topic, which is
00:06React, and we're gonna build out an ES six React component
00:11or an E es six class, which is a component in the process.
00:14We're gonna look at how to crew work with ES six in general.
00:17We're gonna export that class
00:20and we're also gonna import it into our client looking
00:23through the whole process of the creation of a class
00:26through exporting and importing it.
00:29So let's jump right into it.
00:32All righty, let's get some react into the picture.
00:34Now, one thing that I want to also notice
00:36that we're not gonna do in this title, a deep dive into
00:40what React is.
00:41There's a little bit of an assumption
00:43that you're familiar with it.
00:45If you wanna really take a deep dive into the world
00:48of React, please look into my title
00:51Mastering React components,
00:52which is really focus purely on the fundamentals of React
00:57and creating with IT wonderful components,
01:00although we'll do the same in this title,
01:02our focus is not gonna be on that as the process.
01:05Our focus will be on the data itself
01:08and how to visualize it.
01:09With that said, take a look inside of your package that JSO,
01:13just as a reminder that we already installed React
01:17and React dumb as part of our core dependencies.
01:21Now, if you're gonna be installing things on your own
01:24and haven't used our base file, make sure to get out
01:27of your web server that is running and then go ahead
01:30and call NPM install and then dash, dash save,
01:34and then call for React and react Dumb.
01:38In our case, we're not gonna do that
01:40because we already have it installed
01:42so we don't need to install it.
01:43So I'm gonna go ahead and start up our server again
01:47and as we have it already installed,
01:49I wanna show you the basic constructs of how you work
01:52with React in ES six.
01:55So let's go ahead and do it.
01:56And I'm gonna start off from going into my source folder
01:59because what I want to do is I want to create a new
02:02directory and I'm gonna call that directory
02:05or folder components.
02:07Why am I calling calling it components?
02:09The reason's very simple
02:11because React things are components.
02:14Everything you create in React is a component
02:17and I wanna put my components in a separated
02:20folder for components.
02:21Now, by the way, I'm not putting it in a folder called
02:24Components Client
02:25because React components could be used on the server,
02:29on React Native and in many other places.
02:32And generally speaking, I want to create my React components
02:35to be reusable for the client in other environments.
02:39That is part of the logic why there is a library called
02:42React and a separate library called React Dumb.
02:46Now the reason is is
02:47because React itself is in charge of that API to create your
02:52constructs, your components.
02:53While React Dumb is in charge
02:55of getting it up into the dumb,
02:58This separation has a lot of logic, which you cover a lot
03:01of it in our Mastering React components,
03:04but I just wanted to make sure that you see
03:06and understand that separation
03:08because we'll be working when we're working inside
03:11of the component folder only with the React Library.
03:14While when we're trying to render things such inside
03:17of our client folder, we're also gonna leverage the React
03:20dom to tell React where should it render the things
03:23that it's creating.
03:25So with that said, let's go into our components folder
03:27and create our first component.
03:30And I'm gonna go ahead and I'm just gonna call the file app
03:32dot js and lemme just go ahead and save that
03:36and now I have a new file.
03:37Beautiful. So lemme just delete that
03:39and let's start the process of creating a React component.
03:44Now, the first step that we're gonna do
03:46and the most important step is that we need to go ahead
03:49and import the React library.
03:51We do that using the ES six import directive
03:55and I'm saying import from.
03:57And this time around, instead of putting a dot slash
03:59for the current directory,
04:01I'm just gonna type directly React.
04:04That's gonna tell our no JS
04:06and web back for that matter,
04:08that we should find this library as one of the modules
04:11of No js, which surely react is one of those modules.
04:16We've even installed it
04:17and we could see it inside, not here inside of our package,
04:20not JSO, we have the React package.
04:24So I'm going ahead and loading up that
04:26or importing that library
04:27and placing it inside of the React access point for
04:32or object.
04:34The next step is I'm gonna go ahead
04:36and create a class in e ES six.
04:39Now the way I do that is I type the word class,
04:41which is now a reserved word.
04:44Then I type in a name to that class
04:46and then I open a curly bracket and close a curly bracket.
04:51In my situation, I'm not only creating a class, I want
04:54to create a class that extends the React
04:57component based class.
04:59To do that, I'm gonna type in extends
05:02and then type react, do component,
05:07component component, beautiful react component.
05:12What that will do is it's gonna make sure
05:14that my app extends the features that are
05:16inside React component.
05:19Last but not least, to create a ba, a very thin basic
05:24react component, I need to go ahead
05:26and create a rendering function in E ES six.
05:31All I need to do inside
05:32of a class is type the name of the function.
05:35In this case we said it's called render.
05:37Then open and close a round bracket, open a curly bracket,
05:41close a curly bracket.
05:43I do not need to use the word function
05:45and that's all I need to do to create our method
05:50of the react component in our app.
05:52That is called rendered.
05:54The next step that I'm gonna do is I'm gonna
05:57Go ahead and put you a return value
05:58because that's what the render method expects
06:01and return here some construct.
06:04Now React, react expects the render method
06:08to return something that is or HDML or another component.
06:13Now a really cool feature that is the birth child
06:18of Facebook slash react is JSX.
06:21Now because we originally,
06:23when we were setting up our project in our package,
06:26made sure that we also included the React preset.
06:30It enables us to use JS X inside of our application
06:35and inside of our app I wanna show you the basic, basic
06:38construct of JSX
06:40and that is literally I could just put
06:42inside native HTML hello world.
06:46Now one of the basic core rules of JSX is where
06:50whenever you're working with JSX,
06:53and by the way, this is automatically converted into
06:55JavaScript you with the help of Webpac and Babel.
07:00But in our situation, what is actually happening,
07:02what what's important for us to notice is
07:04that we have an opening tag, a closing tag.
07:07You cannot have a H one and then a H two.
07:10It has to be bounded by one item.
07:13With that said, now that I have your A self enclosed root
07:16or XML valid structure, let's go
07:19and integrate this app
07:21that we've just created into our client.
07:23So the next step for us to do is we're gonna go ahead
07:26and OTO import into this area, react itself.
07:30The reason is is because we're gonna use JSX as well
07:34and I'm gonna go ahead and get that from React
07:37and I'm gonna import Oso React Dom from
07:42React dom and lemme just make sure that that's all uppercase
07:45because the DOM is uppercase really.
07:49Okay, now that we've loaded the two library packages
07:53that we need to actually render things out, let's go ahead
07:56and also import our component, which we decided
07:59to call app from.
08:02And this really moves us to that conversation
08:04that we were talking about.
08:05How do you actually load things up?
08:08So first of all, components app JS
08:11or not js it automatically will know,
08:14but in our app itself right now, if I try
08:16to run anything at this point, it will not actually work
08:20because in my application itself I need to tell JavaScript
08:24what is going to be exported.
08:26And in this case, because we only have one class,
08:29we really want it to be exported.
08:31So I'm just gonna type your export default,
08:35which basically tells the browser
08:38or tells in our case Webpac,
08:39that this class is gonna be the default class
08:42that is gonna be imported.
08:44By the way, making the name really not that relevant
08:46because inside of our client,
08:49whenever I'm importing it, it's gonna grab that class
08:51and it's gonna assign it
08:52to whatever name I set here in my case.
08:55App works really well.
08:57The next step that I'm gonna do is I'm gonna go ahead
08:59and delete this document change
09:01because instead of that I want to go ahead
09:03and use that React dom object that we've loaded up
09:07and I'm gonna ask it to render
09:09and what do I wanna render here?
09:10I'm gonna put in another JSX
09:12and what literally what I'm gonna do is I'm gonna call my
09:15app itself last but not least as the second parameter.
09:20This is a like HTML only.
09:22Instead of that we're using here by the way,
09:24a very important differentiation is when I'm typing regular
09:28HTML, it's all lowercase.
09:31In any situation where I'm trying to load in a class I have
09:35to put here in uppercase
09:37that will tell the rendering engine JSX to search
09:40for a class and render that item out
09:43and actually send an object out here and not this XML,
09:47but this XML structure makes it really very intuitive,
09:50especially as you're building structured data.
09:53The next step, I'm gonna go into my document, do get element
09:58by id and I'm gonna tell React that I wanted
10:01to load up into the React ID area
10:04'cause I have your into inside
10:06of my body a React Diviv element with an idea of React.
10:10So again, let's walk through the steps.
10:13I made sure that I loaded React. Why did I load?
10:16React is very simple.
10:18When this app is being rendered, it's converted into React
10:22load component or somewhat,
10:24it actually turns it into React code
10:26and it needs access to that react rendering tool to fetch
10:30that app component and build it out.
10:33That is happening behind the scenes,
10:35which we don't really need to worry about.
10:36As long as we load it up,
10:38react our React dom is gonna help us communicate
10:41with the browser and tell it, Hey, we want our application
10:44to be loaded up and put into this specific diviv.
10:48So in this case we're getting that diviv
10:50and telling it, Hey, this is the root directory
10:52for our rendering.
10:55Last but not least, we have here our data,
10:56which we'll take a look at in the next lecture,
10:58how we connect that data into our new app.
11:01But let's go ahead and save our application and check
11:04and see if we actually got our app
11:06to render out and show something.
11:08So first of all, we could see
11:10that React create element is not loading up correctly.
11:14And the reason is is
11:15because in my app itself, once I've changed
11:17and set that export default, I didn't save it.
11:20So lemme go ahead and save that
11:22and let's go back into my application and click refresh
11:24or well, it's gonna happen automatically
11:26'cause we have Webpack and you can see
11:28that we have your hello world.
11:31And with that, that is the basic, basic construct that
11:34with it we create react components
11:37and ES six with exporting and importing.
11:42In this lecture we created an ES six class, we exported it
11:46and we also imported it.
11:48It's time for us to move into integrating the JSON into our
11:53component and that's exactly
11:54what we'll do in the next lecture.

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!