简体   繁体   English

基于className的条件渲染

[英]Conditional render based on className

I want to conditionally render an element base on whether or not its previous sibling has a certain className. 我想根据其前一个兄弟是否具有某个className来有条件地呈现一个元素。

The Nav.Link receives a className of "is-active" when the user is at the href="" position on that Nav.Link on the page (this is achieved by 3rd party library). 当用户位于页面上Nav.Link上的href =“”位置时,Nav.Link会收到className为“is-active”(这是由第三方库实现的)。

I want to render a Coin element after Nav.Link inside of their parent Nav.Item based on whether or not the Nav.Link has the className "is-active". 我想根据Nav.Link是否具有className“is-active”,在其父Nav.Item内的Nav.Link之后渲染一个Coin元素。

I don't know how to do this. 我不知道该怎么做。

import React, { Component } from "react";
import Nav from "react-bootstrap/Nav";
import "./lrg-nav.styles.scss";
import { Row, Col } from "react-bootstrap";
import ScrollspyNav from "react-scrollspy-nav";
import Coin from "./coin-spinner/coin-spinner.component";

class LrgNav extends Component {
  constructor(props) {
    super(props);
    this.state = {

    };
  }

  render() {
    return (
      <Row className="lrg-nav-container">
        <Col className="d-flex align-items-center justify-content-center">
          <ScrollspyNav
            scrollTargetIds={["about-me", "work", "skills"]}
            activeNavClass="is-active"
            scrollDuration="1000"
            headerBackground="true"
          >
            <Nav>
              <Nav.Item className="d-flex align-items-center">
                <Nav.Link
                  className="about-me is-active lrgNavItem pl-4 pr-0"
                  href="#about-me"
                >
                  About Me
                </Nav.Link>
                {/* this is where I'd like to render a <Coin/> element based on if the previous sibling <Nav.Link className="is-active"> is true */}
              </Nav.Item>
              <Nav.Item className="d-flex align-items-center">
                <Nav.Link className="lrgNavItem pl-4 pr-0" href="#work">
                  Work
                </Nav.Link>
                {/* this is where I'd like to render a <Coin/> element based on if the previous sibling <Nav.Link className="is-active"> is true */}
              </Nav.Item>
              <Nav.Item className="d-flex align-items-center">
                <Nav.Link className="lrgNavItem pl-4 pr-0" href="#skills">
                  Skills
                </Nav.Link>
                {/* this is where I'd like to render a <Coin/> element based on if the previous sibling <Nav.Link className="is-active"> is true */}
              </Nav.Item>
              <Nav.Item className="d-flex align-items-center">
                <Nav.Link className="lrgNavItem pl-4 pr-0" href="#aFewWords">
                  A Few Words
                </Nav.Link>
                {/* this is where I'd like to render a <Coin/> element based on if the previous sibling <Nav.Link className="is-active"> is true */}
              </Nav.Item>
              <Nav.Item className="d-flex align-items-center">
                <Nav.Link className="lrgNavItem pl-4 pr-0" href="#contact">
                  Contact
                </Nav.Link>
                {/* this is where I'd like to render a <Coin/> element based on if the previous sibling <Nav.Link className="is-active"> is true */}
              </Nav.Item>
            </Nav>
          </ScrollspyNav>
        </Col>
      </Row>
    );
  }
}

export default LrgNav;



You could abstract the Nav.Item out to a function to handle it for you: 您可以将Nav.Item抽象为一个函数来为您处理它:

const NavItemAndCoin = () => {
  const isActive = someDeterminationForActivity();
  const classes = `about-me lrgNavItem pl-4 pr-0 ${isActive ? 'is-active'}`;
  return (
    <Nav.Item className="d-flex align-items-center">
      <Nav.Link
        className={classes}
        href="#about-me"
      >
        About Me
      </Nav.Link>
      {isActive && <Coin ... />}
    </Nav.Item>
  );
}

Then you can use NavItemAndCoin in place of NavItem 然后,您可以使用NavItemAndCoin代替NavItem

<NavItemAndCoin />

You would still need to work out some method to get the position (ie, the someDeterminationForActivity() method). 你仍然需要找出一些方法来获得位置(即someDeterminationForActivity()方法)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM