12/03/2024
12/02/2024
12/01/2024
1// ReactTips.jsx23// Tip 1: Leverage `use` for fetching data in server components4// React 19 introduces the `use` hook for server components5import { use }from "react";67async function fetchUser() {8const response = await fetch("/api/user");9return response.json();10}1112const UserProfile = async () => {13// ✅ Use `use` for seamless data fetching in server components14const user = await use(fetchUser());1516return (17<div>18<h1>{user.name}</h1>19<p>Email: {user.email}</p>20</div>21);22};2324// Tip 2: Use `<Suspense>` with server-side rendering (SSR)25import React, { Suspense } from "react";2627const UserProfile = React.lazy(() => import("./UserProfile"));2829const App = () => {30return (31<Suspense fallback={<p>Loading user profile...</p>}>32<UserProfile />33</Suspense>34);35};3637// Tip 3: Utilize the `useId` hook for unique IDs38import { useId } from "react";3940const Form = () => {41// ✅ useId ensures unique IDs across client and server42const id = useId();4344return (45<div>46<label htmlFor={`${id}-input`}>Name:</label>47<input id={`${id}-input`} type="text" />48</div>49);50};5152// Tip 4: Optimize performance with `useTransition`53import { useState, useTransition } from "react";5455const Search = ({ items }) => {56const [searchTerm, setSearchTerm] = useState("");57const [filteredItems, setFilteredItems] = useState(items);58const [isPending, startTransition] = useTransition();5960const handleSearch = (event) => {61const value = event.target.value;62setSearchTerm(value);6364startTransition(() => {65const filtered = items.filter((item) =>66item.toLowerCase().includes(value.toLowerCase())67);68setFilteredItems(filtered);69});70};7172return (73<div>74<input type="text" value={searchTerm} onChange={handleSearch} />75{isPending && <p>Loading...</p>}76<ul>77{filteredItems.map((item, index) => (78<li key={index}>{item}</li>79))}80</ul>81</div>82);83};84
1// ReactTips.jsx23// Tip 1: Use optional chaining to avoid errors when accessing deeply nested properties4const UserProfile = ({ user }) => {5// ❌ Avoid this approach, it can cause runtime errors6const city = user && user.address && user.address.city;78// ✅ Use optional chaining for cleaner and safer code9const city = user?.address?.city;1011return <p>City: {city || "Unknown"}</p>;12};1314// Tip 2: Use functional updates when updating state that depends on previous state15import { useState } from "react";1617const Counter = () => {18const [count, setCount] = useState(0);1920// ❌ Avoid this, it might not reflect the latest state21const increment = () => setCount(count + 1);2223// ✅ Use a functional update for more reliable state management24const incrementFunctional = () => setCount((prevCount) => prevCount + 1);2526return (27<div>28<p>Count: {count}</p>29<button onClick={incrementFunctional}>Increment</button>30</div>31);32};33
1// ReactTips.jsx23// Tip 1: Strings can be passed directly as props4const Input = () => {5// ❌ Avoid doing this6return <input placeholder={"Your name"} />;78// ✅ Strings can be passed directly without {}9return <input placeholder="Your name" />;10};1112// Tip 2: Avoid passing `true` explicitly to boolean props13const Input = () => {14// ❌ Avoid doing this15return <input placeholder="Your name" disabled={true} />;1617// ✅ You only need to specify the property18return <input placeholder="Your name" disabled />;19};2021// Tip 3: Use Fragments (<></>) only when necessary22const Input = () => {23// ❌ Don't do this, it's unnecessary24return (25<>26<input placeholder="Your name" disabled />27</>28);2930// ✅ You can simply return the component31return <input placeholder="Your name" disabled />;32};33