我正在使用表情符號 api,我得到了像這樣的表情符號 unicode U 1F425,以便能夠在 jsx 中顯示表情符號。我必須更換U 1F425為\u{1F425}。基本上我只需1F425要從api中獲取。
表情符號.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;
感謝您的幫助!
uj5u.com熱心網友回復:
表情符號只不過是字符。您必須將該代碼轉換為十六進制,然后將該十六進制代碼轉換為字串。
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
uj5u.com熱心網友回復:
切片從 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;
...你可以試試這個。
uj5u.com熱心網友回復:
<span dangerouslySetInnerHTML={{ __html: emo.htmlCode[0] }}></span>
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/433494.html
標籤:javascript 反应 api 反应钩子 反应打字稿
