Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
To make the Counter component interactive, we need to be able to trigger changes to the data in state. We'll first create an event handler that updates state, using React's built-in setState()
method. Then we'll give the buttons an onClick
event that calls the event handler when clicked.
-
0:00
In React, your UI changes from one state to another.
-
0:03
And state is local to a component, meaning a component can maintain its own state,
-
0:08
unlike props, which are read-only.
-
0:11
So to make our counter interactive,
-
0:13
we need to be able to trigger changes to the data in state.
-
0:16
Now that the score is in our state, we can finally change a player's score.
-
0:21
The score changes when the user clicks the plus or minus button.
-
0:26
So first, we'll create the function or
-
0:28
event handler that updates our state using React's built-in set state method.
-
0:33
Then, we'll give our buttons an onClick event that calls the event handler when
-
0:38
clicked.
-
0:39
Whenever the score state gets updated, React will re-render our component and
-
0:44
the change will be visible in our UI.
-
0:47
In class components,
-
0:48
a common pattern is to create event handlers as a method on the class.
-
0:53
So first, let's create a new method on our component called incrementScore.
-
1:01
This method name has no special meaning in React, it's just a name I came up with.
-
1:06
So to test that our method works, let's log a message to
-
1:11
the console that says, Hi, from inside incrementScore.
-
1:19
Now we'll need to instruct React to listen for the click event on a button.
-
1:24
For this, we'll use React's built-in onClick event.
-
1:27
React events are similar to JavaScript events except that they are written inline
-
1:32
and they named using CamelCase.
-
1:35
In the increment button, we'll specific the event we're listening for, onClick.
-
1:41
And this event is specific to React, so you must name it onClick.
-
1:45
React has other built-in events, like onChange and onSubmit,
-
1:49
which you'll learn about in a later course.
-
1:51
You pass React events, a JSX expression, using curly braces and
-
1:56
the event handler that will get called when the specified event happens.
-
2:01
So let's pass the incrementScore method to the button's
-
2:06
onClick event with this.incrementScore.
-
2:11
And notice how I'm not using parentheses to call incrementScore like we usually
-
2:15
call functions or methods in JavaScript.
-
2:18
We're only passing a reference to the method.
-
2:21
Adding parentheses will call incrementScore and make it run right when
-
2:25
the component mounts, or gets displayed on the page, which we don't want.
-
2:29
We want React to call incrementScore only when the onClick event is fired.
-
2:36
Let's save our file in over in the browser.
-
2:38
I'll refresh, click the + button and in the console,
-
2:43
we see the message, Hi, from inside incrementScore!.
-
2:47
Good, our button is now wired up to the event handler.
-
2:51
So in the next video, we'll update the score state inside our method.
You need to sign up for Treehouse in order to download course files.
Sign up