#3: Implementing actions in app
When you have basic actions and reducers set up and tested, you may start using them in your app. Based on the code responsible for dealing with data in the database, where do you think it may be the best place to dispatch actions? π€
We'll put our code inside list-manager.component.ts
.
Inside the removeItem
method, we want to dispatch deleteTodoItem
action in addition to calling the service. To do so, we need to initialize our Store somewhere in our app. The best way is to do it in the constructor, by adding the store as a parameter:
Dispatching an action is follows the pattern of calling the action name method and passing in props π
Or just
if you don't provide any props in action π
Try updating the removeItem
method.
β³β³β³β³β³β³
Are you finished?
Does it look somewhat like the example below?
Great job!
Now it's time for the addItem
method.
Since _id
is usually provided by MongoDB in our app, you may use an uuid generator to generate id for store purposes.
We used a library called 'uuid' and imported into our code like this:
Try to dispatch proper action in addItem
method.
β³β³β³β³β³β³
Does it look somewhat like the example below?
Awesome! Check it out using store devtools to see how it all works together! π€
The last one is updateItem
method. You're a pro now; you've got this! πͺ
You may sneak a peek on Stackblitz if you get stuck, but we encourage you to try youself first and then ask a mentor for help as you won't be able to always count on a prepared solution.
Do you have other actions to dispatch or code to add based on the brainstorm? Feel free to add them now!
Last updated
Was this helpful?