What are the differences between useState and useReducer Hooks in React?
Các phương án trả lời
Chọn một phương ánLời giảiChọn phương án để mở
Đáp án đúng: "useState is used for managing simple state, while useReducer is used for complex state and state transitions.". useState hợp với state đơn giản và độc lập như một số, một chuỗi hay một cờ bật tắt; useReducer gom mọi cách chuyển trạng thái vào một hàm reducer(state, action), hợp khi state là đối tượng nhiều trường liên quan hoặc giá trị kế tiếp phụ thuộc giá trị trước theo nhiều loại hành động. "useState is a built-in Hook, while useReducer is a custom Hook." sai vì cả hai đều là hook có sẵn của React, import trực tiếp từ gói react. "useState is used for class components, while useReducer is used for functional components." sai vì không hook nào dùng được trong class component; cả hai chỉ dành cho function component. "useState is for local component state, while useReducer is for global application state." nhầm: cả hai đều tạo state cục bộ của component; muốn chia sẻ cho toàn ứng dụng thì phải kết hợp useReducer với Context hoặc dùng Redux.