diff --git a/components/trustpilot-reviews.jsx b/components/trustpilot-reviews.jsx index 3a27f9b..b4ba0ff 100644 --- a/components/trustpilot-reviews.jsx +++ b/components/trustpilot-reviews.jsx @@ -1,8 +1,7 @@ -import React, { useState, useEffect } from "react" +import React, { useState, useEffect, useRef } from "react" import PropTypes from "prop-types" import { useStaticQuery, graphql } from "gatsby" -import isReady from "../utils/is-ready" import filterLocale from "../utils/filter-locale" export default function TrustpilotReviews({ @@ -12,9 +11,8 @@ export default function TrustpilotReviews({ height, width, }) { - const [ready, setReady] = useState(isReady()) const [loaded, setLoaded] = useState(false) - const reference = React.createRef() + const ref = useRef() const { sitePlugin: { pluginOptions: { template, business, username }, @@ -29,40 +27,36 @@ export default function TrustpilotReviews({ ` ) const { domain, locale } = filterLocale(language, culture) - useEffect(() => { - setReady(isReady()) - }, [!ready]) useEffect(() => { - if (!loaded && ready) { - window.Trustpilot.loadFromElement(reference.current, true) + if (typeof window !== "undefined" && window.Trustpilot) { + window.Trustpilot.loadFromElement(ref.current, true) setLoaded(true) } + + console.log(loaded) }, [loaded]) - if (ready) { - return ( -
+ - - Trustpilot - -
- ) - } else { - return null - } + Trustpilot + + + ) } TrustpilotReviews.propTypes = { diff --git a/package.json b/package.json index 4bc43ff..dbd2d66 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "@pittica/gatsby-plugin-trustpilot-widget", "private": false, "description": "Trustpilot widget for GatsbyJS.", - "version": "2.0.3", + "version": "2.0.4", "author": { "name": "Lucio Benini", "email": "info@pittica.com", diff --git a/utils/is-ready.jsx b/utils/is-ready.jsx deleted file mode 100644 index 9371550..0000000 --- a/utils/is-ready.jsx +++ /dev/null @@ -1,3 +0,0 @@ -export default function isReady() { - return typeof window !== "undefined" && window.Trustpilot -}