News for hackers. Stuff that matters.
Re:Statements vs. Expressions by bern4444 on Thursday July 14, 2022 @10:52PM (#32104118)
Something that clicked for me recently in JS and specifically React was why we use {} for interpolation in a component. So an example:

  const MyComponent = () => {
    return (
      <>
        <h1>Hello world</h1> 
        { arr.map(val => <ListItem val={val} />) }
        <h1>Goodbye world</h1> 
      </>
    );
  }
Curly braces often denote expressions. They're everywhere:

In functions:

  const x = () => {

  };
  // or
  function myFunc() { 
  
  };
In conditionals (while the conditionals are statements their bodies are expressions)

  if (x > 2) {
    return true;
  }
In Loops

  while (x > 2) {
    doSomething();
  }
etc.

This is obviously part of the language grammar of many languages and makes it obvious why React uses curly braces as well for interpolating values.

There are no comments on this comment yet.