Skip to main content

Posts

React Components

React applications are made of   components. What’s a component? A component is a small, reusable chunk of code that is responsible for one job. That job is often to render some HTML and re-render it when some data changes. Take a look at the code below. This code will create and render a new React component: import React from 'react'; import ReactDOM from 'react-dom/client'; function MyComponent() { return <h1>Hello world</h1>; } ReactDOM.createRoot( document.getElementById('app') ).render(<MyComponent />); Import React This creates an object named React, which contains methods necessary to use the React library. React is imported from the 'react' package, which should be installed in your project as a dependency. With the object, we can start utilizing features of the react library! By importing the library, we can use important features such as Hooks, which we’ll explore in detail later. import React from 'react';

React Advanced JSX

 class vs className This lesson will cover more advanced JSX. You’ll learn some powerful tricks and some common errors to avoid. Grammar in JSX is mostly the same as in HTML, but there are subtle differences to watch out for. The most frequent of these involves the word class. In HTML, it’s common to use class as an attribute name: <h1 class = "big" > Title </h1> In JSX, you can’t use the word  class ! You have to use  className  instead: <h1 className = "big" > Title </h1> This is because JSX gets translated into JavaScript, and  class  is a reserved word in JavaScript. When JSX is  rendered , JSX  className  attributes are automatically rendered as  class  attributes. Self-Closing Tags Another common JSX error involves  self-closing tags . What’s a self-closing tag? Most HTML elements use two tags: an  opening tag  ( <div> ), and a  closing tag  ( </div> ). However, some HTML elements such as  <img>  and  <input>  u