ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [movie_app_5] stateless functional components
    프로젝트/[react]movie_app 2018. 4. 3. 17:32

    모든 컴포넌트에 state가 있는게 아니다!  


    <stateless functional components (dumb component)>

    -반대로, state가 있는 컴포넌트는 smart component 라고 함

     

     smart component

    dumb component 

     

    state 

    props 

     

    class 

    functional 

     render(), lifecycle







    <smart component를 stateless component로 바꾸기>



    class MoviePoster extends Component{
    render(){
    return(
    <img src={this.props.poster} alt="Movie Poster"></img>
    )
    }
    }



    function MoviePoster({poster}){
    return (
    <img src={poster} alt="Movie Poster"></img>
    )
    }


    1. class -> funtion
    2. render()가 사라짐
    3. props로 받아옴



    <propTypes>

    MoviePoster.propTypes = {
    poster: PropTypes.string.isRequired
    }








    <Movie 컴포넌트도 바꿔보자.>


    class Movie extends Component{
    static propTypes = {
    poster: PropTypes.string
    }
    render(){
    console.log(this.props)
    return (
    <div>
    <MoviePoster poster={this.props.poster}/>
    <h1> {this.props.title} </h1>
    </div>
    )
    }
    }



    function Movie({poster,title}){
    return(
    <div>
    <MoviePoster poster={poster}/> //클래스가 아니니까 this.props는 삭제
    <h1> {title} </h1>
    </div>
    )
    }

    Movie.propTypes = {
    title: PropTypes.string.isRequired,
    poster: PropTypes.string.isRequired
    }


    '프로젝트 > [react]movie_app' 카테고리의 다른 글

    [movie_app_7] css  (0) 2018.04.04
    [movie_app_6] ajax  (0) 2018.04.04
    [movie_app_4] loading state  (0) 2018.04.03
    [movie_app_3] 컴포넌트 Lifecycle과 state  (0) 2018.04.02
    [movie_app_2] 컴포넌트, props  (0) 2018.04.02

    댓글

Designed by Tistory.