--- id: 5a24c314108439a4d4036154 title: Combine Multiple Reducers challengeType: 6 isRequired: false videoUrl: '' localeTitle: الجمع بين متعددة المخفضات --- ## Description
عندما تبدأ حالة تطبيقك في التزايد أكثر تعقيدًا ، قد يكون من المغري تقسيم الدولة إلى أجزاء متعددة. بدلاً من ذلك ، تذكر المبدأ الأول لـ Redux: يتم الاحتفاظ بكل حالة التطبيق في كائن حالة واحد في المتجر. لذلك ، يوفر Redux تركيبة المخفض كحل لنموذج حالة معقد. يمكنك تحديد مخفضات متعددة للتعامل مع أجزاء مختلفة من حالة التطبيق الخاص بك ، ثم يؤلف هذه المخفضات معًا في مخفض جذري واحد. يتم بعد ذلك تمرير مخفض الجذر إلى طريقة createStore() . من أجل السماح لنا بدمج مخفضات متعددة معاً ، يوفر Redux أسلوب combineReducers() . تقبل هذه الطريقة كائنًا كوسيطة حيث تقوم بتعريف الخصائص التي تربط المفاتيح بوظائف المخفض المحددة. سيتم استخدام الاسم الذي تعطيه للمفاتيح بواسطة Redux كاسم حالة القطعة المرتبطة. عادة ، من الممارسات الجيدة إنشاء مخفض لكل حالة من حالات التطبيق عندما تكون متميزة أو فريدة من نوعها بطريقة ما. على سبيل المثال ، في تطبيق تدوين الملاحظات مع مصادقة المستخدم ، يمكن لمخفّض واحد التعامل مع المصادقة بينما يعالج آخر النص ويلاحظ أن المستخدم يرسل. بالنسبة إلى هذا التطبيق ، قد نكتب طريقة combineReducers() كما يلي:
const rootReducer = Redux.combineReducers ({
auth: authenticationReducer ،
ملاحظات: notesReducer
})؛
الآن ، ستحتوي notes الرئيسية على جميع الحالات المرتبطة بملاحظاتنا ويتم التعامل معها من خلال notesReducer . هذه هي الطريقة التي يمكن بها إنشاء مخفضات متعددة لإدارة حالة تطبيق أكثر تعقيدًا. في هذا المثال ، ستكون الحالة الموجودة في مخزن Redux عبارة عن كائن واحد يحتوي على خصائص auth notes .
## Instructions
توجد counterReducer() و authReducer() في محرر التعليمة البرمجية ، بالإضافة إلى مخزن Redux. إنهاء كتابة الدالة rootReducer() باستخدام الأسلوب Redux.combineReducers() . قم بتعيين counterReducer إلى مفتاح يسمى count و authReducer إلى مفتاح يسمى auth .
## Tests
```yml tests: - text: يجب على counterReducer زيادة counterReducer state . testString: 'assert((function() { const initalState = store.getState().count; store.dispatch({type: INCREMENT}); store.dispatch({type: INCREMENT}); const firstState = store.getState().count; store.dispatch({type: DECREMENT}); const secondState = store.getState().count; return firstState === initalState + 2 && secondState === firstState - 1 })(), "The counterReducer should increment and decrement the state.");' - text: يجب على authReducer تبديل state authenticated بين true و false . testString: 'assert((function() { store.dispatch({type: LOGIN}); const loggedIn = store.getState().auth.authenticated; store.dispatch({type: LOGOUT}); const loggedOut = store.getState().auth.authenticated; return loggedIn === true && loggedOut === false })(), "The authReducer should toggle the state of authenticated between true and false.");' - text: 'يجب أن تحتوي state المتجر على مفتاحين: count ، الذي يحتفظ برقم ، و auth ، الذي يحمل كائنًا. يجب أن يكون لعنصر auth خاصية authenticated ، والتي تحمل قيمة منطقية.' testString: 'assert((function() { const state = store.getState(); return typeof state.auth === "object" && typeof state.auth.authenticated === "boolean" && typeof state.count === "number" })(), "The store state should have two keys: count, which holds a number, and auth, which holds an object. The auth object should have a property of authenticated, which holds a boolean.");' - text: يجب أن يكون rootReducer دالة تجمع بين counterReducer و authReducer . testString: 'getUserInput => assert((function() { const noWhiteSpace = getUserInput("index").replace(/\s/g,""); return typeof rootReducer === "function" && noWhiteSpace.includes("Redux.combineReducers") })(), "The rootReducer should be a function that combines the counterReducer and the authReducer.");' ```
## Challenge Seed
```jsx const INCREMENT = 'INCREMENT'; const DECREMENT = 'DECREMENT'; const counterReducer = (state = 0, action) => { switch(action.type) { case INCREMENT: return state + 1; case DECREMENT: return state - 1; default: return state; } }; const LOGIN = 'LOGIN'; const LOGOUT = 'LOGOUT'; const authReducer = (state = {authenticated: false}, action) => { switch(action.type) { case LOGIN: return { authenticated: true } case LOGOUT: return { authenticated: false } default: return state; } }; const rootReducer = // define the root reducer here const store = Redux.createStore(rootReducer); ```
## Solution
```js // solution required ```