Author avatar

Matt Ferderer

Passing Functions in React with TypeScript

Matt Ferderer

  • Feb 6, 2019
  • 6 Min read
  • Feb 6, 2019
  • 6 Min read


If you’re new to TypeScript or React, it might not be obvious how to pass a function to a button or another component to change the state of your current component. Due to JavaScript’s “this” binding, it can become very confusing. This guide will show you how TypeScript can make this easy. In doing so, we will build a simple app to change the color and text of a box with a button. This would be useful for a teacher who wants to know if their students are understanding the material.

Defining TypeScript Functions

Here is an example of how to create a Toggle Button for the user to click.

1import * as React from 'react';
3interface ToggleProps {
4    ClickHandler: (event: React.MouseEvent<HTMLButtonElement>) => void
7export function Toggle(Props: ToggleProps) {
8    return <button onClick={Props.ClickHandler}>Toggle</button>

In this component, we create an interface that defines what kind of props our Toggle component should receive. We are expecting only one prop called ClickHandler. It takes a function and returns void. As a sample reference, the ClickHandler prop function is currently set up to receive an event parameter of React.MouseEvent type that is applied to an HTMLButtonElement. Any time you click on an element in JavaScript with your mouse, it receives this event property by default. We won’t be needing it in our example. If you did need information about the mouse click event, this is how you would let TypeScript know about the type of event and the type of element that was clicked.

Adding a Component to Use the Toggle Button

Next, we need to import our Toggle button into the component we plan to use it with. We will refer to this as our StatusCard component.

1import * as React from 'react';
2import { Toggle } from '../Toggle';
4interface StatusCardProps {
5    DefaultStatus: StatusTypes
8interface StatusCardState {
9    Status: StatusTypes
12type StatusTypes = "Good" | "Slow Down" | "Stuck";
14export default class StatusCard extends React.Component<StatusCardProps, StatusCardState> {
16    static defaultProps = {
17        DefaultStatus: "Good"
18    }
20    state = {
21        Status: this.props.DefaultStatus
22    }
24    toggleClickHandler = () => {
25        this.setState((prevState) => {
26            if (prevState.Status === "Good") {
27                return { Status: "Stuck" };
28            }
29            if (prevState.Status === "Slow Down") {
30                return { Status: "Good" };
31            }
32            return { Status: "Slow Down" };
33        })
34    }
36    getColor() {
37        if (this.state.Status === "Good") {
38            return "#0F0";
39        }
40        if (this.state.Status === "Slow Down") {
41            return "#f4cf53";
42        }
43        return "#F00";
44    }
46    render() {
47        return (
48            <>
49                <div
50                    style={{
51                        background: this.getColor(),
52                        color: "#000",
53                        height: "200px",
54                        width: "200px",
55                    }}>
56                    {this.state.Status}
57                </div>
58                <Toggle ClickHandler={this.toggleClickHandler} />
59            </>
60        )
61    }

Let’s break this down from the top.

First, we declare our component props and state interfaces, so TypeScript knows what type of props this component should receive and what type of state it keeps track of.

Inside our React component, you will notice a special static defaultProps property that allows us to tell TypeScript what props are not required and have a default set. You can read more in TypeScript’s documentation about setting default props for React.

We then set our default component state.

The next method is our toggleClickHandler. This method contains the logic we want to send to our ToggleButton component when it gets clicked. It will change the state of this component. The important part to notice is the use of the arrow method to create the function. This automatically binds JavaScript’s “this” to the StatusCard component. Without it, when we call this.setState JavaScript would look in for the state of the actual button that was clicked instead.

You can do this without using the arrow function but it adds more syntax. You have two alternate options. Both require using JavaScript's bind method.

You could add a this.toggleClickHandler = this.toggleClickHandler.bind(this); into your class constructor.

You could also pass this.toggleClickHandler.bind(this) into the Toggle Button Component ClickHandler prop.

To finish our component, there is a simple getColor method to display a different color and text depending on the state of our app. Last is our render method. The render method displays our component and the Toggle button component. Here we can pass our toggleClickHandler as a property to our Toggle component.


This guide has explained how to useto use a Toggle Button type component with a variety of other components. Each component can tell the Toggle Button what to do when clicked.

It should be noted that, in the above examples, you could have used a plain button component instead of creating a special Toggle component that wraps around the button. The Toggle component shows an extra layer of complexity when it comes to passing our toggleClickHandler method down multiple components. In this case, our toggleClickHandler gets passed to the Toggle component. The Toggle component then passes it to a plain button component. The whole time, our toggleClickHandler will remain connected to our StatusCard component when changing the state.