I am trying to generate a unique key for each new notification. Each notification needs a unique key for tracking purposes, i.e. closing the notification. When I attempt to generate a random key, the key is only being generated once and is then used for multiple actions, instead of a unique key per action.
I have tried generating the key within the action creator, as well as within the corresponding epic. If I could generate the key in the action creator, that would be ideal but I'm not sure if that is possible.
// Action generator.
export const postNotification = (notification: NotificationPostProps): DataAction<Notification, POST_NOTIFICATION> => ({
type: POST_NOTIFICATION,
payload: {
...notification,
dismissed: false,
},
});
// Epic listening for password change errors.
const onPasswordChangeError: Epic = action$ =>
action$.pipe(
ofType(error(CHANGE_PASSWORD)),
mapTo(
postNotification({
type: 'error',
message: 'Incorrect password provided.',
// @ts-ignore
key: new moment().unix() + Math.random(), // Attempt at generating a unique key.
}),
),
);
Each notification should have a unique key, however, the logs show a different story.
// First action.
{
type: "app/POST_NOTIFICATION",
payload: {
dismissed: false,
key: 1564243314.8857393, // These should be unique.
message: "Incorrect password provided.",
}
}
// Second action
{
type: "app/POST_NOTIFICATION",
payload: {
dismissed: false,
key: 1564243314.8857393, // These should be unique.
message: "Incorrect password provided.",
}
}