Janez Kuhar
Janez Kuhar

Reputation: 4286

React element name as a string variable

While skimming through source code of one of the React UI libraries, I've come across this code pattern (I simplify):

function Test() {
  let Button = "button";
  // ...
  return <Button>Click me</Button>;
}

What's going on here - why does this work?:)

Upvotes: 1

Views: 490

Answers (1)

Janez Kuhar
Janez Kuhar

Reputation: 4286

The JSX above is interpreted by React as:

function Test() {
  let Button = "button";
  return React.createElement(
    Button,
    null,
    "Click me"
  );
}

Button is just a string variable, set to "button", that gets passed to React.createElement(...).

Upvotes: 2

Related Questions