Vanilla React - Introduction to React (2024)

Table of Contents
Exercise Exercise FAQs

We will start learning React by using React with vanilla JS - which means we will not include any tooling first, and only code with a simple HTML file and a script tag.

html

<!doctype html>

<html lang="en">

<head>

<meta charset="UTF-8" />

<meta name="viewport" content="width=device-width, initial-scale=1.0" />

<meta http-equiv="X-UA-Compatible" content="ie=edge" />

<link rel="stylesheet" href="https://codepen.io/malcolmkee/pen/xmvMNY.css" />

<title>React Movie App</title>

</head>

<body>

<div id="root">not rendered</div>

<script src="https://unpkg.com/react@16.7.0/umd/react.development.js"></script>

<script src="https://unpkg.com/react-dom@16.7.0/umd/react-dom.development.js"></script>

<script>

// Your code goes here

</script>

</body>

</html>

html

<!doctype html>

<html lang="en">

<head>

<meta charset="UTF-8" />

<meta name="viewport" content="width=device-width, initial-scale=1.0" />

<meta http-equiv="X-UA-Compatible" content="ie=edge" />

<link rel="stylesheet" href="https://codepen.io/malcolmkee/pen/xmvMNY.css" />

<title>React Movie App</title>

</head>

<body>

<div id="root">not rendered</div>

<script src="https://unpkg.com/react@16.7.0/umd/react.development.js"></script>

<script src="https://unpkg.com/react-dom@16.7.0/umd/react-dom.development.js"></script>

<script>

// Your code goes here

</script>

</body>

</html>

  • the codepen link is the css that I’ve created for this workshop to make the code-along app looks nicer. We’re not going to discuss about css in this workshop.
  • unpkg is a CDN for npm, which allows us to explore npm package easily. (If you not sure what is npm, relax. We’ll talk about it slightly later.) The two unpkg script tag is used to load the two packages we need to develop in ReactJS:
    • React: this is the library to construct React component, which I will introduce you right after this. It contains no way to render the component, it’s just the declaration of the component.
    • ReactDOM: the rendering layer. Since we’re learning about rendering in browser today, we’ll be using ReactDOM. There are other rendering layer e.g. React Native, React 360 etc. Both React and ReactDOM are required, but the their orders do not matter.
  • the last script tag would be where we write our code. It must be after the two unpkg script tags.

In the <script> tag, add the following code

javascript

const App = function () {

return React.createElement('div', {}, React.createElement('h1', {}, 'React Movie App'));

};

ReactDOM.render(React.createElement(App), document.getElementById('root'));

javascript

const App = function () {

return React.createElement('div', {}, React.createElement('h1', {}, 'React Movie App'));

};

ReactDOM.render(React.createElement(App), document.getElementById('root'));

  • We create a component, call it as App. React is all about creating component, and a component can be as simple as a function.
  • There are two types of component - function component & class component. This is a function component.
  • A component must return markup, which is generated by React.createElement.
  • React.createElement is a function that takes three parameters: component type, properties, and children. For HTML element and web component, the component type would be a string.
  • ReactDOM.render is used to render our App component to the HTML. In this case, a div with id root.
  • Notice we’re using React.createElement with App as a parameter to ReactDOM.render. We need an instance of App to render out. App is a class of components and we need to render one instance of App. That’s what React.createElement does: it makes an instance of a component.
  • A React component is not a markup itself; it is a markup generator.

Exercise

  1. Create a file and name it as index.html.
  2. Add the code above in the file.

Modify your code, so that it becomes

javascript

const Movie = () =>

React.createElement('div', { className: 'movie-container' }, [

React.createElement('h1', {}, 'Aquaman'),

React.createElement('h2', {}, '2018-12-07'),

]);

const App = function () {

return React.createElement('div', {}, [

React.createElement(

'div',

{ className: 'title-bar' },

React.createElement('h1', {}, 'React Movie App')

),

React.createElement(Movie),

React.createElement(Movie),

React.createElement(Movie),

]);

};

ReactDOM.render(React.createElement(App), document.getElementById('root'));

