jsx

datajcthemax·2023년 6월 13일

next js

목록 보기
3/10

JSX (JavaScript XML) is a syntax extension for JavaScript that is primarily used with React to describe what the UI should look like. It is similar in appearance to HTML and allows JavaScript to be written within it, but it has some key differences:

  1. JSX produces React "elements". You can think of these elements as instructions for how the UI should be updated.

  2. JSX allows JavaScript expressions to be embedded. This is done by wrapping the expression in curly braces {}.

  3. JSX also has a different syntax for attributes/props. In HTML, attributes are used to define characteristics of an element. In JSX, these are referred to as "props". For example, in HTML, you might define a class attribute as class="myClass", but in JSX it would be className="myClass".

  4. JSX prevents injection attacks. It is safe to embed user input in JSX because React DOM escapes any values embedded in JSX before rendering them.

Here's an example of what JSX might look like:

function Welcome(props) {
  return <h1>Hello, {props.name}</h1>;
}

const element = <Welcome name="Sara" />;

ReactDOM.render(
  element,
  document.getElementById('root')
);

In this example, Welcome is a React component defined using JSX. The <Welcome name="Sara" /> syntax is transformed at build time to React.createElement('Welcome', {name: 'Sara'}).

The ReactDOM.render call then instructs the ReactDOM library to render this component into the <div> with id='root' in the HTML document. The {props.name} inside the <h1> tag is a JavaScript expression, indicating where props.name should be inserted into the output.

0개의 댓글