{"id":44,"date":"2019-06-02T00:31:28","date_gmt":"2019-06-01T21:31:28","guid":{"rendered":"https:\/\/guoyunhe.me\/en\/?p=44"},"modified":"2020-04-18T18:23:56","modified_gmt":"2020-04-18T15:23:56","slug":"fix-react-issues-with-google-translate","status":"publish","type":"post","link":"https:\/\/guoyunhe.me\/en\/2019\/06\/02\/fix-react-issues-with-google-translate\/","title":{"rendered":"Fix React issues with Google Translate"},"content":{"rendered":"\n<p>Note: we are only talking about Google Translate function in Chrome or Chromium based browsers. Other translate plugins or software don't necessarily work with this solution. And this solution doesn't solve all issues, just a part of them.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">When does it happen<\/h2>\n\n\n\n<p>You have a component:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const Button = ({children, icon, isLoading, ...rest}) => (\n  &lt;button {...rest}>\n    {icon}\n    {children}\n    {isLoading &amp;&amp; &lt;Loader\/>}\n  &lt;\/button>\n)\n\nconst Test = () => &lt;Button icon=\"+\">Hello World&lt;\/Button><\/code><\/pre>\n\n\n\n<p>Which is rendered to:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;button>+Hello World&lt;\/button><\/code><\/pre>\n\n\n\n<p>If an element (<code>&lt;button&gt;<\/code> in this case) contains multiple rendered string variables, they become text nodes in React's VDOM and HTML DOM. However, Google Translate doesn't care about text nodes and wrap them inside a <code>&lt;font&gt;<\/code> element:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;button>&lt;font>+\u4f60\u597d\u4e16\u754c&lt;\/font>&lt;button><\/code><\/pre>\n\n\n\n<p>Since they are not text nodes anymore, synchronization between React and DOM was broken. The button content will not be updated anymore.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How to fix it<\/h2>\n\n\n\n<p>Simply avoid this situation. Write every <code>{variable}<\/code> inside an element, as the only child.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const Button = ({children, icon, isLoading, ...rest}) => (\n  &lt;button {...rest}>\n    &lt;span className=\"button__icon\">{icon}&lt;\/span>\n    &lt;span className=\"button__text\">{children}&lt;\/span>\n    {isLoading &amp;&amp; &lt;Loader\/>}\n  &lt;\/button>\n)\n\nconst Test = () => &lt;Button icon=\"+\">Hello World&lt;\/Button><\/code><\/pre>\n\n\n\n<p>Now every variable is synchronized with the <code>&lt;span&gt;<\/code> element, not text nodes. Inserting <code>&lt;font&gt;<\/code> elements doesn't break the connection. <\/p>\n\n\n\n<p>Another situation is that when you mix text nodes with elements.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;p>\n  Copyright 2016-{new Date().getFullYear()}\n&lt;\/p><\/code><\/pre>\n\n\n\n<p>Should be converted to:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;p>\n  &lt;span>Copyright 2016-&lt;\/span>\n  &lt;span>{new Date().getFullYear()}&lt;\/span>\n&lt;\/p><\/code><\/pre>\n\n\n\n<p>Just make sure: a text node or string variable must be the <strong>only child<\/strong> of an element.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Event target<\/h2>\n\n\n\n<p>Another issue is that the click event target will change. It might be <code>&lt;font&gt;<\/code> elements instead of <code>&lt;button&gt;<\/code>. Carefully check everywhere you use <code>e.target<\/code>, considering they might not be the button, input or other elements you expected.<\/p>\n\n\n\n<p>A further suggestion is to avoid using any <code>e.target<\/code> reference. Here is a piece of legacy code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const {sizes, onChange} = this.props\nreturn (\n  &lt;div>\n    {sizes.map(size => (\n      &lt;button\n        key={size}\n        name=\"size\"\n        value={size}\n        onClick={e => this.props.onChange(e.target.value)}>\n        {size.toUpperCase()}\n      &lt;\/button>\n    ))}\n  &lt;\/div>\n)<\/code><\/pre>\n\n\n\n<p>Can be changed to:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const {sizes, onChange} = this.props\nreturn (\n  &lt;div>\n    {sizes.map(size => (\n      &lt;button\n        key={size}\n        name=\"size\"\n        value={size}\n        onClick={e => this.props.onChange(size)}>\n        {size.toUpperCase()}\n      &lt;\/button>\n    ))}\n  &lt;\/div>\n)<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">If you use third-party components from a library<\/h2>\n\n\n\n<p>In this case, you usually cannot modify the component as you want.<\/p>\n\n\n\n<p>You can try to use <code>&lt;span&gt;your string&lt;\/span&gt;<\/code> instead of <code>\"your string\"<\/code> as component properties.<\/p>\n\n\n\n<p>Report to the library maintainers and make a PR with above methods.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">If the element doesn't support child elements<\/h2>\n\n\n\n<p>Some components doesn't support inner wrappers, like <code>&lt;option&gt;<\/code>. If you put <code>&lt;span&gt;<\/code> inside <code>&lt;option&gt;<\/code>, React will give you warnings in console, even though the rendering works. So you probably don't want to do it.<\/p>\n\n\n\n<p>For example you have:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;select>\n  {fruites.map(f => &lt;option value={f.name}>{f.name} - {f.price}&lt;\/option>)}\n&lt;\/select><\/code><\/pre>\n\n\n\n<p>Change it to:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;select>\n  {fruites.map(f => {\n    const label = f.name + ' - ' + f.price;\n  \treturn &lt;option value={f.name}>{label}&lt;\/option>\n  })}\n&lt;\/select><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">If you want to disable Google Translate<\/h2>\n\n\n\n<p>Even if you do all above, something can still go wrong. If you don't have time to waste and just want to disable Google Translate, it is simple:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;body class=\"notranslate\">\n  ...\n&lt;\/body><\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Note: we are only talking about Google Translate function in Chrome or Chromium based browsers. Other translate plugins or software don't necessarily work with this solution. And this solution doesn't solve all issues, just a part of them. When does it happen You have a component: Which is rendered to: If an element (&lt;button&gt; in [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[8,6,5,7],"class_list":["post-44","post","type-post","status-publish","format-standard","hentry","category-code","tag-front-end","tag-javascript","tag-react","tag-web"],"_links":{"self":[{"href":"https:\/\/guoyunhe.me\/en\/wp-json\/wp\/v2\/posts\/44","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/guoyunhe.me\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/guoyunhe.me\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/guoyunhe.me\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/guoyunhe.me\/en\/wp-json\/wp\/v2\/comments?post=44"}],"version-history":[{"count":14,"href":"https:\/\/guoyunhe.me\/en\/wp-json\/wp\/v2\/posts\/44\/revisions"}],"predecessor-version":[{"id":139,"href":"https:\/\/guoyunhe.me\/en\/wp-json\/wp\/v2\/posts\/44\/revisions\/139"}],"wp:attachment":[{"href":"https:\/\/guoyunhe.me\/en\/wp-json\/wp\/v2\/media?parent=44"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/guoyunhe.me\/en\/wp-json\/wp\/v2\/categories?post=44"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/guoyunhe.me\/en\/wp-json\/wp\/v2\/tags?post=44"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}