본문 바로가기
REACT

react - 익명함수

by Kimjoy 2023. 8. 4.

함수 선언식
function 함수명(매개변수){
 
}
 
함수 표현식 - 익명함수
선언 함수명 = function(){
 
};
 
화살표함수
 선언 함수명 =() => {
 
};
 

import './App.css';

function App(){
  const name = "김뽀삐";
  const code = "02";
  const getNationality = (code) => {
    if(code === "01") {
      return "한국";
    } else if (code === "02") {
      return "미국";
    } else if (code === "03") {
      return "그리스";
    } else {
      return "None";
    }
  };
      return (
        <>
          <div className="react">{name}</div>
          <h1>{name}님의 국적은 {getNationality(code)}이다.</h1>
        </>
      );
   
  }

--화살표 함수로 바꾸기

function App() {
  const code = "03";
  const name = "Tom";
  const getNationality = () => {
    if (code === "01") {
      return "한국";
    } else if(code === "02") {
      return "미국";
    } else if(code === "03") {
      return "그리스";
    } else {
      return "None";
    }
  };
  return <h1>{name}의 국적은 {getNationality()} 이다.</h1>;
}


'REACT' 카테고리의 다른 글

REACT-이벤트  (1) 2023.08.09
REACT - state  (0) 2023.08.08
REACT - props  (0) 2023.08.08
REACT-컴포넌트  (1) 2023.08.08
REACT- REACT 소개 + JSX 문법  (1) 2023.08.08