Skip to content
Go back

map() 함수에서의 문제

Updated:
Edit page

리액트에서 map문법을 이용하여 게시물을 렌더링하는 기능을 구현했는데, console에는 잘 나오지만 실제로는 렌더링이 되지 않는 문제가 발생했다.
원인은 문법에서 나왔다. 해결법은 간단하다.

return(
    posts.map((file,index) =>(
        <Link key = {index} to = {`$file._id`} style={{textAlign: 'left', marginLeft:'80px'}}>
            <h2 style={{color:'#fff}}>{file.title}</h2>
            <h2 style={{color:'#fff' marginLeft:'10px'}}>{moment(file.createAt).format("MMM Do YY")}</h2>
        </Link>
    ))
)

이런식으로 map한수 이내에 return으로 쓰지 않는다면 () 소괄호를 써야하고,

const renderlist = data.map((element, index)=>{
    return(
        ...
    )
})

map 함수안에 return이 있다면 {} 중괄호를 써줘야한다.
-결론-
중괄호로 쓰면 return을 생략할 수 없고,
소괄호로 쓰면 return 생략이 가능하다.


Edit page
Share this post on:

Previous Post
feast 도커 빌드오류 해결법
Next Post
과대적합과 과소적합