javascript

const Movie = () =>

React.createElement('div', { className: 'movie-container' }, [

React.createElement('h1', {}, 'Aquaman'),

React.createElement('h2', {}, '2018-12-07'),

]);

const App = function () {

return React.createElement('div', {}, [

React.createElement(

'div',

{ className: 'title-bar' },

React.createElement('h1', {}, 'React Movie App')

),

React.createElement(Movie),

React.createElement(Movie),

React.createElement(Movie),

]);

};

ReactDOM.render(React.createElement(App), document.getElementById('root'));

  • to make an element to have multiple children, just pass it an array of elements.
  • Movie is our second component. I use arrow function syntax here, which will have an implicit return if the function body is a single expression
  • We can have multiple Movie instances by calling React.createElement multiple times!
  • className props will be translated to class properties in HTML. This is one of the few instances where React properties and HTML properties are not the same to avoid complication due to reserved JS keywords (class is used to declare class in JS), the others are htmlFor, tabIndex etc.
  • If you’re seeing console warning about keys, ignore it for now.

We have multiple movies but it’s not very useful component yet, since not all movies has name Aquaman. Let’s make Movie component a bit more useful.

javascript

const Movie = (props) =>

React.createElement('div', { className: 'movie-container' }, [

React.createElement('h1', {}, props.name),

React.createElement('h2', {}, props.releaseDate),

]);

const App = function () {

return React.createElement('div', {}, [

React.createElement(

'div',

{ className: 'title-bar' },

React.createElement('h1', {}, 'React Movie App')

),

React.createElement(Movie, {

name: 'Aquaman',

releaseDate: '2018-12-07',

}),

React.createElement(Movie, {

name: 'Bumblebee',

releaseDate: '2018-12-15',

}),

React.createElement(Movie, {

name: 'Fantastic Beasts: The Crimes of Grindelwald',

releaseDate: '2018-11-14',

}),

]);

};

ReactDOM.render(React.createElement(App), document.getElementById('root'));

javascript

const Movie = (props) =>

React.createElement('div', { className: 'movie-container' }, [

React.createElement('h1', {}, props.name),

React.createElement('h2', {}, props.releaseDate),

]);

const App = function () {

return React.createElement('div', {}, [

React.createElement(

'div',

{ className: 'title-bar' },

React.createElement('h1', {}, 'React Movie App')

),

React.createElement(Movie, {

name: 'Aquaman',

releaseDate: '2018-12-07',

}),

React.createElement(Movie, {

name: 'Bumblebee',

releaseDate: '2018-12-15',

}),

React.createElement(Movie, {

name: 'Fantastic Beasts: The Crimes of Grindelwald',

releaseDate: '2018-11-14',

}),

]);

};

ReactDOM.render(React.createElement(App), document.getElementById('root'));

  • Now Movie is a more flexible component that accepts props from its parent.
  • props (stands for properties) are variables that parent component (App) pass to its children (Movie).
  • All React component accepts props as input, and produces the markup (result of React.createElement) as its output.

As mentioned before, there are 2 types of component - function component and class component. Let’s convert App to class component.

javascript

class App extends React.Component {

render() {

return React.createElement('div', {}, [

React.createElement(

'div',

{ className: 'title-bar' },

React.createElement('h1', {}, 'React Movie App')

),

React.createElement(Movie, {

name: 'Aquaman',

releaseDate: '2018-12-07',

}),

React.createElement(Movie, {

name: 'Bumblebee',

releaseDate: '2018-12-15',

}),

React.createElement(Movie, {

name: 'Fantastic Beasts: The Crimes of Grindelwald',

releaseDate: '2018-11-14',

}),

]);

}

}

javascript

