Merge remote-tracking branch 'origin/main'

This commit is contained in:
Fechin committed 2024-03-20 13:50:55 +08:00
commit ffe2884026
5 files changed
+1229 -3

No files matched your search

+1
View File
@@ -39,6 +39,7 @@ Reference provides cheat sheets for the following:
This cheat sheet provides a reference for common commands and features for Laravel 8.
- [Dart](https://cheatsheets.zip/dart.html): A Dart cheat sheet with the most important concepts, functions, methods, and more. A complete quick reference for beginners.
- [JSON](https://cheatsheets.zip/json.html): This is a quick reference cheat sheet for understanding and writing JSON format configuration files.
- [Hook](https://cheatsheets.zip/hook.html): The [Hook](https://github.com/hook-lang/hook/) cheat sheet is a one-page reference sheet for the Hook programming language.
- [HTML](https://cheatsheets.zip/html.html): This HTML quick reference cheat sheet lists the common HTML and HTML5 tags in readable layout.
- [GraphQL](https://cheatsheets.zip/graphql.html): This quick reference cheat sheet provides a brief overview of GraphQL.
- [C++](https://cheatsheets.zip/cpp.html): C++ quick reference cheat sheet that provides basic syntax and methods.
+2 -2
View File
@@ -492,7 +492,7 @@ Futures
------------
### Async Await
```dart
// functionswhich are asynchronous: they return after setting up a possibly time-consuming operation
// functions which are asynchronous: they return after setting up a possibly time-consuming operation
// The async and await keywords support asynchronous programming
Future<String> login() {
@@ -505,7 +505,7 @@ Future<String> login() {
// Asynchronous
main() async {
print('Authenticating please wait...');
print(await userName());
print(await login());
}
```
File diff suppressed because it is too large. Load diff
+186 -1
View File
@@ -382,4 +382,189 @@ export default function Weather(props) {
}
}
```
Note: Make sure to import Axios first to your project.
Note: Make sure to import Axios first to your project.
HOOKS
------------
### useState Hook
```javascript
import React, { useState } from "react";
function Counter() {
const [count, setCount] = useState(0);
return (
<div>
<p>Count: {count}</p>
<button onClick={() => setCount(count + 1)}>Increment</button>
</div>
);
}
export default Counter;
```
Note: The useState Hook is a built-in React Hook that allows functional components to manage local state. It provides a way to declare state variables and update them within a functional component.
Example code illustrating how to use it
### Multiple State Variable Declaration
```javascript
import React, { useState } from "react";
function Counter() {
const [count, setCount] = useState(0);
const [name, setName] = useState("");
const [isCompleted, setIsCompleted] = useState(false);
const handleIncrement = () => {
setCount(count + 1);
};
const handleNameChange = (event) => {
setName(event.target.value);
};
const toggleCompletion = () => {
setIsCompleted(!isCompleted);
};
return (
<div>
<p>Count: {count}</p>
<button onClick={handleIncrement}>Increment</button>
<input type="text" value={name} onChange={handleNameChange} placeholder="Enter your name" />
<label>
<input type="checkbox" checked={isCompleted} onChange={toggleCompletion} />
Completed
</label>
</div>
);
}
export default Counter;
```
Note: You can declare multiple state variables using the useState Hook by calling it multiple times in a functional component. Each call to useState manages a separate piece of state.
### Input State Management
```javascript
import { useState } from "react";
function FormExample() {
const [formData, setFormData] = useState({name: "",email: "",message: ""});
const handleChange = (event) => {
const { name, value } = event.target;
setFormData((prevFormData) => ({ ...prevFormData, [name]: value }));
};
const handleSubmit = (event) => {
event.preventDefault();
alert(`Name: ${formData.name}, Email: ${formData.email}, Message: ${formData.message}`
);
};
return (
<form onSubmit={handleSubmit}>
<label htmlFor="name">Name:</label>
<input type="text" id="name" name="name" value={formData.name} onChange={handleChange}/>
<label htmlFor="email">Email:</label>
<input type="email" id="email" name="email" value={formData.email} onChange={handleChange}/>
<label htmlFor="message">Message:</label>
<textarea id="message" name="message" value={formData.message} onChange={handleChange}/>
<button type="submit">Submit</button>
</form>
);
}
export default FormExample;
```
### useEffect Hook
```javascript
import React, { useState, useEffect } from "react";
function Timer() {
const [seconds, setSeconds] = useState(0);
useEffect(() => {
const interval = setInterval(() => {
setSeconds((prevSeconds) => prevSeconds + 1);
}, 1000);
return () => clearInterval(interval);
}, []);
return <div>Seconds: {seconds}</div>;
}
export default Timer;
```
Note: The useEffect Hook in React is used for performing side effects in functional components. It allows you to execute code based on component lifecycle events like mounting, updating, and unmounting.
### Fetch API using useEffect
```javascript
import React, { useState, useEffect } from "react";
import axios from "axios";
function UserList() {
const [users, setUsers] = useState([]);
useEffect(() => {
axios.get("https://jsonplaceholder.typicode.com/users")
.then(response => {
setUsers(response.data);
})
.catch(error => {
console.error("Error fetching users:", error);
});
}, []);
return (
<div>
<h2>User List</h2>
<ul>
{users.map(user => (
<li key={user.id}>{user.name}</li>
))}
</ul>
</div>
);
}
export default UserList;
```
Note: Make sure to import Axios first to your project.
### Custom Hook creation useLocalStorage
```javascript
import { useState, useEffect } from "react";
function useLocalStorage(key, initialValue) {
const [value, setValue] = useState(() => {
const storedValue = localStorage.getItem(key);
return storedValue !== null ? JSON.parse(storedValue) : initialValue;
});
useEffect(() => {
localStorage.setItem(key, JSON.stringify(value));
}, [key, value]);
return [value, setValue];
}
export default useLocalStorage;
```
Note: Custom Hooks are reusable functions in React that contain logic shared across multiple components. They allow you to extract stateful logic from components into standalone functions.
+5
View File
@@ -0,0 +1,5 @@
<svg xmlns="http://www.w3.org/2000/svg" height="1em" width="1em" viewBox="0 0 12 12" version="1.1" xmlns:xlink="http://www.w3.org/1999/xlink" xml:space="preserve" xmlns:serif="http://www.serif.com/" style="fill-rule:evenodd;clip-rule:evenodd;stroke-linejoin:round;stroke-miterlimit:2;">
<g transform="matrix(0.0234375,0,0,0.0234375,0,0)">
<path d="M256,6.201L472.332,131.1L472.332,380.9L256,505.799L39.668,380.9L39.668,131.1L256,6.201ZM203.134,290.489L308.866,290.489L308.866,419.702L329.853,419.702L329.853,269.502L182.147,269.502L182.147,419.702L203.134,419.702L203.134,290.489ZM155.143,92.298L134.156,92.298L134.156,419.702L155.143,419.702L155.143,92.298ZM377.844,92.298L356.857,92.298L356.857,419.702L377.844,419.702L377.844,92.298ZM179.147,245.516L179.147,92.298L158.161,92.298L158.161,419.702L179.147,419.702L179.147,266.502L332.871,266.502L332.871,419.702L353.857,419.702L353.857,92.298L332.871,92.298L332.871,245.516L179.147,245.516ZM203.134,221.511L203.134,92.298L182.147,92.298L182.147,242.498L329.853,242.498L329.853,92.298L308.866,92.298L308.866,221.511L203.134,221.511Z"/>
</g>
</svg>

After

Width:  |  Height:  |  Size: 1.1 KiB