\\\\n\\\\n
\\\\n\\\\n)\\\\n\\\\n}\\\\n\\\\nEvery time the button is clicked, `setCount` updates the count value, and React re-executes the Counter function with the new value, returning new JSX, and the page updates automatically.\\\\n\\\\n> React's convention: state is destructured as `[something, setSomething]`. The set prefix is a conventional naming pattern, not a strict requirement, but the entire community follows this convention.\\\\n\\\\n* * *\\\\n\\\\n## Why Can't We Directly Modify State?\\\\n\\\\nIn React, state is read-only. You must use the setter function to update it, not modify it directly.\\\\n\\\\n## Example\\\\n\\\\nimport{ useState } from 'react'\\\\n\\\\nfunction ArticleManager(){\\\\n\\\\nconst[title, setTitle]= useState('Default Title')\\\\n\\\\n// Wrong way: modify directly\\\\n\\\\n// title = 'New Title' // React Doesn't know you changed it, won't re-render\\\\n\\\\n// Correct way: update via setter\\\\n\\\\nfunction updateTitle(){\\\\n\\\\n setTitle('New Title')// React Knows state changed, will re-render\\\\n\\\\n}\\\\n\\\\nreturn(\\\\n\\\\nClick count:{count}
\\\\n\\\\n\\\\n\\\\n\\\\n\\\\n
\\\\n\\\\n)\\\\n\\\\n}\\\\n\\\\nThis follows React's Immutable principle: don't modify old data, but replace it with new data.\\\\n\\\\n* * *\\\\n\\\\n## The Correct Way to Update Arrays and Objects\\\\n\\\\nFor arrays and objects, you cannot directly push, splice, or modify properties β you must create new copies.\\\\n\\\\n## Example\\\\n\\\\nimport{ useState } from 'react'\\\\n\\\\nfunction TodoList(){\\\\n\\\\nconst[todos, setTodos]= useState([\\\\n\\\\n{ id:1, text:'Learn React', done:false},\\\\n\\\\n{ id:2, text:'Write Blog', done:true}\\\\n\\\\n])\\\\n\\\\n// Add an item: spread old array + new element\\\\n\\\\nfunction addTodo(text){\\\\n\\\\nconst newTodo ={ id:Date.now(), text, done:false}\\\\n\\\\n setTodos([...todos, newTodo])// Useuse the spread operator to create a new array\\\\n\\\\n}\\\\n\\\\n// Edit an item: map through, find the one to edit, return new object\\\\n\\\\nfunction toggleTodo(id){\\\\n\\\\n setTodos(todos.map(todo =>\\\\n\\\\n todo.id=== id ?{ ...todo, done:!todo.done}: todo\\\\n\\\\n))\\\\n\\\\n}\\\\n\\\\n// Delete an item: filter out the unwanted\\\\n\\\\nfunction removeTodo(id){\\\\n\\\\n setTodos(todos.filter(todo => todo.id!== id))\\\\n\\\\n}\\\\n\\\\nreturn(\\\\n\\\\n{title}
\\\\n\\\\n\\\\n\\\\n- \\\\n\\\\n{todos.map(todo =>(\\\\n\\\\n
- toggleTodo(todo.id)}>\\\\n\\\\n{todo.done?
{todo.text}: todo.text}\\\\n\\\\n \\\\n\\\\n))}\\\\n\\\\n
\\\\n\\\\n\\\\n\\\\n))}\\\\n\\\\n
\\\\n\\\\n)\\\\n\\\\n}\\\\n\\\\n> For useMemo(fn, deps), the dependency array must be complete: all component internal variables used in the function should be declared in the array. React uses shallow comparison to decide whether to recalculate.\\\\n\\\\n* * *\\\\n\\\\n## Hands-on: Add Category Filtering to the Blog\\\\n\\\\n## Example\\\\n\\\\n// File Path: src/App.jsx\\\\n\\\\nimport{ useState, useMemo } from 'react'\\\\n\\\\nimport'./App.css'\\\\n\\\\nfunction App(){\\\\n\\\\nconst= useState([\\\\n\\\\n{ id:1, title:'React Getting StartedComplete Guide', summary:'FromLearn React from Zero', category:'ReactTotal {totalCount} postsοΌcurrent {filteredCount} posts
\\\\n\\\\n\\\\n\\\\n\\\\n\\\\n\\\\n\\\\n\\\\n\\\\n{filteredArticles.map(a =>(\\\\n\\\\n
YouTip