简体   繁体   English

如何在 React App 中显示 Emoji

[英]How to display Emoji in React App

I would like to display emojis on my webpage in a react chat app.我想在反应聊天应用程序中的网页上显示表情符号。 The plan is to give the user a list of emojis to select from.该计划是为用户提供一个到 select 的表情符号列表。 How do I use the codes like '1F683' and have them display the emoji on the page?如何使用“1F683”之类的代码并让它们在页面上显示表情符号? I need to support Chrome.我需要支持 Chrome。

I am able to use css to show the emoji.我可以使用 css 来显示表情符号。

<div className="smiley"></div>

.smiley:after {
    content: "\01F683";
}

I can also have a list of images and map them to the code and display an img element per emoji.我还可以将图像列表和 map 放到代码中,并为每个表情符号显示一个 img 元素。

Is there another way and which is the best way to do this?还有另一种方法,哪种方法是最好的方法?

I am maybe late to the party but I needed to conditionally render different emojis by the same component, so for me the easiest way was:我可能迟到了,但我需要通过同一个组件有条件地呈现不同的表情符号,所以对我来说,最简单的方法是:

  1. Go to https://unicode.org/emoji/charts/full-emoji-list.html转到https://unicode.org/emoji/charts/full-emoji-list.html
  2. Find needed emojis, for example U+1F609 and use it as a string of a hex number 0x1F609 with String.fromCodePoint in your code — see below.找到需要的表情符号,例如U+1F609并将其用作十六进制数字0x1F609的字符串,并在您的代码中使用String.fromCodePoint — 见下文。
  3. Create one little component to satisfy eslint rule which would otherwise throw an error Emojis should be wrapped in <span>, have role="img", and have an accessible description with aria-label or aria-labelledby jsx-a11y/accessible-emoji :创建一个小组件来满足 eslint 规则,否则会抛出错误Emojis should be wrapped in <span>, have role="img", and have an accessible description with aria-label or aria-labelledby jsx-a11y/accessible-emoji
const Emoji = React.memo(({ className, label, symbol }) =>
    <span className={className} role="img" aria-label={label}>
        {String.fromCodePoint(symbol)}
    </span>)

export default Emoji

So then somewhere else you can use it as:那么在其他地方你可以将它用作:

class MagnificentComponent extends PureComponent {
    getEmojiConditionally = () => this.props.happy ? '0x1F60A' : '0x1F61E'

    render = () =>
        <SomeComponentWhereINeedEmoji>
            <Emoji symbol={this.getEmojiConditionally(} />
        </SomeComponentWhereINeedEmoji>
}

All emojis are pretty much standardized with the format at Emoji Cheat Sheet , so your given example above ( \\01F683 ) maps to railway_car in the Emoji Cheat Sheet.所有的表情符号,都几乎与格式标准化的表情小抄,因此以上(你给出的例子\\01F683 )映射到railway_car的表情小抄。

It might be a better idea to store your emojis with these identifiers and map it to the actual emojis later on, without worrying about encoding the actual emoji (🚃) themselves, or not being able to tell/remember the actual emoji represented by the Unicode sequence ( \\01F683 ).使用这些标识符存储您的表情符号并稍后将其映射到实际表情符号可能是一个更好的主意,而不必担心自己对实际表情符号 (🚃) 进行编码,或者无法分辨/记住由 Unicode 表示的实际表情符号序列( \\01F683 )。

If you wish to map this human-readable identifier to the actual Unicode sequence/symbol itself, you have a few options, using railway_car as an example:如果您希望将此人类可读的标识符映射到实际的 Unicode 序列/符号本身,您有几个选择,以railway_car为例:

Twemoji Awesome Twemoji 真棒

Twemoji Awesome is like Font Awesome, but with Twitter Emojis. Twemoji Awesome就像 Font Awesome,但带有 Twitter Emojis。

You can then insert an emoji like this, just like Font Awesome.然后,您可以像 Font Awesome 一样插入这样的表情符号。

<i class="twa twa-railway-car"></i>

This will output the corresponding SVG:这将输出相应的 SVG:

Emoji Dictionary表情词典

There's an npm package aptly named emoji-dictionary that allows you to map the emoji name to the Unicode character, if you wish to use default the browser's default emoji renderer.如果您希望使用默认浏览器的默认表情符号渲染器,有一个名为emoji-dictionary的 npm 包,它允许您将表情符号名称映射到 Unicode 字符。

The usage will then look like this:用法将如下所示:

emoji.getUnicode("railway_car");

This returns 🚃 (which would display on modern browsers/might break on older browsers/etc).这将返回 🚃(它会在现代浏览器上显示/可能会在旧浏览器上中断/等)。

We have the unicode of emojis in W3C .我们在W3C 中有 emojis 的 unicode。 It is in the range of {.它在{的范围内。 Hex 2600 - 26FF .}.十六进制2600 - 26FF .}。

Thus, you can generate it without CSS.因此,您可以在没有 CSS 的情况下生成它。

Check example below 👇🏼检查下面的例子👇🏼

 class Emoji extends React.Component { render() { const {title, code} = this.props; return <span className="emoji" title={title}>{code}</span> } } class App extends React.Component { renderEmojis() { const rangeEmojis = Array.from({length: 256}, (v, k) => (k + 9728).toString(16)); return rangeEmojis.map((code, index) => <Emoji code={unescape ('%u' + code)} title={"My code is :"+code} key={'emj'+index} />) } render() { return ( <div> {this.renderEmojis()} </div> ) } } ReactDOM.render(<App />, document.querySelector('#chat'))
 .emoji { border: solid 1px #3e3e3e; width: 50px; height: 50px; cursor:pointer; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> <section id="chat" />

They are many ways to use emoji's in react.他们有很多方法可以在反应中使用表情符号。 With NPM and also without it by a "span" tag.使用 NPM 也可以不使用“span”标签。

<span role="img" aria-label="arrow">➡️</span>

I find this simple and easy to use.我觉得这很简单且易于使用。

Putting this here just incase anyone else stumbles on this post looking for what I needed.把这个放在这里只是为了防止其他人偶然发现这篇文章寻找我需要的东西。

<p>
After a lot of Googling and reading I just copy/pasted the emoji, it seems to work fine 🤷‍♂️. 
</p>

To get the emoji I went to Discord sent the emoji I needed and copy/pasted it into my code and it shows up on screen.为了获得表情符号,我去了 Discord 发送了我需要的表情符号并将其复制/粘贴到我的代码中,它会显示在屏幕上。


React code for Grinning face with big eyes : Grinning face with big eyes反应代码:

<div>
  {String.fromCodePoint('0x1f603')}
</div>


Output: Output:


Full Emoji List, v15.0 - https://unicode.org/emoji/charts/full-emoji-list.html完整的表情符号列表,v15.0 - https://unicode.org/emoji/charts/full-emoji-list.html

-- --

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM