state
a Redux. Este es el primer paso para conectar la aplicación React simple a Redux. La única funcionalidad que tiene su aplicación es agregar nuevos mensajes del usuario a una lista desordenada. El ejemplo es simple para demostrar cómo React y Redux trabajan juntos. ADD
. A continuación, defina un creador de acción addMessage()
que crea la acción para agregar un mensaje. Deberá pasar un message
a este creador de acción e incluir el mensaje en la action
devuelta. Luego cree un reductor llamado messageReducer()
que maneje el estado de los mensajes. El estado inicial debe ser igual a una matriz vacía. Este reductor debe agregar un mensaje a la matriz de mensajes retenidos o devolver el estado actual. Finalmente, crea tu tienda Redux y pásale el reductor. ADD
debe existir y mantener un valor igual a la cadena ADD
testString: 'assert(ADD === "ADD", "The const ADD
should exist and hold a value equal to the string ADD
");'
- text: El creador de la acción addMessage
debería devolver un objeto con un type
igual a ADD
y un mensaje igual al mensaje que se pasa.
testString: 'assert((function() { const addAction = addMessage("__TEST__MESSAGE__"); return addAction.type === ADD && addAction.message === "__TEST__MESSAGE__"; })(), "The action creator addMessage
should return an object with type
equal to ADD
and message equal to the message that is passed in.");'
- text: messageReducer
debería ser una función.
testString: 'assert(typeof messageReducer === "function", "messageReducer
should be a function.");'
- text: El almacén debe existir y tener un estado inicial establecido en una matriz vacía.
testString: 'assert((function() { const initialState = store.getState(); return typeof store === "object" && initialState.length === 0; })(), "The store should exist and have an initial state set to an empty array.");'
- text: El envío de addMessage
contra la tienda debe agregar de manera inmutable un nuevo mensaje a la matriz de mensajes mantenidos en estado.
testString: 'assert((function() { const initialState = store.getState(); const isFrozen = DeepFreeze(initialState); store.dispatch(addMessage("__A__TEST__MESSAGE")); const addState = store.getState(); return (isFrozen && addState[0] === "__A__TEST__MESSAGE"); })(), "Dispatching addMessage
against the store should immutably add a new message to the array of messages held in state.");'
- text: El messageReducer
debería devolver el estado actual si se llama con cualquier otra acción.
testString: 'assert((function() { const addState = store.getState(); store.dispatch({type: "FAKE_ACTION"}); const testState = store.getState(); return (addState === testState); })(), "The messageReducer
should return the current state if called with any other actions.");'
```