Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

stop playing music when page is closed react

import React, { useRef, useEffect } from 'react';
import waves from '../audio/waves.mp3';
    
const RockyCoast = (props) => {
    // the audio variable needs to be stored in a ref in order to access it across renders
    let audio = useRef();
    // start the audio (using the .current property of the ref we just created) when the component mounts using the useEffect hook
    useEffect(() => {
        audio.current = new Audio(waves)
        audio.current.play()
    }, [])
    // Stop the audio when the component unmounts
    // (not exactly what you asked re React Router, but similar idea)
    useEffect(() => {
        return () => {
            audio.current.pause()
            console.log("in cleanup")
        }
    }, [])
    ...

    return (
        <>
            ...
        </>
    )
}
export default RockyCoast;

Comment

PREVIOUS NEXT
Code Example
Javascript :: passar a página atual na url js net core 
Javascript :: convert python to js online 
Javascript :: javaScipt diference != and !== 
Javascript :: javascript zoom to meters 
Javascript :: how to edit local json files using node 
Javascript :: selectize get instance id from onchange 
Javascript :: native base change track color 
Javascript :: javascript only works in codepen 
Javascript :: javascript add character to string at position 
Javascript :: setstate to false after 10 sec react native 
Javascript :: vanilla js game loop 
Javascript :: 4.6.3. Order of Operations¶ 
Javascript :: how to get button text in javascript 
Javascript :: filter object javascript es6 
Javascript :: fecha javascript mes de 2 digitos 
Javascript :: masterselect angular 8 
Javascript :: append dynamica html in jsx react 
Javascript :: lity popup 
Javascript :: react-image-lightbox npm 
Javascript :: vue apollo refetch every x ms 
Javascript :: https://stackoverflow.com/questions/51115640/how-to-send-form-data-from-react-to-express/51116082 
Javascript :: undefined is not an object render header flatlist react hook 
Javascript :: alegato termino excluido 
Javascript :: discord.js change role permissions 
Javascript :: find date range btween start date to end date in node js 
Javascript :: mac address validation regex angular 
Javascript :: suisie with c 
Javascript :: Entendendo Package Json e instalando o Express 
Javascript :: how to use cookiestore javascript console 
Javascript :: document get all elements by property has white color 
ADD CONTENT
Topic
Content
Source link
Name
2+2 =