Add Actions
Get the project source code below, and follow along with the lesson material.
Download Project Source CodeTo set up the project on your local machine, please follow the directions provided in the README.md
file. If you run into any issues with running the project source code, then feel free to reach out to the author in the course's Discord channel.
Lesson Transcript
[00:00 - 00:08] Add the actions. Inside of the SRC folder, create new file, actions.ts, and here define the following type constants for the actions.
[00:09 - 00:23] Expert, const, begin stroke, is gonna equal begin stroke, expert, const, update stroke, the value is update stroke, and expert, const. And stroke equals and stroke.
[00:24 - 00:31] Now define the action type. Expert type, action, is gonna be either type, type of begin stroke.
[00:32 - 00:52] If you check the type, it's gonna be begin stroke value, because type here is the literal value of that string. And payload is gonna be a point, because we start a new stroke at some point, or type, type of update stroke, with payload also a point, because we continue the stroke, adding points to it.
[00:53 - 01:00] And then type, type of end stroke, won't have any payload. Define the action creators.
[01:01 - 01:18] Expert, const, begin stroke, is gonna be a function that will receive number. And y number, basically x and y coordinates, and we want to return a new object with type, begin stroke, with payload, representing a point.
[01:19 - 01:31] It's an object with x and y. The update stroke is gonna look very similar, update stroke, receives x and y, that type should be update stroke, and the payload is also a point.
[01:32 - 01:47] And define an expert, const and stroke, where you'll return an object with type and stroke. To make sure that you return proper action types, you can set the return type, action, and repeat it for all the action creators.
[01:48 - 02:00] Now, if you make a mistake, and for example, pass some payload with x and y, to end stroke that doesn't have any payload, you will get a TypeScript error. Let's remove the payload and save the file.
This lesson preview is part of the Fullstack React with TypeScript Masterclass course and can be unlocked immediately with a \newline Pro subscription or a single-time purchase. Already have access to this course? Log in here.
Get unlimited access to Fullstack React with TypeScript Masterclass, plus 70+ \newline books, guides and courses with the \newline Pro subscription.
![Thumbnail for the \newline course Fullstack React with TypeScript Masterclass](/_next/image?url=https%3A%2F%2Fs3.amazonaws.com%2Fassets.fullstack.io%2Fn%2F20220425171607321_CoverImage_FSReactWTypeScript_Masterclas.png&w=3840&q=75)