#TIL: React's JSX Default Value

    Today I learned about React JSX's default value.

    Consider the Button component below:

    import React from 'react';
    import Loader from '../Loader';
    
    function Button ({  loading }) {
     return <button>{ loading ? <Loader /> : 'Click me!'}<button/>
    }

    A huge aha moment moments ago! When you supply props without a value, it will default to true.

    So in a nutshell, these two JSX expressions below are equivalent:

    <Button loading={true} />
    
    <Button loading />

    And so, if you use make use of JavaScript coercion nature. These two JSX expressions are also equivalent.

    <Button loading={false}/>
    
    <Button />

    Plus tip! If you have type-checking in your React project, this could help.

    type TButtonProps = {
      loading?: Boolean
    }
    // So if you do this the type checking won't complain
    <Button />

    More articles

    Why AI Makes Us Lazy (And How to Fight It)

    The framework is simple. Following it is hard. Not because it's complicated — because we're human. Here are the temptations, the warning signs, and how to build the discipline.

    Read more

    The Responsible AI Development Framework

    Six phases for workflow. Five practices for thinking. A systematic approach to using AI that amplifies your judgment instead of replacing it.

    Read more

    Let's talk about your project

    Ready to bring your ideas to life? Whether you need a consultation, want to discuss a project, or just want to say hello.