meta_pixel
Tapesearch Logo
Log in
Syntax - Tasty Web Development Treats

React + TypeScript

Syntax - Tasty Web Development Treats

Wes Bos

Tech News, News, Technology

4.91.2K Ratings

🗓️ 2 June 2021

⏱️ 72 minutes

🧾️ Download transcript

Summary

In this episode of Syntax, Scott and Wes talk about using React with Typescript — how to set it up, components, state, props, passing data, custom hooks, and more! Freshbooks - Sponsor Get a 30 day free trial of Freshbooks at freshbooks.com/syntax and put SYNTAX in the “How did you hear about us?” section. Sentry - Sponsor If you want to know what’s happening with your code, track errors and monitor performance with Sentry. Sentry’s Application Monitoring platform helps developers see performance issues, fix errors faster, and optimize their code health. Cut your time on error resolution from hours to minutes. It works with any language and integrates with dozens of other services. Syntax listeners new to Sentry can get two months for free by visiting Sentry.io and using the coupon code TASTYTREAT during sign up. Linode - Sponsor Whether you’re working on a personal project or managing enterprise infrastructure, you deserve simple, affordable, and accessible cloud computing solutions that allow you to take your project to the next level. Simplify your cloud infrastructure with Linode’s Linux virtual machines and develop, deploy, and scale your modern applications faster and easier. Get started on Linode today with a $100 in free credit for listeners of Syntax. You can find all the details at linode.com/syntax. Linode has 11 global data centers and provides 24/7/365 human support with no tiers or hand-offs regardless of your plan size. In addition to shared and dedicated compute instances, you can use your $100 in credit on S3-compatible object storage, Managed Kubernetes, and more. Visit linode.com/syntax and click on the “Create Free Account” button to get started. Show Notes 04:55 - Components Strategies Example: type Props = { value: string; } const App = (props: Props) => Return type? JSX.Element FC or FunctionComponent It’s discouraged for this reason: It means that all components accept children, even if they're not supposed to It could be useful for a return type 12:13 - Props Default props: const defaultJoke: JokeProps = { joke: 'LOL JOE', id: 'YEAH', status: 200, }; function JokeItem({ joke = defaultJoke }: JokeProps): JSX.Element { return ( {joke.joke} = {joke.id} ); } Because props are always destructured, you often have to make a new type for your props. You can’t just type each argument by itself. 18:38 - State Just like Generics, State can be inferred If your type is simple and you’re using useState, it just works: const [user, setUser] = useState(null); 22:27 - useEffect Nothing special required Good use of void: If you want to use a Promise function but not worry about await or .then(), you can pop a void in front of it: useEffect(() => { console.log('Mounted'); // getJoke().then(console.log).catch(console.error); void getJoke(); }, [getJoke]); 26:09 - Refs Very similar to state however some interesting things with null: const ref1 = useRef(null!); “Instantiating the ref with a current value of null but lying to TypeScript that it’s not null.” 29:33 - Custom Hooks This is a great use case for Tuples 31:00 - Context This is probably the most complex thing in this list First define the types Use generic to pass in types OR null This can also be non-null if you have default values in createContext: const AppCtx = React.createContext(null); 35:21 - Events The React events system is better than Vanilla JS Can handle them inline and have it inferred: onClick={e ⇒ yeah(e.target)} const onSetType = (e: React.ChangeEvent) => setType(e.target.value) React has a bunch of events built in — many of them take a Generic argument so you can specify the type of element that triggered it. Handy for native API methods like play and pause. 39:27 - ForwardRef Again use of to pass in forwarded ref type as param 1, Props types as param 2: type Props = { children: React.ReactNode; type: "submit" | "button" }; export type Ref = HTMLButtonElement; export const FancyButton = React.forwardRef((props, ref) => ( {props.children} )); 41:30 - ESLint Typescript-react is good Extend your own Most JS rules apply to TS 46:20 - React as Global React 17 Also add JSX: True to eslint globals for typing things like JSX.Element global.d.ts import * as react from "react" import * as react_dom from "react-dom" declare global { type React = typeof react type ReactDOM = typeof react_dom } 48:08 - TSConfig jsx: "react" React emit .js files with JSX changed Preserve .jsx file output React-native → .js files with jsx not changed 53:05 - Frameworks? Next.js makes this a TREAT Gatsby just use .tsx extensions https://www.gatsbyjs.com/plugins/gatsby-plugin-ts-config/ Links https://github.com/typescript-cheatsheets/react Tanner Linsley https://github.com/wesbos/eslint-config-wesbos Deno Architect https://fav.farm/ Snowpack Vite Parcel 2 Foam ××× SIIIIICK ××× PIIIICKS ××× Scott: Obsidian Wes: Folding Allen Keys Shameless Plugs Scott: 1: Level Up Tutorials Pro Spring Sale - 50% off annual subscriptions! 2: SvelteKit Course Wes: Beginner Javascript Course - Use the coupon code ‘Syntax’ for $10 off! Tweet us your tasty treats! Scott’s Instagram LevelUpTutorials Instagram Wes’ Instagram Wes’ Twitter Wes’ Facebook Scott’s Twitter Make sure to include @SyntaxFM in your tweets

Transcript

Click on a timestamp to play from that location

0:00.0

You're listening to Syntax, the podcast with the tastiest web development treats out there.

0:06.0

Strap yourself in and get ready. Here is Scott Talinsky and West Boss.

0:10.5

Welcome to Syntax, the podcast with the tastiest typescript and react treats.

0:16.6

Hey, today we've got a really good show for you. We've been doing a whole slew of typescript shows

0:22.6

lately and this is the show that is entirely on using React with typescript. So we're going to go

0:29.6

through pretty much everything as to like how do you set it up and how do you do components and

0:34.4

state and props and passing data, custom hooks and yes, Lin and all kinds of good stuff. So

0:40.3

we are sponsored by three awesome companies. First one is FreshBooks Cloud Accounting. Second

0:45.6

one is Century Error, Exception and Performance Tracking and the third one is Lin node. They are

0:52.8

Cloud Computing Developers Trust. They have a Linux server so you can use to run literally anything

0:58.5

you want. That runs on Linux servers. We'll talk about all of them part with through the episode.

1:02.6

How are you doing today, Mr. Scott Talinsky? Hey, I'm doing good. It's a rainy Monday morning

1:09.6

here and I'm just I'm kind of digging the vibe, you know. It's raining here too. Do you ever listen

1:14.7

to the, you know, the low chill O5 beats to study too on YouTube? Do you ever listen to that

1:21.3

YouTube channel? Once or twice, I believe I have, but I know the vibe you're talking about.

1:26.2

Yeah. It's that specific vibe and that's exactly what's going on over here right now and I like

1:31.0

it. Yeah. I could use some of those low five beats right now. Yeah. I've got the windows open

1:36.6

right now and I just love one here in the rain. Yeah. Good stuff. Let's get into it. Type

1:42.6

script and react. Big fan. How about you? I think I'm a pretty big fan. Yeah. I think I'm a

1:48.7

pretty big fan. At least for the most part, I think when I first started getting into it and

1:54.1

first started learning it, I don't know if there was the same level of resources out there.

1:58.7

So some of it was a little bit you'd hit problems and spit. I gotta go to Stack Overflow and

...

Please login to see the full transcript.

Disclaimer: The podcast and artwork embedded on this page are from Wes Bos, and are the property of its owner and not affiliated with or endorsed by Tapesearch.

Generated transcripts are the property of Wes Bos and are distributed freely under the Fair Use doctrine. Transcripts generated by Tapesearch are not guaranteed to be accurate.

Copyright © Tapesearch 2026.