From eec31c6723aa0731f2149f896522e8ba50ff8b36 Mon Sep 17 00:00:00 2001 From: artikov Date: Fri, 17 Nov 2023 10:54:47 +0500 Subject: [PATCH] Added React.js Cheatsheet --- source/_posts/react.md | 385 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 385 insertions(+) create mode 100644 source/_posts/react.md diff --git a/source/_posts/react.md b/source/_posts/react.md new file mode 100644 index 0000000..7743c74 --- /dev/null +++ b/source/_posts/react.md @@ -0,0 +1,385 @@ +--- +title: React.js +date: 2023-11-17 10:12:25 +background: bg-[#1289D8] +tags: + - react + - web +categories: + - Programming +intro: | + A React.js cheat sheet with the most important concepts, functions, methods, and more. A complete quick reference for beginners. +plugins: + - copyCode +--- + + + + +Getting Started +------------ + +### JSX + +JSX is a syntax extension for JavaScript that lets you write HTML-like markup inside a JavaScript file. + +```javascript +let element =

Hello, world!

; + +let emptyHeading =

; +``` + + +### JSX Expressions + +```javascript +let name = 'Josh Perez'; +let element =

Hello, {name}

; + +function fullName(firstName, lastName) { + return firstName + ' ' + lastName; +} +let element =

Hello, {fullName('Julie', 'Johnson')}

+``` + + +### JSX Attributes + +```javascript +const element = ; +const element = ; +``` + + + +### JSX Functions + +```javascript +name() { + return "Julie"; +} + +return ( +

+ Hi {name()}! +

+) +``` + + +### JSX Conditional Rendering + +```javascript +import React from "react"; +export default function Weather(props) { + if (props.temperature >= 20) { + return ( +

+ It is {props.temperature}°C (Warm) in {props.city} +

+ ); + } else { + return ( +

+ It is {props.temperature}°C in {props.city} +

+ ); + } +} +``` +Note: A component must always return something. + + +Components +------------ + + +### Functional Component + +```javascript +import React from 'react'; + +export default function UserProfile() { + return ( +
+
Hello
+
World
+
+ ); +} +``` +Note: Every component needs one root element + + +### Embed an internal Component + +```javascript +import React from 'react'; +import UserAvatar from "./UserAvatar"; + +export default function UserProfile() { + return ( +
+ + +
+ ); +} +``` +Note: Assuming UserAvatar is declared in UserAvatar.js + + +### Embed an external Component + +```javascript +import React from 'react'; +import ComponentName from 'component-name'; + +export default function UserProfile() { + return ( +
+ +
+ ); +} +``` +Note: External components are found on npmjs.com and need to be imported first. + + +### Advanced Functional Components + +```javascript +import React from "react"; + +export default function Hello(props) { + function fullName() { + return `${props.firstName} ${props.lastName}`; + } + return ( +

+ {fullName()} +

+ ); +} + + + +``` + + +Properties +------------ + +### Passing Properties to a Component + +```javascript + +``` + +### Assigning the Properties from a Component + +```javascript +import React from "react"; + +export default function Student(props) { + return ( +

+ {props.firstName} {props.lastName} is {props.age}. +

+ ) +} +``` + + +States +------------ + +### React State + +```javascript +import React, { useState } from "react"; + +export default function Hello(props) { + let [name, setName] = useState("Julie"); + function updateName() { + let newName = prompt("What is your name?"); + setName(newName); + } + + return ( +
+

+ {name} +

+ +
+ ); +} +``` + + +Events +------------ + +### Event Listener + +```javascript +import React from "react"; + +export default function Hello() { + function handleClick(event) { + event.preventDefault(); + alert("Hello World"); + } + + return ( + + Say Hi + + ); +} +``` +Note: The most common event listeners are onClick for links/buttons and onSubmit for forms. + + +Loops +------------ + +### Looping through an Array + +```javascript +let elements = ['one', 'two', 'three']; + +return ( +
    + {elements.map(function(value, index) { + return
  • {value}
  • + })} +
+); +``` +Note: Each list item inside a map loop needs a key attribute with a unique value which is generally the index. + + +### Looping through an Array of Objects + +```javascript +let elements = [ + { + name: "one", + value: 1, + }, + { + name: "two", + value: 2, + }, + { + name: "three", + value: 3, + }, +]; +return ( +
    + {elements.map(function (element, index) { + return ( +
  • + The value for {element.name} is {element.value} +
  • + ); + })} +
+); +``` +Note: Each list item inside a map loop needs a key attribute with a unique value which is generally the index. + + +Forms +------------ + +### React Forms + +```javascript +import React, { useState } from "react"; + +export default function LoginForm() { + let [username, setUsername] = useState(""); + let [password, setPassword] = useState(""); + + function handleSubmit(event) { + event.preventDefault(); + alert(`Loging in with ${username} and ${password}`); + } + + function updateUsername(event) { + setUsername(event.target.value); + } + + function updatePassword(event) { + setPassword(event.target.value); + } + + return ( +
+ + + +
+ ); +} +``` + + +CSS +------------ + +### CSS in a React Component + +```javascript +import React from "react"; +import "./Student.css"; + +export default function Student() { + return ( +
+ Julie Johnson +
+ ) +} +``` +Note: You'll then have to crate a css file called Student.css + + +AJAX +------------ + +### AJAX Request with Axios + +```javascript +import React from "react"; +import axios from "axios"; + +export default function Weather(props) { + function handleResponse(response) { + console.log(response); + } + + if (notifications) { + return ( +

+ notifications +

+ ); + } else { + let url = `https://notifications.com`; + axios.get(url).then(handleResponse); + return

Loading notifications..

; + } +} +``` +Note: Make sure to import Axios first to your project. \ No newline at end of file