schiehll / react-alert

alerts for React
MIT License
608 stars 98 forks source link

Problem when using withAlert() in muliple components #63

Closed guludo closed 6 years ago

guludo commented 6 years ago

Hi, there.

react-alert behaves inconsistently when more than one component using the withAlert() HoC. The following link can be checked to reproduce the problem: https://codesandbox.io/s/w6l7pz9j9w

I'm also posting part of the source here:

import React, { Component, Fragment } from 'react'
import { withAlert } from 'react-alert'

const Foo = ({alert}) => (<button onClick={() => { alert.info('Foo') }}>Foo</button>);
const FooWithAlert = withAlert(Foo);

const Bar = ({ alert }) => (<button onClick={() => { alert.info('Bar') }}>Bar</button>);
const BarWithAlert = withAlert(Bar);

class Home extends Component {
  state = { now: Date.now() }

  render () {
    return (
      <div>
        <FooWithAlert />
        <BarWithAlert />
        <br />
        <button onClick={() => {this.setState({ now: Date.now() })}}>
          Render wrapping component
        </button>
      </div>
    )
  }
}

export default Home

If you use one of the buttons Foo or Bar and then click in Render wrapping component, the previous alert(s) will be shown.

I've fixed the problem and will raise a pull request shortly.

Best regards, Gustavo Sousa

schiehll commented 6 years ago

Fixed in #64