React Helmet TypeError: Cannot convert a Symbol value to a string
React Helmet TypeError: Cannot convert a Symbol value to a string
我正在尝试使用以下 react-helmet 向我的网站添加 Google 分析:
<Helmet
htmlAttributes={{
lang: this.props.language
? convertLangCodeToISO639(this.props.language)
: "en",
}}
>
{this.props.googleTrackingID && (
<>
<script
async
src={`https://www.googletagmanager.com/gtag/js?id=${this.props.googleTrackingID}`}
></script>
<script>
{`window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', \"${this.props.googleTrackingID}\");`}
</script>
</>
)}
</Helmet>
不幸的是,这给出了错误:
TypeError: Cannot convert a Symbol value to a string. Helmet does not support rendering <" + child.type + "> elements. Refer to our API for more information.
这不是我正在做的,因为我使用的是 <script>
标签。我还需要外部 JSX 标签,因为有两个子 <script>
标签。
导致此错误的原因是什么?
我最终能够使用多个 <Helmet>
标签做到这一点,将 JSX 条件移动到头盔外:
<Helmet
htmlAttributes={{
lang: this.props.language
? convertLangCodeToISO639(this.props.language)
: "en",
}}
/>
{/* Google Analytics */}
{this.props.survey.googleTrackingID && (
<Helmet>
<script
async
src={`https://www.googletagmanager.com/gtag/js?id=${this.props.survey.googleTrackingID}`}
></script>
<script>
{`window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', "${this.props.survey.googleTrackingID}");`}
</script>
</Helmet>
)}
我正在尝试使用以下 react-helmet 向我的网站添加 Google 分析:
<Helmet
htmlAttributes={{
lang: this.props.language
? convertLangCodeToISO639(this.props.language)
: "en",
}}
>
{this.props.googleTrackingID && (
<>
<script
async
src={`https://www.googletagmanager.com/gtag/js?id=${this.props.googleTrackingID}`}
></script>
<script>
{`window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', \"${this.props.googleTrackingID}\");`}
</script>
</>
)}
</Helmet>
不幸的是,这给出了错误:
TypeError: Cannot convert a Symbol value to a string. Helmet does not support rendering <" + child.type + "> elements. Refer to our API for more information.
这不是我正在做的,因为我使用的是 <script>
标签。我还需要外部 JSX 标签,因为有两个子 <script>
标签。
导致此错误的原因是什么?
我最终能够使用多个 <Helmet>
标签做到这一点,将 JSX 条件移动到头盔外:
<Helmet
htmlAttributes={{
lang: this.props.language
? convertLangCodeToISO639(this.props.language)
: "en",
}}
/>
{/* Google Analytics */}
{this.props.survey.googleTrackingID && (
<Helmet>
<script
async
src={`https://www.googletagmanager.com/gtag/js?id=${this.props.survey.googleTrackingID}`}
></script>
<script>
{`window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', "${this.props.survey.googleTrackingID}");`}
</script>
</Helmet>
)}