【问题标题】:Adding react components to header based on the page根据页面添加react组件到header
【发布时间】:2018-06-01 18:32:17
【问题描述】:

在我网站的所有页面上,除了两个页面,我希望标题包含两个按钮。我怎样才能让标题意识到这一点,以便它在需要时包含按钮,而不是其他两个页面。我正在使用 React 和 Gatsby

我可以通过向我的组件添加 if 语句来实现这一点吗?如果是这样,我不确定如何超越这一步。

  const isHomepage = pathname == `/`
  const isContact = pathname == `/contact/`
  let styles = {}
  if (isHomepage) {

    }
  } else if (isContact) {



     } else {
  }

这是我的标题代码:

import React from 'react'
import Link from 'gatsby-link'
import colors from '../utils/colors'

const Header = ({ siteTitle }) => (
  <div
    style={{
      background: colors.white,
      marginBottom: '1.45rem',
      borderBottom: '1px solid',
      borderBottomColor: colors.green,
      height: '3rem',
      top: 0,
      left: 0,
      width: '100%',
      position: 'fixed',
    }}
  >
    <div
      style={{
        paddingLeft: 20,
        paddingRight: 20,
        marginLeft: 'auto',
        marginRight: 'auto',
        maxWidth: 960,
      }}
    >
      <div
        style={{
          display: 'flex',
          flexDirection: 'row',
          alignItems: 'center',
          height: 53,
        }}>
        <h3 style={{
          margin: 0,
        }}>
        <Link
          to="/"
          style={{
            color: colors.green,
            textDecoration: 'none',
            fontWeight: 450,
          }}
        >
          {siteTitle}
        </Link>
      </h3>
        </div>
    </div>
  </div>
)

export default Header

【问题讨论】:

    标签: javascript reactjs children gatsby


    【解决方案1】:

    使用基于 location.pathname 的条件渲染

    {['/path1', '/path2'].indexOf(location.pathname) === -1 && (
       <button>1<button>
       <button>2<button>
    )}
    

    import React from 'react'
    import Link from 'gatsby-link'
    import colors from '../utils/colors'
    
    const Header = ({ siteTitle }) => (
      <div
        style={{
          background: colors.white,
          marginBottom: '1.45rem',
          borderBottom: '1px solid',
          borderBottomColor: colors.green,
          height: '3rem',
          top: 0,
          left: 0,
          width: '100%',
          position: 'fixed',
        }}
      >
        <div
          style={{
            paddingLeft: 20,
            paddingRight: 20,
            marginLeft: 'auto',
            marginRight: 'auto',
            maxWidth: 960,
          }}
        >
          <div
            style={{
              display: 'flex',
              flexDirection: 'row',
              alignItems: 'center',
              height: 53,
            }}>
            <h3 style={{
              margin: 0,
            }}>
            <Link
              to="/"
              style={{
                color: colors.green,
                textDecoration: 'none',
                fontWeight: 450,
              }}
            >
              {siteTitle}
            </Link>
            {['/path1', '/path2'].indexOf(location.pathname) === -1 && (
              <button>1<button>
              <button>2<button>
            )}
          </h3>
            </div>
        </div>
      </div>
    )
    
    export default Header

    【讨论】:

    • 非常感谢您的回复。我怎样才能做到这一点,以便标题在所有不是/path1 和/path2 的路径上都包含这两个按钮。我想要每个页面的按钮,除了两个。
    • 你看过答案了吗?我回答了你的问题。现在轮到您在哪里添加这些按钮以及您想要什么样的按钮了。自己做点什么
    • 我已阅读并实施了您的回答。当 URL 包含 path1 和 path2 时,将显示这些按钮。而不是添加我想让这两个按钮出现的每条路径(有很多而且数量正在增长)我希望按钮出现在除了两个之外的所有东西上。这有意义吗?
    • 你是在使用我的代码吗['/path1', '/path2'].indexOf(location.pathname) !== -1
    • 是的,我是。我不想搞砸任何事情。我是 React 新手。
    猜你喜欢
    • 1970-01-01
    • 2012-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-05
    • 1970-01-01
    • 2022-10-23
    • 1970-01-01
    相关资源
    最近更新 更多