Skip to content

Commit

Permalink
preventDuplicate by comparing keys (if specified) [#190]
Browse files Browse the repository at this point in the history
Check for item key when a key is provided to prevent duplicates
  • Loading branch information
iamhosseindhv authored Nov 4, 2019
2 parents f10c075 + 2779c55 commit 806cd1b
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/SnackbarProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,12 @@ class SnackbarProvider extends Component {
*/
enqueueSnackbar = (message, { key, preventDuplicate, ...options } = {}) => {
if ((preventDuplicate === undefined && this.props.preventDuplicate) || preventDuplicate) {
const inQueue = this.queue.findIndex(item => item.message === message) > -1;
const inView = this.state.snacks.findIndex(item => item.message === message) > -1;
const compareFunction = (item) => (
(key || key === 0) ? item.key === key : item.message === message
)

const inQueue = this.queue.findIndex(compareFunction) > -1;
const inView = this.state.snacks.findIndex(compareFunction) > -1;
if (inQueue || inView) {
return null;
}
Expand Down

0 comments on commit 806cd1b

Please sign in to comment.