React

 

 

훅 만들기

src/hooks/useOnClickOutside.js

import { useEffect } from 'react'

const useOnClickOutside = (ref, handler) => {

    useEffect(() => {
       const listener=(event)=>{
            if(!ref.current || ref.current.contains(event.target)){
                console.log('outside click');
                return;
            }
            
            handler();
       };

       document.addEventListener('mousedown', listener);
       document.addEventListener('touchstart', listener);

        return()=>{
            document.removeEventListener('mousedown', listener);
            document.removeEventListener('touchstart', listener);
        }
    }, [ref, handler]);

}


export default useOnClickOutside;

 

 

modal.

index.js

import React, { useEffect, useRef } from 'react'
import './MovieModal.css';
import useOnClickOutside from '../../hooks/useOnClickOutside';

const MovieModal = ({
        adult,
        backdrop_path,
        genre_ids,
        id,
        original_language,
        original_title,
        overview,
        popularity,
        poster_path,
        release_date,
        title,
        video,
        vote_average,        
        setModalOpen
    }) => {

  const ref=useRef();

  useOnClickOutside(ref, ()=>{setModalOpen(false)});


  return (
    <div className='presentation'  role="presentation">
        <div className='wrapper-modal'>
          <div className='modal' ref={ref} >

            <span onClick={()=>setModalOpen(false)} className='modal-close'>x</span>
        

        <img className='modal__poster-img'  src={`https://image.tmdb.org/t/p/original/${backdrop_path}`}  alt="modal-img"  />

        <div className='modal__content'>
            <p className='modal_details'>
                <span className='modal__user_perc'>100% for you</span>{" "}
                {release_date}
            </p>

            <h2 className='modal__title'>{title ? title : original_title }</h2>
            <p className='modal__overview'>평점 : {vote_average}</p>
            <p className='modal__overview'>{overview}</p>
        </div>
       
       
        </div>
        </div>    
    </div>
  )
}

export default MovieModal

 

 

 

https://github.com/braverokmc79/react-disney-plus-app

 

 

 

 

리액트

 

about author

PHRASE

Level 60  라이트

군자의 말은 극히 비근하지만 그 중에 참된 도(道)가 있다. 불하대(不下帶)는 눈앞에 보이는 것의 비유. 옛 사람들은 사람을 대할 때에 띠(帶) 아래에는 눈을 주지 않았다. -맹자

댓글 ( 0)

댓글 남기기

작성