Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the significance of "{" "}" braces around this react library code? [closed]

I was going through react library code. After going through I found a special piece of code I am unable to understand its significance. Can someone help?

var validateFormat = function () {};

{
  validateFormat = function (format) {
    if (format === undefined) {
      throw new Error('invariant requires an error message argument');
    }
  };
}

Here why react developer has wrapped the validateFormat into curly braces? is there any significance of doing this.

If I do the following it works the same -

var validateFormat = function () {};

validateFormat = function (format) {
   if (format === undefined) {
   throw new Error('invariant requires an error message argument');
   }
};
like image 716
Abhishek Gangwar Avatar asked Dec 27 '18 08:12

Abhishek Gangwar


People also ask

What are {} used for in react?

You put curly braces when you want to use the value of a variable inside "html" (so inside the render part). It's just a way of telling the app to take the value of the variable and put it there, as opposed to a word.

What's the purpose of using {} curly braces in JSX?

Curly braces { } are special syntax in JSX. It is used to evaluate a JavaScript expression during compilation. A JavaScript expression can be a variable, function, an object, or any code that resolves into a value.

What is the purpose of the double curly braces in react native?

From documentation The exterior set of curly braces are letting JSX know you want a JS expression. The interior set of curly braces represent a JavaScript object, meaning you're passing in a object to the style attribute.

What does double curly braces mean in JavaScript?

Declare a template in the HTML file. Handlebars expressions are put into double curly braces {{expr}} for HTML-escaped content; otherwise, use triple curly brackets {{{expr}}} to avoid HTML-escaping.


2 Answers

The block scope is a result of their babel-preset-fbjs. If you look at the original source you'll find that instead, this function is conditionally defined depending on the value of __DEV__, which is optimized out during transpilation since it is equivalent to process.env.NODE_ENV !== 'production'.

let validateFormat = () => {};

if (__DEV__) {
  validateFormat = function(format) {
    if (format === undefined) {
      throw new Error('invariant requires an error message argument');
    }
  };
}
like image 143
Patrick Roberts Avatar answered Oct 04 '22 21:10

Patrick Roberts


This code makes perfect sense.

var v = function () {};

{
  v = function (format) {
    // actual code
  };
}

The first assignment defines v to an empty placeholder function so that code does not break.

The second assignment contains the actual code of the function. It is inside a block scope, which is a legit JS construct.

But... block scoping does nothing because of variable hoisting, which nullifies any locality contrary to C/C++ adepts' expectations. Many say there is no block scoping in JS, which is false. There is block scoping but it's ineffective (apart from more recent let/const declarations).

So what this code does is abuse the ineffective block syntax to separate visually parts of code.

But (and this is what I think is going on here) what we see here is just an EXAMPLE. I could very well come up with another example that makes perfect sense, such as this:

var v = function () {};

{
  let localValue = 0;

  v = function (format) {
    // actual code using localValue
    localValue = 1;
  };
}

In other words, you may find other examples in the code base which leverages block scoping through let/const and encapsulates the definition as shown. The example you give just doesn't leverage this opportunity but the scoping remains because:

  • it does not disrupt or break code;
  • uniformity;
  • in future may carry more weight adding let/const.

This is all guessing on my part.

like image 25
pid Avatar answered Oct 04 '22 20:10

pid