const[count,setCount]=useState<number>(0);setCount((oldCount)=>oldCount+1);// this guarantee that you will always access the latest value of count.
when a state is managed to child components, do not pass the setState function; instead, define functions that interact with the state and pass them to the child components.
typeTodo={id:string;text:string;completed:boolean};const[todos,setTodos]=useState<Todo[]>([]);constaddTodo=(text:string)=>{constnewTodo:Todo={id:uuidv4(),text,completed:false};setTodos((prevTodos)=>[...prevTodos,newTodo]);};constdeleteTodo=(id:string)=>{setTodos((prevTodos)=>prevTodos.filter((todo)=>todo.id!==id));};// ... pass those addTod, deleteTodo down to child components; not the `setTodos` function.<TodoFormaddTodo={addTodo}/>;{todos.map((todo)=><TododeleteTodo={deleteTodo}/>);}// Todo.tsx// ...code<div>{todo.text}<TrashonClick={props.deleteTodo.bind(null,todo.id)}//bindthedeleteTodofunctiontothe`Todo`object./></div>;
add styles, using component.module.css or inline-style:
// component.module.css.todo{...}// 1. use styles as `styles.className`:importstylesfrom"./styles.module.css";<divclassName={styles.todo}></div>;// 2. use styles as `className` (higher probability of css rules clashes):import"./styles.module.css";<divclassName="todo"></div>;// 3. inline styles:<divstyle={{color:"red"}}></div>;