如何替换反应中的字符串
我正在使用表情符号 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;
I'm using emoji API and I get emoji unicode's like this U+1F425
to be able to show emoji's in jsx. I have to replace U+1F425
to \u{1F425}
; basically I just need to get 1F425
from the API.
Emoji.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;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以使用函数替换接收到的数据中的格式,然后再将其渲染到 jsx 中。
根据您的情况,您可以尝试拆分字符串,将十六进制部分转换为 unicode 点,然后以所需的格式重新构建。
在midify代码中,convertUnicode函数接受Unicode字符串(U+1F425)并以(\u{iF425}格式返回。然后在getEmoji函数中,每个emo对象被映射以用Unicode字符替换unicode属性。
you can use function to replace the format in the received data before rendering it in jsx.
in your case, you can try to splitting the string,converting hexadecimal part to unicode point and then recontructing in the desired format.
In midify code, convertUnicode function that takes Unicode string(U+1F425) and return in (\u{iF425} format. Then in the getEmoji function each emo object is mapped to replace in unicode propertywith Unicode character.
表情符号只不过是字符。
您必须将该代码转换为十六进制,然后将该十六进制代码转换为字符串。
Emojis are nothing but characters.
You have to convert that code to hex and then convert that hex code to string.
简单的一行答案
Simple one line answer
切片从 API 获得的表情符号代码应该可以工作......
你可以尝试一下。
Slicing the emoji code got from API should work...
...you may try this out.