logo
episode-header-image
About this episode

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) => <div />  
  • 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?

    Links

    ××× SIIIIICK ××× PIIIICKS ×××

    Shameless Plugs

    Tweet us your tasty treats!

    Up next
    Jul 9
    918: Extreme Native Perf on the Web with Superhuman
    Wes and Scott talk with Loïc Houssier about how Superhuman builds lightning-fast, delightfully-designed email software. They dig into engineering philosophy, offline-first architecture, local databases, AI-powered productivity, and what it takes to create tools that people love. ... Show More
    48m 54s
    Jul 7
    917: AI Tools You Should Know
    Scott and Wes round up the hottest AI tools you should have on your radar; from text-to-speech wizards to self-hosted image generators. They break down what they’re using, what’s worth paying for, and which tools are changing their workflows. Show Notes 00:00 Welcome to Syntax! 0 ... Show More
    37m 48s
    Jul 2
    916: I got fired, what should I focus on?
    In this potluck episode of Syntax, Wes and Scott answer your questions about maintaining popular open-source projects, where to start after a layoff, impostor syndrome, Scott’s recording setup, whether a computer science degree is still worth it in the age of AI, and more! Show N ... Show More
    58m 24s
    Recommended Episodes
    Feb 2022
    Is functional programming the hipster programming paradigm?
    Here’s a useful primer on functional programming with JavaScript.This tutorial will guide you in exploring the fundamentals of functional programming with React.If you’re looking for more info on functional programming in React, we’d like to tell you why hooks are the best thing ... Show More
    27m 49s
    Sep 2022
    The spicy React debate show 🌶️
    We’re back with another spicy YepNope debate! This time, Amelia and KBall are arguing that there’s real value to (continue) using React in 2022, while Amal and special guest (and author of the post which stemmed the whole debate) Josh Collinsworth argue that React’s time leading ... Show More
    1h 3m
    Jun 2022
    ESLint and TypeScript
    Josh Goldberg joins Nick, Chris & a very nasally-sounding KBall for a fun conversation around TypeScript ESLint. They discuss why we need ESLint when we have TypeScript, some useful rules in typescript-eslint, how it works, and a few hot takes along the way! Discuss on Changelog ... Show More
    1h 3m
    Feb 2024
    Angular Signals
    KBall & Amal interview Alex & Pavel from the Angular Signals team. They cover the history, how the Angular team decided to move to signals, what the new mental model looks like, migration path & even dive into community integrations and future roadmap. Leave us a comment Changelo ... Show More
    1h 6m
    Feb 2024
    Stately with Laura Kalbag
    Stately is a web-based drag and drop editor for collaboratively developing code, diagrams, and documentation. Laura Kalbag is the Developer Advocate at Stately and she joins the show today to talk about Stately, state machines, building good documentation, and more. Josh Goldberg ... Show More
    1h 6m
    Mar 2024
    Netlify and Edge Computing with Erica Pisani
    Netlify is a popular hosting platform that provides build, deploy, and serverless backend services for web apps. The platform enables deployment directly from source files stored in a version control system like GitHub. Erica Pisani is a Senior Software Engineer at Netlify. She j ... Show More
    42m 16s
    Feb 2024
    Angular moves fast without breaking things
    KBall & Amal dive deep with the “Dazzle of Zebras” (possible future band name), Angular team members Jessica Janiuk & Mark “Techson” Thompson. Along with an absolute riot of puns, they cover topics such as Angular’s new deferrable views feature, how the Angular core team handles ... Show More
    1h 11m