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

Creating a Map

Creating a Map with React

In this lesson, you'll learn how to create a React component for displaying an interactive map. Follow these steps to set up and render the component:


Step 1: Setting Up Your Starter Files

  1. Begin with the provided starter files. Ensure you have React and ReactDOM installed as dependencies in your package.json file.
  2. Run npm install in your terminal to install all required modules.

Step 2: Creating the Map Component

  1. Create a new folder named components inside your src directory.
  2. Inside components, create a new file named Map.js.
  3. Write the following code to define your React class:
import React from 'react';

export default class Map extends React.Component {
  render() {
    return (
      <img 
        src="https://upload.wikimedia.org/wikipedia/commons/e/e3/World_map_blank_without_borders.svg" 
        alt="Map" 
        style={{ width: '100%' }} 
      />
    );
  }
}

Step 3: Rendering the Map Component

  1. Open your client.js file (or the entry point for your React app).
  2. Import React, ReactDOM, and the Map component:
import React from 'react';
import ReactDOM from 'react-dom';
import Map from './components/Map';
  1. Render the Map component into the div with the ID react:
ReactDOM.render(<Map />, document.getElementById('react'));

Step 4: Test Your Component

  1. Save all files and start your development server (npm start).
  2. Open your browser and navigate to http://localhost:3000.
  3. Verify that a map image is displayed on the page.

Key Concepts Covered

  • React Component Structure: You learned how to create a reusable React component using ES6 classes.
  • JSX Syntax: Integrated JSX to render an HTML-like structure in React.
  • Rendering Components: Used ReactDOM.render to attach the Map component to the DOM.

In the next lesson, you’ll learn how to make this map interactive by capturing and calculating click events on it.

Explore more in the full course here: Mastering ReactJS: Data Visualization

Video Transcript

00:00Welcome back to a new section
00:02where we're gonna explore web services
00:04and through them create an interactive map
00:08that even has a grid.
00:10So we're gonna go ahead and create a map.
00:13We'll, we'll learn how to work with latitude and longitude
00:16and how to calculate them.
00:18We're gonna explore some more ES six code, connect our
00:23map to a web service,
00:24and then change the component state
00:28learning more about React states
00:29as we integrate dynamic data that will be derived out
00:33of the user's interaction with that map.
00:35So with that said, let's jump right into this section
00:38and start with our first lecture
00:39where we're gonna be creating a map using a fresh
00:43new react component.
00:44In the end of this lecture, we're gonna have
00:47a react component that has a map within.
00:50Let's jump right into it.
00:52This is a friendly reminder that our starter file
00:56that we're gonna be working with is,
00:58although we're in web services chapter, we're starting off
01:01with the same starter files that we've worked
01:03with previously.
01:05So with that said, let's jump right into our code.
01:08And all it really is, is a duplicate of our starter files
01:12and I'm gonna go ahead and into the editor
01:16and we're gonna start off by creating a new component.
01:19So I'm gonna create a new folder
01:20and I'm gonna call it components.
01:26And in our components folder, I'm gonna go ahead
01:29and create a new file and I'm gonna call this file map js
01:34and go ahead and save it.
01:37And all it's left for us to do is to build out our class.
01:41And again, the steps involved with it are, first of all,
01:44we're gonna need to use the React Library.
01:47So to do that I'm gonna go ahead
01:48and import React from React.
01:53And again, just as a reminder, we already installed it.
01:57And make sure also
01:58that when you're running your source files, make sure
02:00that you're gonna click
02:02and type an NPM install
02:04so the node modules will be installed or nothing will work.
02:07And you'll find inside of your package do JSON in our
02:11starter file that we have React
02:13and react dom included as part
02:14of the dependencies that are being used.
02:17So back into our map, we've imported react.
02:20The next step that we want
02:21to do is we wanna define a new class
02:23that we're gonna be exporting.
02:25So I'm going to set it and say export default class
02:30and give it a name followed by extends
02:34react component.
02:37Okay, so that is the structure of our uh, class.
02:41All that's really left for us to do is to go ahead
02:43and render something out.
02:45And I'm gonna go ahead and do that right away
02:47by calling the render function.
02:49And I'm gonna go ahead
02:50and return from that render, uh, JSX an image
02:56and it's just gonna be a self enclosed image.
02:58Now before I wrap this baby up, I'm gonna go ahead
03:02and copy paste here an image that I already selected.
03:05And by the way, it's in your appended resources as well.
03:09It's a common, it's a image that belongs to the commons,
03:12um, legal whatnot.
03:14So you can't use it for commercial uses,
03:16but I'm taking it right from Wikipedia.
03:19What I'm gonna go ahead and do is literally just
03:21load up that image.
03:22So once I've created the render function,
03:24let's go ahead into our client
03:26and configure the client as well.
03:28So we'll actually be able to see it.
03:30So again, I'm gonna have to import, lemme just copy, copy
03:33that word so we don't have to do it over and over.
03:36And I'm gonna import React from React.
03:40Next I want to go ahead and import React DOM from React dom.
03:46Last but not least, I want to also import our map
03:49and I'm gonna call it map
03:50and I'm gonna get it from our current
03:53directory going into the components directory, asking
03:57for the map file or class for this matter.
04:00Um, and all that's left for us to do, I'm gonna go ahead
04:02and delete this, is to go ahead and actually render it out.
04:06And the way that we do that, we call the re React dom
04:09and we call a render method.
04:12And we send as the first parameter, what do we wanna render.
04:15In this case we wanna render the map.
04:17And the next is where do we wanna render it?
04:20And I wanna render it in my document, do document do,
04:25get element by id.
04:29And our ID is called React.
04:32And if I go ahead and say this
04:33and go into my browser, I should be seeing now a map.
04:36And surely I do in this lecture,
04:41we created a React component of a map.
04:44Now that we have the foundations ready in the next lecture,
04:47we're gonna go ahead and try to find the points
04:50that users click on on a map and calculate those points.

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!