Last active
September 9, 2019 19:08
-
-
Save adamhenson/9087c5f3cdb0a5eba1a8d36e34567dd1 to your computer and use it in GitHub Desktop.
Example useMemo Usage
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
import React, { useMemo } from 'react'; | |
import EmojiImages from './EmojiImages'; | |
import emojiList from './emojis.json'; | |
import styles from './Emojis.module.css'; | |
// all de-duped emojis | |
const getAllEmojis = emojis => ({ | |
id: Date.now(), | |
emojis: Array.from(new Set(Object.values(emojis))) | |
}); | |
// returns a de-duped array of emoji image urls that include a string | |
// in the name. a case insensitive check. | |
const getEmojisByName = ({ emojis, name }) => ({ | |
id: Date.now(), | |
emojis: Array.from( | |
new Set( | |
Object.keys(emojis).reduce( | |
(accumulator, current) => [ | |
...accumulator, | |
...(!current.toLowerCase().includes(name.toLowerCase()) | |
? [] | |
: [emojis[current]]) | |
], | |
[] | |
) | |
) | |
) | |
}); | |
export default ({ emojiSearchName }) => { | |
// we use all emojis if none are found from the search | |
const allEmojis = useMemo(() => getAllEmojis(emojiList.data), []); | |
// if the search is empty show all emojis | |
const emojisByName = !emojiSearchName | |
? allEmojis | |
: getEmojisByName({ | |
emojis: emojiList.data, | |
name: emojiSearchName | |
}); | |
return ( | |
<div className={styles.root}> | |
<EmojiImages | |
// if search results are empty (no matches) - show all emojis | |
list={!emojisByName.emojis.length ? allEmojis : emojisByName} | |
/> | |
</div> | |
); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment