Introduction To React - React Components Cheatsheet - Codecademy
Introduction To React - React Components Cheatsheet - Codecademy
React Components
render() Method
React class components must have a render() method. class MyComponent extends React.Component
This method should return some React elements created
{
with JSX.
render() {
return <h1>Hello from the render
method!</h1>;
}
}
Importing React
In order to use React, we must first import the React import React from 'react';
library. When we import the library, it creates an object
that contains properties needed to make React work,
including JSX and creating custom components.
React Components
A React component is a reusable piece of code used to import React from 'react';
define the appearance, behavior, and state of a portion of
a web app’s interface. Components are defined as
functions or as classes. Using the component as a factory, function MyFunctionComponent() {
an infinite number of component instances can be return <h1>Hello from a function
created.
component!</h1>;
}
JSX Capitalization
React requires that the first letter of components be // This is considered a component by
capitalized. JSX will use this capitalization to tell the
React.
difference between an HTML tag and a component
instance. If the first letter of a name is capitalized, then <ThisComponent />
JSX knows it’s a component instance; if not, then it’s an
HTML element.
// This is considered a JSX HTML tag.
<div>
ReactDOM.render()
ReactDOM.render() ‘s first argument is a component import React from 'react';
instance. It will render that component instance.
import ReactDOM from 'react-dom';
In this example, we will render an instance of
MyComponent .
class MyComponent extends React.Component
{
render() {
return <h1>Hello world!</h1>;
}
}
ReactDOM.render(<MyComponent />,
document.getElementById('app'));
href="https://en.wikipedia.org/wiki/Mahatm
a_Gandhi"
>
Mahatma Gandhi
</a>
</cite>
</blockquote>
);
}
Code in render()
A React component can contain JavaScript before any class Integer extends React.Component {
JSX is returned. The JavaScript before the return
render() {
statement informs any logic necessary to render the
component. const value = 3.14;
In the example code, we see JavaScript prior to the const asInteger = Math.round(value);
return statement which rounds the value to an
return <p>{asInteger}</p>;
integer.
}
}
Print Share