Reducer pattern, action/dispatch, pure funksiyalar, Todo va Shopping Cart reducerlar — test va mashqlar bilan.
useState oddiy holatlar uchun yetarli. State murakkablashganda (bir nechta bog'liq maydon, ko'p turdagi yangilanish) — useReducer aniqroq tuzilma beradi.
App.jsxReact JSXfunction reducer(state, action) {switch (action.type) {case "increment":return { count: state.count + 1 };case "decrement":return { count: state.count - 1 };default:return state;}}function Counter() {const [state, dispatch] = useReducer(reducer, { count: 0 });return (<><button onClick={() => dispatch({ type: "increment" })}>+</button><span>{state.count}</span><button onClick={() => dispatch({ type: "decrement" })}>-</button></>);}
code.textTEXTUser Action → dispatch(action) → Reducer(state, action) → Yangi State → UI Update
App.jsxReact JSXfunction todoReducer(todos, action) {switch (action.type) {case "add":return [...todos, { id: Date.now(), text: action.text, done: false }];case "toggle":return todos.map((t) => t.id === action.id ? { ...t, done: !t.done } : t);case "remove":return todos.filter((t) => t.id !== action.id);default:return todos;}}
Task: Increment/Decrement/Reset action'lari bilan Counter reducer yozing.
Task: Todo qo'shish, o'chirish, bajarilgan deb belgilash — barchasi reducer orqali.
Task: Shopping Cart — mahsulot qo'shish, o'chirish, sonini o'zgartirish va umumiy narxni hisoblash.
App.jsxReact JSXfunction reducer(state, action) {switch (action.type) {case "increment":state.count++;return state;default:return state;}}
Your Task: Bu reducer nega noto'g'ri?
<details> <summary>Solution</summary> `state.count++` — state'ni to'g'ridan-to'g'ri mutate qiladi, bu React/reducer qoidalarini buzadi. Yechim: ```jsx case "increment": return { ...state, count: state.count + 1 }; ``` </details>useReducer — bir nechta bog'liq state yangilanishini bitta joyga jamlaydi. Reducer — har doim pure funksiya. dispatch({ type, payload }) — "nima sodir bo'lishi kerak"ni e'lon qilish, "qanday" qilishni reducer hal qiladi.
dispatch — "nima" ni e'lon qiladi, reducer "qanday"ni hal qiladiApp.jsxReact JSXfunction reducer(state, action) {switch (action.type) {case "TYPE": return { ...state, /* yangilanish */ };default: return state;}}const [state, dispatch] = useReducer(reducer, initialState);dispatch({ type: "TYPE", payload: data });
Mavzu bo‘yicha tushunchalaringizni interaktiv test orqali sinovdan o‘tkazing va natijalarni bilib oling.
Darslikda o‘rganilgan qoidalarga asosan real kod yozing, testlardan o‘tkazing va yechimlarni mustahkamlang.
Test Sinovi
Hali topshirilmagan (kamida 75% kerak)
Amaliy Masalalar
0 / 3 ta yechildi (kamida 75% kerak)
Dars sifati va mazmunini yaxshilash uchun o‘z haqqoniy bahoingizni qoldiring.
Mavzuni to‘liq o‘zlashtirish uchun test sinovi, amaliy kodlash va bog‘liq darslar
O‘z bilimingizni interaktiv test savollari orqali tekshiring va darhol xatolarni tahlil qiling.
Nazariyani real kod yozish bilan mustahkamlang, avtomatik testlardan o‘ting va XP to‘plang.
Kurs bo‘yicha keyingi va bog‘liq mavzular: