小编Mik*_*ugs的帖子

无法在生产环境中运行的Gatsby应用中基于URL参数更新JSX属性

在生产模式下,URL参数值不能用于呈现动态属性值。相同的URL参数值可用于呈现特定的组件。

我已经建立了一个带有最小可复制示例的仓库。

https://github.com/mikepuglisi/gatsby-dynamic-attribute-bug

我们已经能够通过将参数值存储在状态中来解决此问题,但是我不明白为什么这是必要的(特别是因为它呈现了正确的div)。

相关代码(src / pages / index.js)

const IndexPage = ({location}) => {
  const params = new URLSearchParams(location.search);
  const color = params.get('color');
  return (
      <Layout>
        <SEO title="Home" />
        <h1>Hi people</h1>
        { color ?
          <p style={{color: color}}>
            I SHOULD BE THE COLOR {color} in production mode even after hitting CTRL+F5 (hard refresh)
          </p> :
          <p>
            No Color was passed. Add ?color=blue to URL and hit CTRL+F5 to ensure a hard refresh
          </p>
        }

        <div style={{ maxWidth: `300px`, marginBottom: `1.45rem` …
Run Code Online (Sandbox Code Playgroud)

reactjs gatsby

3
推荐指数
1
解决办法
86
查看次数

标签 统计

gatsby ×1

reactjs ×1