createStore() Redux. Para permitirnos combinar varios reductores juntos, Redux proporciona el método combineReducers() . Este método acepta un objeto como un argumento en el que define propiedades que asocian claves a funciones reductoras específicas. Redux usará el nombre que le dé a las teclas como el nombre para el estado asociado. Normalmente, es una buena práctica crear un reductor para cada parte del estado de la aplicación cuando son distintos o únicos de alguna manera. Por ejemplo, en una aplicación para tomar notas con autenticación de usuario, un reductor podría manejar la autenticación, mientras que otro maneja el texto y observa que el usuario está enviando. Para una aplicación de este tipo, podríamos escribir el método combineReducers() siguiente manera: const rootReducer = Redux.combineReducers ({Ahora, las
auth: authenticationReducer,
notas: notas reductor
});
notes clave contendrán todo el estado asociado con nuestras notas y manejado por nuestro notesReducer . Así es como se pueden componer varios reductores para administrar un estado de aplicación más complejo. En este ejemplo, el estado mantenido en el almacén de Redux sería entonces un único objeto que contiene las propiedades auth y notes . counterReducer() y authReducer() proporcionadas en el editor de código, junto con una tienda Redux. Termine de escribir la función rootReducer() utilizando el método Redux.combineReducers() . Asigne counterReducer a una clave llamada count y authReducer a una clave llamada auth . counterReducer debe counterReducer y disminuir el 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: El authReducer debe cambiar el state de authenticated entre true y 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: 'El state tienda debe tener dos claves: count , que contiene un número, y auth , que contiene un objeto. El objeto auth debe tener una propiedad de authenticated , que contiene un valor booleano.'
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: El rootReducer debe ser una función que combine el counterReducer y el 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.");'
```