VOOZH about

URL: https://thenewstack.io/learn-react-add-event-functionality-to-a-component/

⇱ Learn React: Add Event Functionality to a Component - The New Stack


TNS
SUBSCRIBE
Join our community of software engineering leaders and aspirational developers. Always stay in-the-know by getting the most important news and exclusive content delivered fresh to your inbox to learn more about at-scale software development.
REQUIRED
It seems that you've previously unsubscribed from our newsletter in the past. Click the button below to open the re-subscribe form in a new tab. When you're done, simply close that tab and continue with this form to complete your subscription.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.
Welcome and thank you for joining The New Stack community!
Please answer a few simple questions to help us deliver the news and resources you are interested in.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Great to meet you!
Tell us a bit about your job so we can cover the topics you find most relevant.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Welcome!

We’re so glad you’re here. You can expect all the best TNS content to arrive Monday through Friday to keep you on top of the news and at the top of your game.

What’s next?

Check your inbox for a confirmation email where you can adjust your preferences and even join additional groups.

Follow TNS on your favorite social media networks.

Become a TNS follower on LinkedIn.

Check out the latest featured and trending stories while you wait for your first TNS newsletter.

PREV
1 of 2
NEXT
VOXPOP
As a JavaScript developer, what non-React tools do you use most often?
Angular
0%
Astro
0%
Svelte
0%
Vue.js
0%
Other
0%
I only use React
0%
I don't use JavaScript
0%
Thanks for your opinion! Subscribe below to get the final results, published exclusively in our TNS Update newsletter:
NEW! Try Stackie AI
From clobbered drafts to real-time sync
Apr 14th 2026 10:00am, by David Moore
TypeScript 6.0 RC arrives as a bridge to a faster future
Mar 14th 2026 9:00am, by Darryl K. Taft
Mastra empowers web devs to build AI agents in TypeScript
Jan 28th 2026 11:00am, by Loraine Lawson
2022-06-11 03:00:36
Learn React: Add Event Functionality to a Component
tutorial,
Software Development

Learn React: Add Event Functionality to a Component

We are going to build a new React component, SubmitComponent, add event functional (add text through the text bar). The topics covered will be setState and JavaScript Event Handlers.
Jun 11th, 2022 3:00am by Jessica Wachtel
👁 Featued image for: Learn React: Add Event Functionality to a Component
Welcome to the third installment of our learning React.js tutorial series (Part 1), (Part 2). Here’s a link to the GitHub Repo. The Read Me has all the instructions needed to get started plus some helpful links for anyone unfamiliar with GitHub.

There is a concept in React that state is immutable. This doesn’t mean state can’t be changed. It just means we don’t mutate state directly. We do it with outside functions and copies.

The rule of thumb in React is to modify state via a setState function rather than mutate state directly. For example, our state is an array so rather than writing something like this.state.taks.push(insert new item here) our function will look a lot different and utilize setState.

It’s possible to update state with the push method directly, I believe there are use cases where it provides update functionality but it causes bugs along the component tree and I don’t recommend it. Here are some excellent resources for a deeper dive into setState from Kingsley Silas and one from Geeks for Geeks.

In this article, we are going to build a new React.js component, SubmitComponent, and add event functionality — to add text through the text bar. (We won’t discuss the JavaScript Event handlers themselves in depth so please check out this article instead if you’re curious.)

Setting up Our App for the New Code

In index.js (from the Github folder) , please make sure that your index.js looks like the image below:

👁 Image

Please update TaskComponent as well to look like this:

👁 Image

And on to the new code!

SubmitComponent

React is built from reusable components so let’s start by setting up a new file in the src folder. I’m going to call it SubmitComponent but you can call it whatever you want.

This component will hold the submit elements – the text input box/ text input box functionality and the submit button/submit button functionality.

The component itself has the same skeleton as the App and TaskComponent components. Don’t forget to add the element to the app component and import.

Once that’s set up, add the HTML element code for a text box and a button. Don’t worry about styling. We can style everything later on once we have full functionality.

It should look like this once everything is built.

👁 Image

Run the npm start script and check the browser to see if everything is working.

👁 Image

Add Functionality

Write the main functionality in “my app” component. The space between the end if constructor and the beginning of the render is where you write the function. In React, functions usually start with the word handle.

The first bit of functionality we are going to add is the handleChange. This is where we track the changes in the text box and make sure each character typed into the box is saved in state. Here are the steps:

  1. Create a new empty string variable in state.
  2. Create the function that will hold setState.
  3. Bind the function in the constructor.
  4. Pass the function to the element as a prop in the App component.
  5. Add the function as a prop to the element in the SubmitComponent.

To expand:

Step 1: Create The New Variable As An Empty String In State

Since we are typing a string in the text bar, we can add it to state as a string. While state is immutable and we can’t update it directly, that relates to updates from the browser, in terms of hard coding, state can be updated directly.

The new state should look like this.

👁 Image

Step 2: Create the New Function That Will Hold Set State

This will look a lot like the traditional event handler that tracks changes but is just updated for the React functionality. The React Docs are helpful if you want a deeper dive on this (image below under step 3).

Step 3: Bind the Function in the Constructor

The function .bind() creates a new bound function that wraps the original function object in order for the function to work. I often forget this part until my function doesn’t trigger, which is why I listed it as its own step. For more details, please see this link.

👁 Image

Step 4: Pass the Function to the Element as a Prop in the App Component

This one is pretty standard and we did it in the last tutorial as well. In the submit component element in the return section on the App component, I defined a variable called handleChange = {this.handleChange}. Since the handleChange function isn’t in state, we don’t call it this.state.handle change but it will still be called as a prop in the submit component.

👁 Image

Step 5: Add the Function as a Prop to the Submit Component

Last step! In the input box element characteristics, add the onChange = {this.props.handleChange}. I like to track my changes using an <h1> tag. Otherwise, how would I be able to tell that my state is getting updated? I’ll have to then pass the text string in state as a variable to the submit component as a prop.

👁 Image

These steps are pretty straightforward and can be used when creating components and adding functionality. Can you now add a submit button that has a new functionality when it’s clicked?

Next week we are going to add a submit button that adds the input text to the task array and maps through the task array and displays multiple tasks on the page. I highly recommend getting familiar with the JavaScript map functionality as well as setState.

The GitHub is updated. You can check out the files on GitHub as well.

Next Tutorial: Click Functionality and Reusable Components.

TRENDING STORIES
Jessica Wachtel is a developer marketing writer at InfluxData where she creates content that helps make the world of time series data more understandable and accessible. Jessica has a background in software development and technical journalism.
Read more from Jessica Wachtel
SHARE THIS STORY
TRENDING STORIES
SHARE THIS STORY
TRENDING STORIES
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.