What is a reducer in Rodux?
masterA reducer is a function provided to Store.new that determines how the application state changes in response to dispatched actions.
Key principles of reducers:
- Input: It receives the current
stateand the dispatchedaction. - Output: It must return a new state object.
- Immutability: The
stateis read-only. You must never modify the existing state object directly; instead, you must construct and return a new object (e.g., a new table) representing the updated state. - Default Behavior: If the reducer does not handle a specific action type, it should return the current
stateunchanged.
local phoneNumberReducer = function(state, action)
if action.type == "ReceivedNewPhoneNumber" then
return action.phoneNumber
end
return state
end