如何替换反应中的字符串

发布于 2025-01-10 14:17:14 字数 1065 浏览 2 评论 0原文

我正在使用表情符号 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

饮惑 2025-01-17 14:17:14
  • 您可以使用函数替换接收到的数据中的格式,然后再将其渲染到 jsx 中。

  • 根据您的情况,您可以尝试拆分字符串,将十六进制部分转换为 unicode 点,然后以所需的格式重新构建。

  • 在midify代码中,convertUnicode函数接受Unicode字符串(U+1F425)并以(\u{iF425}格式返回。然后在getEmoji函数中,每个emo对象被映射以用Unicode字符替换unicode属性。

    import React, { useEffect, useState } from "react";
      从“../../Sidebar/Sidebar”导入侧边栏;
      导入“./Emoji.css”;
    
     const Emoji = ({ isAuth, setIsAuth }) =>; {
       const [类型,setType] = useState([]);
    
       const ConvertUnicode = (unicode) =>; {
         // 分割Unicode字符串并获取十六进制部分
         const hexString = unicode.split("U+")[1];
         // 将十六进制转换为十进制
         const DecimalCode = parseInt(hexString, 16);
         // 将十进制代码转换为 Unicode 字符
         返回 String.fromCodePoint(decimalCode);
       };
    
       const getEmoji = () =>; {
         fetch("https://emojihub.herokuapp.com/api/all/group_animal_bird")
           .then((resp) => resp.json())
           .then((dat) => setType(dat.map(emo => ({ ...emo, unicode:convertUnicode(emo.unicode) }))));
       };
    
       useEffect(() => {
         获取表情符号();
       }, []);
    
       返回 (
         <>
           {type.map((emo, 索引) => (
             
    {emo.name}; {/* 渲染转换后的 Unicode */} {emo.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.

    import React, { useEffect, useState } from "react";
      import Sidebar from "../../Sidebar/Sidebar";
      import "./Emoji.css";
    
     const Emoji = ({ isAuth, setIsAuth }) => {
       const [type, setType] = useState([]);
    
       const convertUnicode = (unicode) => {
         // Split the Unicode string and get the hexadecimal part
         const hexString = unicode.split("U+")[1];
         // Convert hexadecimal to decimal
         const decimalCode = parseInt(hexString, 16);
         // Convert decimal code to Unicode character
         return String.fromCodePoint(decimalCode);
       };
    
       const getEmoji = () => {
         fetch("https://emojihub.herokuapp.com/api/all/group_animal_bird")
           .then((resp) => resp.json())
           .then((dat) => setType(dat.map(emo => ({ ...emo, unicode: convertUnicode(emo.unicode) }))));
       };
    
       useEffect(() => {
         getEmoji();
       }, []);
    
       return (
         <>
           {type.map((emo, index) => (
             <div key={index}>
               <h6>{emo.name}</h6>
               {/* Render the converted Unicode */}
               <span>{emo.unicode}</span>
             </div>
           ))}
         </>
       );
     };
    
     export default Emoji;
    
秋千易 2025-01-17 14:17:14

表情符号只不过是字符。
您必须将该代码转换为十六进制,然后将该十六进制代码转换为字符串。

 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

Emojis are nothing but characters.
You have to convert that code to hex and then convert that hex code to string.

 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
七色彩虹 2025-01-17 14:17:14

简单的一行答案

function convertUnicode(unicode) {
    // split by +, use 2nd element and add \u infront with {}
    return `\u{${unicode.split("+")[1]}}`
}

Simple one line answer

function convertUnicode(unicode) {
    // split by +, use 2nd element and add \u infront with {}
    return `\u{${unicode.split("+")[1]}}`
}
风向决定发型 2025-01-17 14:17:14
<span dangerouslySetInnerHTML={{ __html: emo.htmlCode[0] }}></span>
<span dangerouslySetInnerHTML={{ __html: emo.htmlCode[0] }}></span>
情场扛把子 2025-01-17 14:17:14

切片从 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;

你可以尝试一下。

Slicing the emoji code got from API should work...

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;

...you may try this out.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文