<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/wrapping-react-usestate-with-typescript-pk0ya3awt" -->

---
title: Wrapping React.useState with TypeScript | daily.dev
description: I made a useDarkMode hook that looks like this. It powers the &quot;dark mode&quot; support for all the Epic React workshop apps (for example React Fundamentals) I want...
canonical: https://daily.dev/posts/wrapping-react-usestate-with-typescript-pk0ya3awt
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Wrapping React.useState with TypeScript | daily.dev
og:description: I made a useDarkMode hook that looks like this. It powers the &quot;dark mode&quot; support for all the Epic React workshop apps (for example React Fundamentals) I want...
og:url: https://daily.dev/posts/wrapping-react-usestate-with-typescript-pk0ya3awt
og:image: https://api.daily.dev/og/posts/pk0ya3awt.png
og:image:alt: Wrapping React.useState with TypeScript
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Wrapping React.useState with TypeScript

**[Kent C. Dodds](https://daily.dev/sources/kentcdodds)** · [@kentcdodds](https://daily.dev/kentcdodds) · 4 min read · 17 upvotes · 2 comments

## Summary

I made a useDarkMode hook that looks like this. It powers the "dark mode" support for all the Epic React workshop apps (for example React Fundamentals) I want to call out a few things about the hook itself that made things work well from a TypeScript perspective. Let's clear out all the extra stuff and just look at the important bits.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://kentcdodds.com/blog/wrapping-react-use-state-with-type-script>

## Community discussion

Top comments from developers on daily.dev.

**@hamatoyogi** · 1 upvotes

> This is a great article to keep as a reference.

**@idoshamun** · 1 upvotes

> The `as const` trick is super neat! But I use an eslint plugin that forces me into setting the return type of every function. As mentioned in the article, this is also a possible solution for this issue. I find the latter more readable and maintainable.

## Similar posts on daily.dev

- [Don’t just attend KubeCon \+ CloudNativeCon, Merge Forward your experience\!](https://daily.dev/posts/don-t-just-attend-kubecon-cloudnativecon-merge-forward-your-experience--l0rpp73x8) · CNCF · 1 upvotes · 0 comments
- [Announcing H2 2026 KCDs](https://daily.dev/posts/announcing-h2-2026-kcds-m96goajm1) · CNCF · 1 upvotes · 0 comments
- [Two months of Open Community Groups](https://daily.dev/posts/two-months-of-open-community-groups-asf52zhbs) · CNCF · 0 upvotes · 0 comments

---

Tags: [#react](https://daily.dev/tags/react), [#typescript](https://daily.dev/tags/typescript)

[View this post on daily.dev](https://daily.dev/posts/wrapping-react-usestate-with-typescript-pk0ya3awt)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"Wrapping React.useState with TypeScript","url":"https://daily.dev/posts/wrapping-react-usestate-with-typescript-pk0ya3awt","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/wrapping-react-usestate-with-typescript-pk0ya3awt"},"datePublished":"2021-01-20T01:20:31.692Z","dateModified":"2021-10-07T14:17:10.317Z","description":"I made a useDarkMode hook that looks like this. It powers the \"dark mode\" support for all the Epic React workshop apps (for example React Fundamentals) I want...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/5b6971c64f87bbf862a967d8aa4183af","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/5b6971c64f87bbf862a967d8aa4183af","isAccessibleForFree":true,"articleSection":"Kent C. Dodds","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Person","name":"Kent C. Dodds","url":"https://daily.dev/kentcdodds","image":"https://avatars.githubusercontent.com/u/1500684?v=4","description":"My name is Kent C. Dodds and I'm a JavaScript engineer and teacher. I'm also active in the open source community. I like my family, JavaScript, and React.","interactionStatistic":{"@type":"InteractionCounter","interactionType":{"@type":"EndorseAction"},"userInteractionCount":1780}},"commentCount":2,"discussionUrl":"https://daily.dev/posts/wrapping-react-usestate-with-typescript-pk0ya3awt","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":17},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":2}],"keywords":"react,typescript","timeRequired":"PT4M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Kent C. Dodds","item":"https://daily.dev/sources/kentcdodds"},{"@type":"ListItem","position":3,"name":"Wrapping React.useState with TypeScript"}]}
{"@context":"https://schema.org","@type":"WebPage","@id":"https://daily.dev/posts/wrapping-react-usestate-with-typescript-pk0ya3awt","comment":[{"@type":"Comment","text":"This is a great article to keep as a reference.","datePublished":"2021-01-31T15:13:18.351Z","url":"https://daily.dev/posts/pk0ya3awt#c-6PUnBHwT9","author":{"@type":"Person","name":"Yoav Ganbar","url":"https://daily.dev/hamatoyogi","image":"https://avatars0.githubusercontent.com/u/6682981?v=4"},"interactionStatistic":{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":1}},{"@type":"Comment","text":"The as const trick is super neat! But I use an eslint plugin that forces me into setting the return type of every function. As mentioned in the article, this is also a possible solution for this issue. I find the latter more readable and maintainable.","datePublished":"2021-01-31T08:10:36.089Z","url":"https://daily.dev/posts/pk0ya3awt#c-_nmnKZecr","author":{"@type":"Person","name":"Ido Shamun","url":"https://daily.dev/idoshamun","image":"https://media.daily.dev/image/upload/s---xy_OAwk--/f_auto,q_auto/v1703781380/avatars/avatar_28849d86070e4c099c877ab6837c61f0"},"interactionStatistic":{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":1}}]}
```

