Whenever I try to display some data fetched in my React Native project from my API endpoint I get this JSON parse error: JSON parse error on line 0 near .."}], [18933, "RC': expected another Unicode escape for the second half of surrogate pair
Clearly, there is a missing escape character for Unicode escape symbol.
My string is "You can try, but I donβt think so ππ"
I tried printing the string in the console and it displays fine, but when I try to display it in React Native's <Text></Text>
component my app crashes.
Any guidance would be appreciated.
UPDATE
I found that the problem was with the JavaScript String.substring()
method. It does not know how to properly split unicode surrogate pairs and therefore will try to split the string in the middle of a pair if you're unlucky with the positioning.
I solved the problem by using a library called "runes" that properly splits and takes a substring of a string with unicode surrogate pairs honored.
Problem:
postComment.substring(0, 35)
Solution: runes.substr(postComment, 0, 35)
Simply install the runes
library and use runes.substr(yourString, start, end)
to handle unicode surrogate pairs in your string :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With