tjallingt / react-youtube

react.js powered YouTube player component
http://tjallingt.github.io/react-youtube/
MIT License
1.86k stars 222 forks source link

TypeError: Cannot read properties of null (reading 'playVideo') #392

Open vicckuo opened 1 year ago

vicckuo commented 1 year ago

Unhandled Runtime Error TypeError: Cannot read properties of null (reading 'playVideo')

Call Stack eval node_modules/react-youtube/node_modules/youtube-player/dist/index.js (65:0) new Promise

exports.default node_modules/react-youtube/node_modules/youtube-player/dist/index.js (64:0) createPlayer node_modules/react-youtube/dist/YouTube.esm.js (170:41) [https://upload.cc/i1/2023/02/26/daWAmu.png](url) This error may occur when I click pagination to get the video or just load the page,I show three videos on page, and change the page when want to get more. Sorry for the English not my native language. React 18.2.0 Next 13.1.6 ``` const Playlist = ({ playlistId }) => { const [videos, setVideos] = useState([]); const [currentPage, setCurrentPage] = useState(0); const isMobile = useMediaQuery({ maxWidth: 640 }); const isPad = useMediaQuery({ maxWidth: 1180 }); const videosPerPage = isMobile ? 1 : isPad ? 2 : 3; useEffect(() => { const fetchData = async () => { try { const response = await fetch( `https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=50&playlistId=${playlistId}&key=${process.env.NEXT_PUBLIC_ytAPI}` ); const data = await response.json(); setVideos(data.items); console.log('data: ', data); } catch (error) { console.log(error); } }; fetchData(); }, [playlistId]); const handlePageClick = ({ selected }) => { setCurrentPage(selected); }; const currentVideos = useMemo(() => { const offset = currentPage * videosPerPage; return videos.slice(offset, offset + videosPerPage); }, [currentPage, videos, videosPerPage]); const getOpts = useCallback(() => { return { playerVars: { autoplay: 0, }, }; }, []); const opts = getOpts(); return ( <>
{currentVideos.map((video) => (

{video.snippet.title}

))}
); }; export default Playlist; ```
Dzhuneyt commented 1 year ago

I have seen the same error.

dcsan commented 6 months ago

same as #369 critical crash bug