Last active
July 26, 2019 04:29
-
-
Save TSMMark/456a6268b81a6bba091022360ed1c8e5 to your computer and use it in GitHub Desktop.
useSelectedItems
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
// Default use: | |
const [selectedMedias, addId, removeId] = useSelectedItems(medias) | |
// Example of how to select an item | |
map(medias, ({ id, title }) => <a onClick={() => addId(id)}>{ id } { title }</a>) | |
// Example of how to deselect an item | |
map(selectedMedias, ({ id, title }) => <a onClick={() => removeId(id)}>{ id } { title } is selected!</a>) | |
// Optional Arguments: | |
// Optional 4th return if you need a list of all selected ids | |
const [selectedMedias, addId, removeId, allSelectedIds /* (optional) */] = useSelectedItems(medias) | |
// Optional 2nd arg to decide if the item is selected, if different from { id }. Default if not provided: `(ids, { id }) => ids.include(id)` | |
const [selectedMedias, addId, removeId] = useSelectedItems(medias, (ids, { someForeignKey }) => ids.include(someForeignKey)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Would allow us to replace stuff like this:
https://github.com/Vydia/d2/blob/0ddf2b68a511c55a58e75770245b6a2d4cfb99ee/src/screens/Publish/AdditionalServices/template.js#L48