React Components Cheatsheet
React Components Cheatsheet
example:
Rendering a Component:
To render a React component, you establish a root container, typically
an HTML element, and utilize the ReactDOM library to render the
component within that container.
Example:
// Component to be rendered
function MyComponent() {
ReactDOM.createRoot(
document.getElementById('app')
).render(<MyComponent />);
Example:
function MyComponent() {
Components:
For instance:
// Function Component
function MyFunctionComponent() {
// Class Component
render() {
For illustration:
Here's an example:
// Greeting.js
function Greeting() {
return (
<>
<h2>Learn React!</h2>
</>
);
// App.js
For instance:
return (
<blockquote>
<cite>
<a
target="_blank"
href="https://en.wikipedia.org/wiki/Mahatma_Gandhi"
>
Mahatma Gandhi
</a>
</cite>
</blockquote>
);
Example:
function Integer() {
return <p>{asInteger}</p>;
In React, you can assign JSX attribute values using data stored in
regular JavaScript objects. This approach enhances the readability of
your JSX by employing variables to define attribute values.
Here's an example:
const seaAnemones = {
src:
'https://commons.wikimedia.org/wiki/Category:Images#/media/
File:Anemones_0429.jpg',
width: '300px',
};
function SeaAnemones() {
return (
<div>
<img
src={seaAnemones.src}
alt={seaAnemones.alt}
width={seaAnemones.width}
/>
</div>
);