Objects are not valid as a React child

박재훈·2023년 2월 12일
0

React

목록 보기
4/6

Error: Objects are not valid as a React child (found: object with keys {username}). If you meant to render a collection of children, use an array instead.

import React from 'react';

const Greeting = ( props ) => {
  return <h1>{props.username}님 안녕하세요.</h1>;
};

export default Greeting;

위의 코드에서 매개변수로써 username을 쓰고싶다면 아래는 틀린 방법이다.

import React from 'react';

const Greeting = (username) => {
  return <h1>{username}님 안녕하세요.</h1>;
};

export default Greeting;

아래처럼 매개변수인 username을 {}로 감싸줘야한다.

import React from 'react';

const Greeting = ({username}) => {
  return <h1>{username}님 안녕하세요.</h1>;
};

export default Greeting;
profile
신입 개발자

0개의 댓글