{"componentChunkName":"component---src-templates-blog-post-js","path":"/blog/load-zendesk-in-react/","result":{"data":{"site":{"siteMetadata":{"title":"Gatsby Contentful starter"}},"contentfulBlogPost":{"title":"Load Zendesk in React","publishDate":"January 27th, 2023","heroImage":{"fluid":{"tracedSVG":"data:image/svg+xml,%3csvg%20xmlns='http://www.w3.org/2000/svg'%20width='400'%20height='267'%20viewBox='0%200%20400%20267'%20preserveAspectRatio='none'%3e%3cpath%20d='M0%20134v133h401V0H0v134m221-72l-2%201-1%201-1%202h-2l-2-2-3-1v2l2%202%201%201v2l-1%202c0%201-1%201-2-1-3-4-5-4-2%200v2l-3-5c-2-6-3-5-3%202%200%205%201%206%202%206v1c-4%201-4%201-3-1h-1c-2%202-2%202-8%203-4%200-8%202-4%202l2%201%201%202v1l-1%202%202%201c3-2%206-2%206-1v3h-4c-1%201-4-1-6-2-4-3-5-4-4-7%201-5-5-14-8-12l-2%201-1%203c-3%202-2%203%201%203l3%202-1%201v3c1%201%200%202-1%203v4c1%202%200%203-2%200s-1%201%201%205l4%203v1l-2%202h2v1l-1%202-1%203c-1%201-1%201%201%201%202-1%202-1%202%201-1%202-1%202%202%200l4-1%202-2c0-2%202-3%202-1v2c-2%201-2%202-1%203v3h2l1%201-4%201c-5%201-7%203-2%203%204%200%209%202%209%204l-2%202c-2%200-3%201-3%202-1%201%200%201%201%201%202%200%202%201%202%202l-2%202c-2%200-2%201-1%202v9l1-3h14v5l1%204%201-10c0-9%200-10-2-11s-5-7-5-9l-1-1-1-1h-1l-1%201v-12c-2-1-3-5-1-5l1-3c0-2%202-5%203-4l-1%202v9l3-3%201-1c1%201-2%208-3%207v4c1%201%201%201%201-1l2-3%201-1c-1-2%200-3%202-2%201%201%201%201%201-2s0-4%202-4l1%201c-1%201%200%202%203%202s4-1%205-3l2-2-1%203c-1%202%200%204%201%202%201-1%201-1%201%201l2%203c1%200%202%201%202%206%200%206%200%206-3%209-4%203-6%206-8%2013l-2%208c-3%204-2%205%200%205%203%200%205-2%205-4l1-2%201-2c1-1%200-2-1-2v2l-1%201v-6c0-3%202-3%202%200%201%202%201%202%203%200l13-9c3-2%202%200-3%205-3%204-4%205-3%206l4-2%203-3%202-1c1-1%201-1%201%201v2c2%200%201%202-1%204-1%201-4%209-2%209l3-5c3-6%204-15%202-16v-1c2%200%201-4-1-5l-1-2c0-1-1-2-3-2-1%200-2-1-3-6l-2-8v1l1%208c0%205%200%205-2%206-3%201-3%200-4-10-1-8-1-8%201-7%202%202%204%201%202-2v-2h1l2%202c2%200%202%200%201-3v-4c1-2%201-2-1-2-1-1%200-1%202-1%203%201%203%201%202-6l-2-1h-1c0-1%202-4%204-4v-3c2-1%203-4%201-4l-2%203-2%202-2%202h-2c-2-2-2-2-3%200s-2%200-2-7c0-6-1-7-2-5'%20fill='%23d3d3d3'%20fill-rule='evenodd'/%3e%3c/svg%3e","aspectRatio":1.5,"src":"//images.ctfassets.net/umhszhofm0zy/7orLdboQQowIUs22KAW4U/445eaa8c80c6fd2303c6fb327f38e9e5/matt-palmer-254999.jpg?w=1180&q=50&bg=rgb%3A000000","srcSet":"//images.ctfassets.net/umhszhofm0zy/7orLdboQQowIUs22KAW4U/445eaa8c80c6fd2303c6fb327f38e9e5/matt-palmer-254999.jpg?w=295&h=197&q=50&bg=rgb%3A000000 295w,\n//images.ctfassets.net/umhszhofm0zy/7orLdboQQowIUs22KAW4U/445eaa8c80c6fd2303c6fb327f38e9e5/matt-palmer-254999.jpg?w=590&h=393&q=50&bg=rgb%3A000000 590w,\n//images.ctfassets.net/umhszhofm0zy/7orLdboQQowIUs22KAW4U/445eaa8c80c6fd2303c6fb327f38e9e5/matt-palmer-254999.jpg?w=1180&h=787&q=50&bg=rgb%3A000000 1180w,\n//images.ctfassets.net/umhszhofm0zy/7orLdboQQowIUs22KAW4U/445eaa8c80c6fd2303c6fb327f38e9e5/matt-palmer-254999.jpg?w=1770&h=1180&q=50&bg=rgb%3A000000 1770w,\n//images.ctfassets.net/umhszhofm0zy/7orLdboQQowIUs22KAW4U/445eaa8c80c6fd2303c6fb327f38e9e5/matt-palmer-254999.jpg?w=2360&h=1573&q=50&bg=rgb%3A000000 2360w,\n//images.ctfassets.net/umhszhofm0zy/7orLdboQQowIUs22KAW4U/445eaa8c80c6fd2303c6fb327f38e9e5/matt-palmer-254999.jpg?w=3000&h=2000&q=50&bg=rgb%3A000000 3000w","sizes":"(max-width: 1180px) 100vw, 1180px"}},"body":{"childMarkdownRemark":{"html":"<p>Adding Zendesk livechat on a react app might not be as straghtforward as it looks.</p>\n<p>The example uses state to check if $zopim is ready to interact (isZopimReady).</p>\n<p>Once that state is true, $zopim is ready to be interacted with. The best way is to have a useEffect hook listening to isZopimReady and run the desired configurations on $zopin.</p>\n<pre><code class=\"language-javascript\">import React, { FC, useEffect, useState } from \"react\";\nimport { useInterval } from \"usehooks-ts\";\nimport useScript from \"hooks/useScript\";\n\ndeclare const $zopim: any;\n\ninterface ZendeskProps {\n  firstName?: string | null;\n  lastName?: string | null;\n  email?: string | null;\n}\n\nconst Zendesk: FC&#x3C;ZendeskProps> = ({ firstName, lastName, email }) => {\n      const status = useScript(\"https://static.zdassets.com/ekr/snippet.js?key=8f60d464-1c48-48a6-8d14-25f47c389e59\", {\nid: \"ze-snippet\",\n      });\n\n      const [isZopimReady, setIsZopimReady] = useState(false);\n\n      /**\n       * used to set livechat configurations\n       * once isZopimReady is true\n       */\n      useEffect(() => {\nif (isZopimReady) {\n  $zopim(() => {\n    $zopim.livechat.button.setOffsetVertical(80);\n    $zopim.livechat.button.setOffsetHorizontal(60);\n    $zopim.livechat.button.setPosition(\"br\");\n    if (firstName &#x26;&#x26; lastName) {\n      $zopim.livechat.setName(`${firstName} ${lastName}`);\n    }\n    if (email) {\n      $zopim.livechat.setEmail(email);\n    }\n  });\n}\n      }, [isZopimReady]);\n\n      /**\n       * used to set isZopimReady to true\n       * once script is ready and $zopim is defined\n       */\n      useInterval(\n() => {\n  if (status === \"ready\" &#x26;&#x26; typeof $zopim !== \"undefined\") {\n    setIsZopimReady(true);\n  }\n  return;\n},\n!isZopimReady ? 200 : null,\n      );\n\n      return null;\n};\n\nexport default Zendesk;\n</code></pre>"}}}},"pageContext":{"slug":"load-zendesk-in-react"}}}