react-use#useAudio TypeScript Examples

The following examples show how to use react-use#useAudio. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: App.tsx    From Intro_to_React with GNU General Public License v2.0 5 votes vote down vote up
App = () => {
  const [songInfo, setSong] = useState<song>(null)
  const [audio, state, controls, ref] = useAudio({ src: songInfo?.url ?? "", autoPlay: false })


  const history = useHistory()
  useEffect(() => {
    history.push("/search")
  }, [history])

  useEffect(() => {
    if (songInfo?.needToPlay) {
      controls.play()
      setSong({ ...songInfo, needToPlay: false })
    }
  }, [songInfo, controls])
  return (
    <section className={styles.container}>
      <Nav />
      {audio}
      <main className={styles.main}>
        <Header />

        <Switch>
          <Route path="/about">
            <div>About!</div>
          </Route>

          <Route path="/me">
            <div>Me! :D</div>
          </Route>

          <Route path="/search">
            <Search songInfo={songInfo} setSong={setSong} state={state} controls={controls} />
          </Route>
        </Switch>
      </main>
      <Player songInfo={songInfo} state={state} controls={controls} audio={ref.current} />
    </section>
  )
}
Example #2
Source File: App.tsx    From Intro_to_React with GNU General Public License v2.0 5 votes vote down vote up
App = () => {
  const [songInfo, setSong] = useState<song>(null)
  const [audio, state, controls, ref] = useAudio({ src: songInfo?.url ?? "", autoPlay: false })

  const history = useHistory()
  useEffect(() => {
    history.push("/search")
  }, [history])

  useEffect(() => {
    if (songInfo?.needToPlay) {
      controls.play()
      setSong({ ...songInfo, needToPlay: false })
    }
  }, [songInfo, controls])
  return (
    <section className={styles.container}>
      <Nav />
      {audio}
      <main className={styles.main}>
        <Header />

        <Switch>
          <Route path="/about">
            <div>About!</div>
          </Route>

          <Route path="/me">
            <div>Me! :D</div>
          </Route>

          <Route path="/search">
            <Search songInfo={songInfo} setSong={setSong} state={state} controls={controls} />
          </Route>
        </Switch>
      </main>
      <Player songInfo={songInfo} state={state} controls={controls} audio={ref.current} />
    </section>
  )
}