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

Overview of JSON Basics

Tutorial: Setting Things Up for React Development

In this lesson, we focus on setting up the development environment for our ReactJS data visualization course. Here's a step-by-step guide:


Prerequisites

  1. Node.js and npm:
    • Open your terminal and type node -v to check the Node.js version.
    • Similarly, type npm -v to verify the npm installation.
    • Install the latest versions from Node.js official website if needed.

Development Environment Setup

  1. Tools:

    • Install Babel for ES6 and JSX transpilation.
    • Use Webpack and Webpack Dev Server to bundle and serve files with live refresh.
  2. Configuration Files:

    • package.json: Lists project dependencies such as react, react-dom, and babel-polyfill.
    • webpack.config.js: Configures the Webpack process to bundle JavaScript into an index.js file for use in the browser.
  3. Source Files:

    • client.js: Entry point for React code, starting as a basic JavaScript file.
    • Starter Files: Located in the "00-starter-files" folder, these provide a foundation for your work.

Key Features of the Setup

  1. Babel Polyfill: Ensures compatibility with older browsers by adding support for ES5.
  2. Hot Refresh: Webpack Dev Server automatically compiles and refreshes the browser when changes are made.
  3. Local Development Server: Runs on localhost:3000 with live updates for quick iteration.

Next Steps

  1. Navigate to the starter files and open the 00-starter-files folder.

  2. Install project dependencies:

    npm install
    

    This command installs all listed dependencies and dev dependencies from package.json.

  3. Verify the setup by running the Webpack development server:

    npm start
    

When everything is set, you're ready to explore the basics of JSON in the next lesson.


2024

In 2024, some aspects of the setup in the transcript are slightly dated. Modern best practices and tools for ReactJS development have evolved. Here's a revised, optimal approach:


Updated Setup for React Development in 2024

Prerequisites

  1. Node.js:

    • Use the LTS version for stability.
    • Install via Node.js or a version manager like nvm (Node Version Manager). This makes it easier to switch Node.js versions for different projects.
    nvm install --lts
    nvm use --lts
    
  2. Package Manager:

    • Use pnpm or yarn instead of npm for faster and more reliable dependency management.
    • Install pnpm:
      npm install -g pnpm
      

Development Tools

  1. Babel:

    • Babel is still widely used, but Vite has become the preferred choice for React projects in 2024 due to its faster build times and better development experience.
  2. Vite:

    • Use Vite instead of Webpack for modern React development. Vite supports ES6+ out of the box and provides hot module replacement (HMR) for faster refreshes.

    To set up a Vite project with React:

    pnpm create vite@latest my-react-app --template react
    cd my-react-app
    pnpm install
    
  3. TypeScript:

    • TypeScript is increasingly standard in React development for better type safety and scalability. If not using TypeScript, consider adopting it for new projects:
      pnpm create vite@latest my-react-app --template react-ts
      

Modern Folder Structure

  1. Public Folder:

    • Contains static files like index.html. Modern setups still use an empty HTML file with a <div id="root"></div> for React.
  2. Source Folder (src):

    • Holds components, styles, and utilities. The entry file is often main.jsx (or main.tsx for TypeScript).

    Example:

    src/
    ├── components/
    ├── App.jsx
    ├── main.jsx
    ├── styles/
    ├── utils/
    

Key Features of Vite Setup

  1. Built-in ES Module Support:

    • No need for Babel Polyfill—modern browsers fully support ES6+.
  2. Automatic HMR:

    • Vite's hot module replacement is faster and smoother than Webpack's.
  3. Optimized Builds:

    • Vite's build process uses Rollup under the hood, producing smaller and faster production builds.

Commands for 2024 Setup

  1. Install Dependencies:

    pnpm install
    
  2. Run Dev Server:

    pnpm dev
    
  3. Build for Production:

    pnpm build
    

Ecosystem Considerations

  • Use React Router v6+ for client-side routing.
  • Consider React Query or TanStack Query for state management when working with server data.
  • For CSS, prefer modern tools like Tailwind CSS or CSS Modules over traditional CSS files.

Comparison with 2016 Approach

Feature2016 Setup2024 Setup
BundlerWebpackVite
Package Managernpmpnpm (or yarn)
Dev ServerWebpack Dev ServerVite Dev Server
JavaScript VersionES6 (via Babel)ES6+ (native)
PolyfillsBabel PolyfillRarely needed
DependenciesReact, ReactDOM, BabelReact, Vite

Conclusion

While the foundational ideas from the original approach are still relevant, transitioning to tools like Vite provides significant speed and efficiency improvements. If this course were updated today, using Vite and pnpm with TypeScript would reflect modern ReactJS practices.

Video Transcript

