schiehll / react-alert

alerts for React
MIT License
607 stars 99 forks source link

How do I trigger an alert as a function? #181

Open sonnyboy27 opened 3 years ago

sonnyboy27 commented 3 years ago

I'm trying to update an old application that I inherited that was using 2.4. Much of the code is triggering the alerts through a callback function after a fetch. These data calls are completely divorced from the page/render context so I'm not sure how to get the reference with the new hooks setup.

besLisbeth commented 3 years ago

Hi @sonnyboy27, did you try to use HOCs as in the example below?

import React from 'react'
import { withAlert } from 'react-alert'

const App = ({ alert }) => (
  <button
    onClick={() => {
      alert.show('Oh look, an alert!')
    }}
  >
    Show Alert
  </button>
)

export default withAlert()(App)