createStore() . من أجل السماح لنا بدمج مخفضات متعددة معاً ، يوفر Redux أسلوب combineReducers() . تقبل هذه الطريقة كائنًا كوسيطة حيث تقوم بتعريف الخصائص التي تربط المفاتيح بوظائف المخفض المحددة. سيتم استخدام الاسم الذي تعطيه للمفاتيح بواسطة Redux كاسم حالة القطعة المرتبطة. عادة ، من الممارسات الجيدة إنشاء مخفض لكل حالة من حالات التطبيق عندما تكون متميزة أو فريدة من نوعها بطريقة ما. على سبيل المثال ، في تطبيق تدوين الملاحظات مع مصادقة المستخدم ، يمكن لمخفّض واحد التعامل مع المصادقة بينما يعالج آخر النص ويلاحظ أن المستخدم يرسل. بالنسبة إلى هذا التطبيق ، قد نكتب طريقة combineReducers() كما يلي: const rootReducer = Redux.combineReducers ({الآن ، ستحتوي
auth: authenticationReducer ،
ملاحظات: notesReducer
})؛
notes الرئيسية على جميع الحالات المرتبطة بملاحظاتنا ويتم التعامل معها من خلال notesReducer . هذه هي الطريقة التي يمكن بها إنشاء مخفضات متعددة لإدارة حالة تطبيق أكثر تعقيدًا. في هذا المثال ، ستكون الحالة الموجودة في مخزن Redux عبارة عن كائن واحد يحتوي على خصائص auth notes . counterReducer() و authReducer() في محرر التعليمة البرمجية ، بالإضافة إلى مخزن Redux. إنهاء كتابة الدالة rootReducer() باستخدام الأسلوب Redux.combineReducers() . قم بتعيين counterReducer إلى مفتاح يسمى count و authReducer إلى مفتاح يسمى auth . 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.");'
```