Study/React

Variable Attributes in JSX

더 멋진 세상을 꿈꾸는 개발자 2020. 5. 25. 18:57

Variable Attributes in JSX

When writing JSX, it’s common to use variables to set attributes.

Here’s an example of how that might work:

 

// Use a variable to set the `height` and `width` attributes:

const sideLength = "200px";

const panda = (
  <img 
    src="images/panda.jpg" 
    alt="panda" 
    height={sideLength} 
    width={sideLength} />
);

 

Notice how in this example, the <img />‘s attributes each get their own line. This can make your code more readable if you have a lot of attributes on one element.

Object properties are also often used to set attributes:

 

const pics = {
  panda: "http://bit.ly/1Tqltv5",
  owl: "http://bit.ly/1XGtkM3",
  owlCat: "http://bit.ly/1Upbczi"
}; 

const panda = (
  <img 
    src={pics.panda} 
    alt="Lazy Panda" />
);

const owl = (
  <img 
    src={pics.owl} 
    alt="Unimpressed Owl" />
);

const owlCat = (
  <img 
    src={pics.owlCat} 
    alt="Ghastly Abomination" />
); 

JSX의 변수 attributes

JSX를 작성할때, 속성을 설정하는 변수를 사용하는 것은 일반적이다. 

여기 예시가 있다. 

 

// Use a variable to set the `height` and `width` attributes:

const sideLength = "200px";

const panda = (
  <img 
    src="images/panda.jpg" 
    alt="panda" 
    height={sideLength} 
    width={sideLength} />
);

 

Notice how in this example, the <img />‘s attributes each get their own line.

This can make your code more readable if you have a lot of attributes on one element.

Object properties are also often used to set attributes:

 

 

이 예에서 <img />의 속성이 각각 어떻게 자신만의 선을 갖게 되는지 주목하라.

만약 당신이 한 요소에 많은 속성을 가지고 있다면, 이것은 당신의 코드를 더 가독성좋게 만든다. 

객체 propertie들은 속성을 설정하는데 자주 사용된다. 

const pics = {
  panda: "http://bit.ly/1Tqltv5",
  owl: "http://bit.ly/1XGtkM3",
  owlCat: "http://bit.ly/1Upbczi"
}; 

const panda = (
  <img 
    src={pics.panda} 
    alt="Lazy Panda" />
);

const owl = (
  <img 
    src={pics.owl} 
    alt="Unimpressed Owl" />
);

const owlCat = (
  <img 
    src={pics.owlCat} 
    alt="Ghastly Abomination" />
); 

https://www.codecademy.com/courses/react-101/lessons/react-jsx-advanced/exercises/jsx-event-listeners