4 ms·
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 = ()
by bern4444 4y ago
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.