00:00If you're not up to speed with JSON, fear not
00:03because this lecture will speed you through it,
00:05because it's really very similar to JavaScript
00:08because it's derived out of JavaScript,
00:10but it's an independent setup.
00:13Now, basically,
00:14the way you would create objects is the way you would
00:17create, oh, so JSON only with a few slight exceptions.
00:21And we're gonna explore them and look at how they work.
00:24Now, some implementations
00:26of JSON are more strict than others.
00:29So for example, MongoDB,
00:31which we'll explore in the last section, which uses JSON
00:33as well, is a lot more flexible than a lot of other parsers
00:38that are run on a browser or run on the server itself.
00:42So we're gonna show you the strict
00:43configurations of how to set it up.
00:45And then if you ever have an error when you're running JSONs
00:48in a high probability is
00:49because you didn't adhere directly to the configurations
00:52that we're gonna talk about in this lecture.
00:54So let's jump right into it.
00:56Again, assuming that you have NPM
00:59and no JS installed, our next step
01:02before we start talking about JSON is
01:04to get our server running.
01:06Now, because I already pre-configured our server settings in
01:10my package, JSON, I've made sure that webpac
01:13and Webpac Dev server are installed.
01:16And as well, I've added a script here,
01:19which is called start.
01:20That will enable me very easily to start up our server
01:24when we start up our server.
01:25And let me just go ahead and do that.
01:27If I go into my terminal, I'm just gonna type NPM
01:31start by doing that.
01:33It's gonna start up my server
01:34and my server will be running on the local host.
01:38Now all that's left for me to do to see that it's running,
01:41is to go onto my browser and click on Refresh.
01:44And we should see Master and React data visualization.
01:47I want you to note and deliberately,
01:49I didn't set up our JavaScript file to run.
01:52You see that right now that starter file is working,
01:55is not running, and that is
01:57because I need to go into my index dot html
02:00and also add that file.
02:02Now that file is not gonna be called Client js, and that is
02:06because in my web pack, I'm using that entry point of
02:10that client and converting it into a bundled version
02:13of all the JavaScript files that will be included in this
02:17or used by this file into the file index js.
02:22So all I really need to do to gain access
02:24to it is literally just go
02:26and type index js in the browser to see that file,
02:30which actually renders out
02:31with all the extra things it needs to be able to run
02:35ES six on any browser.
02:38Uh, E ES five, sorry, on any browser,
02:40'cause it's converting from E ES six to ES five
02:43and also adding shims to make sure that things are working,
02:46adding in all the things that are used.
02:49Now for us to actually take advantage of it, we need
02:51to add it into our HTML.
02:53So go right to the bottom of your file
02:55and add a new script tag.
02:56And we don't need type because it's the default type,
02:59which is JavaScript,
03:00but make sure that we're referencing to index js
03:04and I'll make sure that it's OTO in the starter file.
03:07So in the future, you won't need to do it,
03:09but make sure that you are aware that this is needed
03:12for it actually to work.
03:14With that said, if you go back into your browser, it should
03:18usually refresh, but
03:20because we didn't add any JavaScript into the file,
03:22then it's not refreshing automatically.
03:25But if I click your refresh one more time, you'll see
03:28that our starter file is working.
03:30Moreover than that. If I go back into that starter file
03:33and change it from starter file is working to
03:37and save that change, you'll see
03:39that also our browser will refresh its content
03:42and update that as well.
03:45With that said, before we wrap up this conversation,
03:49I wanna also talk about JSON in general,
03:52although your JavaScript developers,
03:54so you should be familiar with the basic constructs
03:57of J-S-O-N-J.
03:59SON in general is an object,
04:02and it has an object structure with a few different rules
04:04where the name can't be a name directly,
04:07although it does work in JavaScript for the most.
04:10You wanna bound it in a string and then you set the value.
04:14Now the value could be,
04:15or a string, It could, the value could be, or a number.
04:22The value could also be a Boolean value.
04:29A value could be oto, uh, an array,
04:39and so on and so forth.
04:44Last but not least, you could also add objects inside of it.
04:47So I could have another object.
04:49Object, for example, um, let's say eyes
04:54and Eyes could have your an object
04:57and that object could have your left
05:01and my left eye is green.
05:05And let's say that my right eye
05:10is brown.
05:15In a nutshell, this is JSON.
05:18Now the next question is how do you get it
05:20to be in a separate file, which is something
05:22that we're gonna cover in the next lecture,
05:25but as this is quite a simple construct for someone
05:28that is working in JavaScript,
05:30I'm gonna go ahead and delete it.
05:31The most important thing again is just making sure
05:34that you're using construct the legal object.
05:37Notations. We're not using any modern variations
05:41or any fancy JavaScript things.
05:42It's really just the basics of the basics.
05:45A string from one side, a string on the other,
05:48although on the other side it can be,
05:49or a string, a number, a Boolean value, an array,
05:54or even an object.
05:55And that is the construct of
05:59A-J-S-O-N file.
06:01In this lecture, we explored JSON
06:04and saw how it works, including its specification.
06:07It's time for us to go into building something,
06:09and we're gonna take a look at how do we import JS files
06:12with webpac.

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!