如何替换react中的字符串



im使用表情符号api,我得到了类似U+1F425的表情符号unicode,以便能够在jsx中显示表情符号。我必须将U+1F425替换为u{1F425}。基本上,我只需要从api中获取1F425

表情包.jsx

import React, { useEffect, useState } from "react";
import Sidebar from "../../Sidebar/Sidebar";
import "./Emoji.css";
const Emoji = ({ isAuth, setIsAuth }) => {
const [type, setType] = useState([]);
const getEmoji = () => {
fetch("https://emojihub.herokuapp.com/api/all/group_animal_bird")
.then((resp) => resp.json())
.then((dat) => (console.log(dat), setType(dat)));
};
useEffect(() => {
getEmoji();
console.log(type);
}, []);
return (
<>
{type.map((emo) => (
<>
<h6>{emo.name}</h6>
<span>{emo.unicode}</span> // This Not
<span>{"u{1F985}"}</span> //This works
</>
))}

</>
);
};
export default Emoji;

谢谢你的帮助!

表情符号只不过是字符。您必须将该代码转换为十六进制,然后将该十六进制代码转换为字符串。
const res = '1F425';
// Convert your string to hex code
const resHex = +`0x${res}`;
// Convert Hex to string
String.fromCodePoint(resHex); // You can render this directly
<span dangerouslySetInnerHTML={{ __html: emo.htmlCode[0] }}></span>

剪切从API获得的表情符号代码应该可以。。。

import React, { useEffect, useState } from "react";
import Sidebar from "../../Sidebar/Sidebar";
import "./Emoji.css";
const Emoji = ({ isAuth, setIsAuth }) => {
const [type, setType] = useState([]);
const getEmoji = () => {
fetch("https://emojihub.herokuapp.com/api/all/group_animal_bird")
.then((resp) => resp.json())
.then((dat) => (console.log(dat), setType(dat)));
};
useEffect(() => {
getEmoji();
console.log(type);
}, []);
return (
<>
{type.map((emo) => (
<>
<h6>{emo.name}</h6>
<span>{"u{" + emo.unicode.slice(2) + "}"}</span> //This should work now.
</>
))}

</>
);
};
export default Emoji;

你可以试试这个。

最新更新