You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A helper function that can be used to make action creators.
161
+
162
+
Action creators are helper objects that will generate actions from provided data and automatically populate the `type` field.
163
+
164
+
Actions often have a structure that looks like this:
165
+
166
+
```lua
167
+
localMyAction= {
168
+
type="SetFoo",
169
+
value=1,
170
+
}
171
+
```
172
+
173
+
They are often generated by functions that take the action's data as arguments:
174
+
175
+
```lua
176
+
localfunctionSetFoo(value)
177
+
return {
178
+
type="SetFoo",
179
+
value=value,
180
+
}
181
+
end
182
+
```
183
+
184
+
`makeActionCreator` looks similar, but it automatically populates the action's type with the action creator's name. This makes it easier to keep track of which actions your reducers are responding to:
185
+
186
+
Make an action creator in `SetFoo.lua`:
187
+
```lua
188
+
returnmakeActionCreator("SetFoo", function(value)
189
+
-- The action creator will automatically add the 'type' field
190
+
return {
191
+
value=value,
192
+
}
193
+
end)
194
+
```
195
+
196
+
Then check for that action by name in `FooReducer.lua`:
197
+
```lua
198
+
localSetFoo=require(SetFoo)
199
+
...
200
+
ifaction.type==SetFoo.namethen
201
+
-- change some state!
202
+
end
203
+
```
204
+
155
205
## Middleware
156
206
Rodux provides an API that allows changing the way that actions are dispatched called *middleware*. To attach middleware to a store, pass a list of middleware as the third argument to `Store.new`.
In most cases your `action` will be sent directly to the `reducer` to be processed. However, if you specified any `middleware` when initializing your `store`, your `action` might also be processed by that `middleware`.
25
+
In most cases your `action` will be sent directly to the `reducer` to be processed. However, if you specified any `middleware` when initializing your `store`, your `action` might also be processed by that `middleware`.
26
+
27
+
Additionally, Rodux provides a helper method called `makeActionCreator` to generate 'action creators'. These are a lot like the `ReceivedNewPhoneNumber` function above, except for two key differences:
28
+
29
+
* Instead of functions, action creators returned from `makeActionCreator` are callable tables that also include a `name` field.
30
+
* Action creators will automatically populate the `type` field of each action they create using their `name`.
Since the `name` of the action creator populates the `type` of the actions it creates, we can use an action creators `name` to identify actions that were created by it. As we'll see in the Reducers section, this is helpful for determining which action we're processing:
43
+
44
+
```lua
45
+
localMyAction=require(MyAction)
46
+
...
47
+
ifaction.type==MyAction.namethen
48
+
-- change some state!
49
+
end
50
+
```
51
+
52
+
!!! info
53
+
Actions are nothing more than tables with a `type` field, so there are many ways to generate them! If `makeActionCreator` doesn't work for your project, you can always generate actions and action creators however you like!
0 commit comments