-
-
Notifications
You must be signed in to change notification settings - Fork 298
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #205 - Use hooks in redux-example
- Loading branch information
1 parent
97fd1c7
commit 38f598c
Showing
4 changed files
with
56 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,56 @@ | ||
/* eslint-disable react/prop-types */ | ||
import React, { Component } from 'react'; | ||
import { bindActionCreators } from 'redux'; | ||
import { connect } from 'react-redux'; | ||
import { withSnackbar } from 'notistack'; | ||
import React from 'react'; | ||
import { useDispatch, useSelector } from 'react-redux'; | ||
import { useSnackbar } from 'notistack'; | ||
import { removeSnackbar } from './redux/actions'; | ||
|
||
class Notifier extends Component { | ||
displayed = []; | ||
|
||
storeDisplayed = (id) => { | ||
this.displayed = [...this.displayed, id]; | ||
}; | ||
let displayed = []; | ||
|
||
const Notifier = () => { | ||
const dispatch = useDispatch(); | ||
const notifications = useSelector(store => store.app.notifications || []); | ||
const { enqueueSnackbar, closeSnackbar } = useSnackbar(); | ||
|
||
removeDisplayed = (id) => { | ||
this.displayed = this.displayed.filter(key => id !== key) | ||
} | ||
const storeDisplayed = (id) => { | ||
displayed = [...displayed, id]; | ||
}; | ||
|
||
componentDidUpdate() { | ||
const { notifications = [] } = this.props; | ||
const removeDisplayed = (id) => { | ||
displayed = [...displayed.filter(key => id !== key)]; | ||
}; | ||
|
||
React.useEffect(() => { | ||
notifications.forEach(({ key, message, options = {}, dismissed = false }) => { | ||
if (dismissed) { | ||
this.props.closeSnackbar(key) | ||
return | ||
// dismiss snackbar using notistack | ||
closeSnackbar(key); | ||
return; | ||
} | ||
// Do nothing if snackbar is already displayed | ||
if (this.displayed.includes(key)) return; | ||
// Display snackbar using notistack | ||
this.props.enqueueSnackbar(message, { | ||
|
||
// do nothing if snackbar is already displayed | ||
if (displayed.includes(key)) return; | ||
|
||
// display snackbar using notistack | ||
enqueueSnackbar(message, { | ||
key, | ||
...options, | ||
onClose: (event, reason, key) => { | ||
onClose: (event, reason, myKey) => { | ||
if (options.onClose) { | ||
options.onClose(event, reason, key); | ||
options.onClose(event, reason, myKey); | ||
} | ||
}, | ||
onExited: (event, key) => { | ||
this.props.removeSnackbar(key); | ||
this.removeDisplayed(key) | ||
} | ||
onExited: (event, myKey) => { | ||
// removen this snackbar from redux store | ||
dispatch(removeSnackbar(myKey)); | ||
removeDisplayed(myKey); | ||
}, | ||
}); | ||
// Keep track of snackbars that we've displayed | ||
this.storeDisplayed(key); | ||
}); | ||
} | ||
|
||
render() { | ||
return null; | ||
} | ||
} | ||
|
||
const mapStateToProps = store => ({ | ||
notifications: store.app.notifications, | ||
}); | ||
// keep track of snackbars that we've displayed | ||
storeDisplayed(key); | ||
}); | ||
}, [notifications, closeSnackbar, enqueueSnackbar, dispatch]); | ||
|
||
const mapDispatchToProps = dispatch => bindActionCreators({ removeSnackbar }, dispatch); | ||
return null; | ||
}; | ||
|
||
export default withSnackbar(connect( | ||
mapStateToProps, | ||
mapDispatchToProps, | ||
)(Notifier)); | ||
export default Notifier; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters