[리액트] JSX 기본 : react (미완)

horiz.d·2021년 12월 16일
0

리액트 꿀단지

목록 보기
4/41

BASIC REACT Code

ReactDOM.render(
  <h1>Hello world!</h1>,
  document.getElementById('root')
);

JSX


CASE 1 : TAG를 가진 JSX

const element = <h1>Hello, world!</h1>;

CASE 2 : {variable} : JSX내의 표현식에 {변수}를 사용

In the example below, we declare a variable called name and then use it inside JSX by wrapping it in { } curly braces

const name = 'Josh Perez';
const element = <h1>Hello, {name}</h1>;

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

You can put any valid JavaScript expression inside { } :the curly braces in JSX. For example, 2 + 2, user.firstName, or formatName(user) are all valid JavaScript expressions.



CASE 3 : 가독성을 위한 코드분할과 (괄호) 사용

We split JSX over multiple lines for readability. While it isn’t required, when doing this, we also recommend wrapping it in ( ) parentheses to avoid the pitfalls(함정) of automatic semicolon insertion.

function formatName(user) {
  return user.firstName + ' ' + user.lastName;
}

const user = {
  firstName: 'Harper',
  lastName: 'Perez'
};

const element = (
  <h1>
    Hello, {formatName(user)}!
  </h1>
);

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

CASE 4 : if & for :

you can use JSX inside of if statements and for loops, assign it to variables, accept it as arguments, and return it from functions:

function getGreeting(user) {
  if (user) {
    return <h1>Hello, {formatName(user)}!</h1>;
  }
  return <h1>Hello, Stranger.</h1>;
}


REF :

React Official Docs - Introducing JSX :
https://reactjs.org/docs/introducing-jsx.html

profile
가용한 시간은 한정적이고, 배울건 넘쳐난다.

0개의 댓글