class App extends React.Component {

render() {

return React.createElement('div', {}, [

React.createElement(

'div',

{ className: 'title-bar' },

React.createElement('h1', {}, 'React Movie App')

),

React.createElement(Movie, {

name: 'Aquaman',

releaseDate: '2018-12-07',

}),

React.createElement(Movie, {

name: 'Bumblebee',

releaseDate: '2018-12-15',

}),

React.createElement(Movie, {

name: 'Fantastic Beasts: The Crimes of Grindelwald',

releaseDate: '2018-11-14',

}),

]);

}

}

  • App is functionally same as before. However, a class component unlocks more powers, like having state and defining lifecycle methods, which will be explained later.
  • Each class component must have a render method, which must return the result of React.createElement call.
  • For function component, props are available as first parameter of the function. For class component, props are available via this.props.
  • Both function component and render method of class component must be pure function, i.e.:
    • given the same input, it always return the same output (markup)
    • it does not have any side effect - you should not make ajax call / add event listener here.
  • So, when you should make an ajax call in a React app? That will be answered in part 3 of this course. But before that, let’s dive into the tooling around React.

Exercise

  1. Add Movie component in your code and use it as explained above.
  2. Modify your App component to a class component
  3. (Bonus) Add a new props genres in your Movie component and render it in a span tag.
Vanilla React - Introduction to React (2024)

FAQs

Is vanilla JS harder than React? ›

React has a steeper learning curve than vanilla javascript as it embraces component-based architecture and JSX syntax. Furthermore, React documentation and community support make it easier to learn and build complex applications once the basics are grasped.

Is React very difficult? ›

Starting with React can be tough, especially if you're new to web development. Concepts like JSX, components, and state management might seem like a maze. But don't worry! With some practice and patience, it gets easier, and React becomes more familiar.

Why is React Native so complicated? ›

Why is React Native so complicated? React Native can be somewhat complicated due to the need to understand both JavaScript and native platform code.

Do I need to learn vanilla JavaScript before React? ›

While we don't believe you need to be a JavaScript Master (™) in order to begin learning about React, it will benefit you to have the core JavaScript concepts and syntax under your belt before embarking on your React journey. Anything you do with React, you can do with “vanilla” JavaScript.

Should I use vanilla React or Nextjs? ›

js offers easy-to-code functionality compared to React. When it comes to writing code, Next. js requires less code, whereas React framework requires a long line of coding. React can be the right choice if you wish to build a large complex web application with complex routing and heavily data-driven components.

What is the disadvantage of Vanilla JS? ›

Disadvantages of using JavaScript

The simple file operations like reading and writting are prohibitted in JavaScript due to security issues. VanillaJS also lacks for multipurpose and multithreading capabilities. It does not support the network based applications.

Is React harder than Python? ›

ReactJS has a more demanding learning curve and isn't challenging to master, while Python is the simplest language, making learning much simpler.

How many hours do I need to learn React? ›

Most people take one to six months to learn React. How fast you learn React will depend on your prior experience with programming—specifically with JavaScript. Any obligations, such as family or work, can also impact you.

Is React easy for beginners? ›

You'll have an easy time learning React if you're already well-versed in JavaScript, but it can be challenging if you don't have little or no experience in JavaScript beforehand. That's why it's recommended to learn JavaScript first.

Is React Native dead in 2024? ›

React Native continues to evolve and make strides in 2024. As competition increases from other cross-platform tools like Flutter, Jetpack Compose, and . NET MAUI, there is pressure to improve the React Native developer and user experience.

Is React Native losing popularity? ›

According to the Statista survey, Flutter has replaced React Native as the most popular cross-platform mobile framework used by developers. While in 2020, React Native had 42% of users, it has been reduced to 38% in 2021.

Why we stopped using React Native? ›

To add to the efficiency problems, the framework's native modules take a lot of time to develop, mainly because of long run-debug-fix cycles. This means that if you have a library built with Swift or Kotlin, it can take more time to get it to work in a React Native app.

Is it OK to use vanilla JS? ›

Vanilla JavaScript is better for:

You have complete control over the codebase without any framework limitations. Learning and foundational purposes: Working with vanilla JavaScript is highly recommended if you're new to web development or want to strengthen your core JavaScript skills.

How long does it take to learn vanilla JavaScript? ›

