I'm fairly new and trying to build a simple bookmark application with react & redux.
I can't spin my head around this problem:
A user can create one bookmark and add it to multiple folders. So I dispatch an addMark(bookmark)
action, and after that addMark(folder)
or editFolder(folder)
if the folder already exists. As you can see, bookmark and folder are added via the same action, because in my state tree they are both just marks - distinguished by their type property.
My problem: How can I tell the folder-objects which is the new bookmark to add to folders list of bookmarks? How can I retrieve the ID of the newly created bookmark between the two dispatches?
Solutions I don't find satisfying:
- I know how the bookmark-ID is generated in the reducer (via
Math.max
over the existing bookmark IDs), so I can reproduce the new bookmark ID between the 2 dispatches.This sounds like a bad hack. - Bookmarks and folders are kept in the same state-branch (same reducer), because they are both just "Marks", I could have a state-property that references the latest added bookmark, but this also sounds like a bad hack.
A little bit of source code, to understand what I have:
// mapping between dispatcher and props to my react view
const mapDispatchToProps = (dispatch) => ({
saveMark: (mark) => {
if (mark.id) {
dispatch(editMark(mark));
} else {
dispatch(addMark(mark));
}
},
});
export default connect(mapStateToProps, mapDispatchToProps)(AddMark);
And Inside AddMark, which is the container component:
// save the bookmark first
this.props.saveMark({
type: 'bookmark',
title: this.state.title,
url: this.state.url,
icon: this.props.icon,
style: this.state.style,
});
// now I need the bookmark ID
folders.forEach(folder => {
folder.children.push(bookmarkID) // <-- !!!
});
folders.forEach(folder => this.props.saveMark(folder));
I can't find a satisfying solution for this.