Learn React - React Components Cheatsheet - Codecademy
Learn React - React Components Cheatsheet - Codecademy
React Components
return statement
React function components must contain a return function MyComponent() {
statement. This should return some React elements
return <h1>Hello from MyComponent!
created with JSX.
</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. Using the component as a factory, an infinite function MyFunctionComponent() {
number of component instances can be created. return <h1>Hello from a function
component!</h1>;
}
function Greeting() {
return (
<>
<h1>Hello, welcome to...</h
<h2>Learn React!</h2>
</>
);
}
In App.js:
Rendering a Component
A React function component can be rendered by //Component to be rendered
creating a root container and rendering the component
function MyComponent() {
into the root container.
return <h1>Hello, World!</h1>
}
href="https://en.wikipedia.org/wiki/Mahat
ma_Gandhi"
>
Mahatma Gandhi
</a>
</cite>
</blockquote>
);
function SeaAnemones () {
return (
<div>
<h1>Colorful Sea Anemones</h1>
<img
src={seaAnemones.src}
alt={seaAnemones.alt}
width={seaAnemones.width}
/>
</div>
);
}
Print Share