You can learn the basics of JavaScript in three to six months if you're dedicated and practice every day. However, like most programming languages, JavaScript can take many years to master. It's like learning a new language. You might be able to read some Spanish and learn basic phrases in six to nine months.

Is Vanilla JS hard? ›

However, Vanilla JavaScript can be used to write React code. Is Vanilla JavaScript more difficult to learn than JavaScript libraries? Vanilla JavaScript is easier to learn than JavaScript libraries, but it requires more code to be written from scratch.

Which is harder, JS or React? ›

Learning Curve: React presents an alternative method of thinking about creating visual user interfaces, which might be difficult for designers who are new to the toolkit. The initial learning curve can appear higher for more straightforward applications than for basic JavaScript.

Is Vanilla JS faster than framework? ›

Vanilla JavaScript has a lighter codebase and requires fewer resources, resulting in faster performance than JavaScript Frameworks. However, complex features in a JavaScript Framework can affect performance.

What is easier than React? ›

Learning Curve

Vue is easier to learn compared to React. This is because Vue separates concerns in a way that web developers are already used to, decoupling HTML, CSS, and JavaScript.

Top Articles
What to GPU Mine? GPU mining in 2022. A list of GPU mineable coins
Ravencoin RVN Network Hashrate Chart - 2Miners
Mickey Moniak Walk Up Song
Funny Roblox Id Codes 2023
Dte Outage Map Woodhaven
Brady Hughes Justified
Best Big Jumpshot 2K23
The Daily News Leader from Staunton, Virginia
80 For Brady Showtimes Near Marcus Point Cinema
Mr Tire Prince Frederick Md 20678
Athletic Squad With Poles Crossword
123Moviescloud
What Is A Good Estimate For 380 Of 60
Oscar Nominated Brings Winning Profile to the Kentucky Turf Cup
Craigslist Alabama Montgomery
Bernie Platt, former Cherry Hill mayor and funeral home magnate, has died at 90
Chris Hipkins Fue Juramentado Como El Nuevo Primer Ministro De...
A rough Sunday for some of the NFL's best teams in 2023 led to the three biggest upsets: Analysis - NFL
Craigslist Blackshear Ga
Pac Man Deviantart
979-200-6466
Ostateillustrated Com Message Boards
Cyndaquil Gen 4 Learnset
TBM 910 | Turboprop Aircraft - DAHER TBM 960, TBM 910
Azpeople View Paycheck/W2
Barber Gym Quantico Hours
Where to eat: the 50 best restaurants in Freiburg im Breisgau
Aes Salt Lake City Showdown
8005607994
Walgreens Bunce Rd
6 Most Trusted Pheromone perfumes of 2024 for Winning Over Women
Rogue Lineage Uber Titles
Avatar: The Way Of Water Showtimes Near Maya Pittsburg Cinemas
Afni Collections
Busch Gardens Wait Times
Dreamcargiveaways
Metra Union Pacific West Schedule
Desirulez.tv
Breckie Hill Fapello
Reading Craigslist Pa
Daily Jail Count - Harrison County Sheriff's Office - Mississippi
Instafeet Login
Vons Credit Union Routing Number
Stranahan Theater Dress Code
Unitedhealthcare Community Plan Eye Doctors
Yale College Confidential 2027
Avance Primary Care Morrisville
Professors Helpers Abbreviation
Jackerman Mothers Warmth Part 3
Where and How to Watch Sound of Freedom | Angel Studios
Honeybee: Classification, Morphology, Types, and Lifecycle
Latest Posts
Article information

Author: Dong Thiel

Last Updated:

Views: 6539

Rating: 4.9 / 5 (59 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Dong Thiel

Birthday: 2001-07-14

Address: 2865 Kasha Unions, West Corrinne, AK 05708-1071

Phone: +3512198379449

Job: Design Planner

Hobby: Graffiti, Foreign language learning, Gambling, Metalworking, Rowing, Sculling, Sewing

Introduction: My name is Dong Thiel, I am a brainy, happy, tasty, lively, splendid, talented, cooperative person who loves writing and wants to share my knowledge and understanding with you.