My React Native Journey - 10 - Sharing Wordle success on social media

 Sharing success on social media

In the Wordle game, users post their success by hiding the individual words guessed and only showing the colored squares. This lets other users understand how many tries the poster took to arrive at the correct word.

How is this done?
When the user wins, we send out a message saying he won. In addition, we show a clickable text field, called Share. On press of this text field, we execute a function called shareScore, which will return the colored rows, and also copy the same to clipboard.
For the clipboard, 

import * as Clipboard from "expo-clipboard";


Alert.alert('Hurraayy', 'You won', [{text: 'Share', onPress: shareScore}]);


Function shareScore:

const shareScore = () => {
    const textShare = rows.map(
                                (row,i) => row.map(
                                               (cell, j) => colorsToEmoji[getCellBGColor(i, j)]
                                           ).join('')
                          ).filter((row) => row).join('\n');
    Clipboard.setStringAsync(textShare)
    Alert.alert('Copied Successfully', 'Share your score on your social media')
  }

The function does this: For every i row in rows (6 rows), for every j in cell(5 columns), return the colors of the cells, joined by ''(empty string), to textShare. textShare will now look like:

Array [ "⬛🟧⬛⬛🟧", "🟩🟩🟩🟩🟩", "", "", "", "", ]

Further, filter this output to retain only the rows that have values. The resulting output in textShare
will be:

⬛🟧⬛⬛🟧
🟩🟩🟩🟩🟩

Clipboard.setStringAsync copies this into the clipboard.

You can now go and paste the clipboard contents on your social media feed, or share it with your
friends on any chat app.


Comments

Popular posts from this blog

Reading a pre-populated sqlite database using react native with expo..

React Hooks - useState Vs useRef (An explanation)