【问题标题】:Component it's not being display in react app组件未显示在反应应用程序中
【发布时间】:2020-09-03 16:23:34
【问题描述】:

我正在尝试在我的应用程序中路由到微积分页面,路由器运行良好,但遗憾的是我选择的组件没有显示,我使用了反应路由器、链接、history.push 和 switch。在我的登录路径中,我使用了几乎相同的代码并且一切正常。

所以如果有人可以看一下代码,请。

代码


import React, { useState } from "react";
import {
  BrowserRouter as Router,
  Switch,
  Route,
  Link,
  useHistory,
  //useLocation,
} from "react-router-dom";
import classNames from "classnames";
import { SectionProps } from "../../utils/SectionProps";
import ButtonGroup from "../elements/ButtonGroup";
import Button from "../elements/Button";
import Image from "../elements/Image";
import Modal from "../elements/Modal";

import CalculusIntro from "./partials/CalculusIntro";

const propTypes = {
  ...SectionProps.types,
};

const defaultProps = {
  ...SectionProps.defaults,
};

const Hero = ({
  className,
  topOuterDivider,
  bottomOuterDivider,
  topDivider,
  bottomDivider,
  hasBgColor,
  invertColor,
  ...props
}) => {
  const [videoModalActive, setVideomodalactive] = useState(false);

  let history = useHistory();

  const openModal = (e) => {
    e.preventDefault();
    setVideomodalactive(true);
  };

  const closeModal = (e) => {
    e.preventDefault();
    setVideomodalactive(false);
  };

  const outerClasses = classNames(
    "hero section center-content",
    topOuterDivider && "has-top-divider",
    bottomOuterDivider && "has-bottom-divider",
    hasBgColor && "has-bg-color",
    invertColor && "invert-color",
    className
  );

  const innerClasses = classNames(
    "hero-inner section-inner",
    topDivider && "has-top-divider",
    bottomDivider && "has-bottom-divider"
  );

  return (
    <Router>
      <section {...props} className={outerClasses}>
        <div className="container-sm">
          <div className={innerClasses}>
            <div className="hero-content">
              <h1
                className="mt-0 mb-16 reveal-from-bottom"
                data-reveal-delay="200"
              >
                Solução em <span className="text-color-primary">fretes</span>{" "}
                para o seu negócio!
              </h1>
              <div className="container-xs">
                <p
                  className="m-0 mb-32 reveal-from-bottom"
                  data-reveal-delay="400"
                >
                  25 anos de experiência em gestão de fretes.
                </p>
                <div className="reveal-from-bottom" data-reveal-delay="600">
                  <ButtonGroup>
                    <Button tag="a" color="primary" wideMobile>
                      <Link
                        to="/calculo"
                        onClick={() => {
                          history.push("/calculo");
                        }}
                      >
                        Faça uma cotação
                      </Link>
                    </Button>
                    <Button tag="a" color="dark" wideMobile href="#">
                      Seja nosso cliente
                    </Button>
                  </ButtonGroup>
                  <Switch>
                    <Route
                      path="/calculo"
                      component={CalculusIntro}
                    ></Route>
                  </Switch>
                </div>
              </div>
            </div>
            <div
              className="hero-figure reveal-from-bottom illustration-element-01"
              data-reveal-value="20px"
              data-reveal-delay="800"
            >
              <a
                data-video="https://player.vimeo.com/video/416768488"
                href="#0"
                aria-controls="video-modal"
                onClick={openModal}
              >
                <Image
                  className="has-shadow"
                  src={require("./../../assets/images/video-placeholder.jpg")}
                  alt="Hero"
                  width={1200}
                  height={800}
                />
              </a>
            </div>
            <Modal
              id="video-modal"
              show={videoModalActive}
              handleClose={closeModal}
              video="https://player.vimeo.com/video/416768488"
              videoTag="iframe"
            />
          </div>
        </div>
      </section>
    </Router>
  );
};

Hero.propTypes = propTypes;
Hero.defaultProps = defaultProps;

export default Hero;


【问题讨论】:

    标签: javascript reactjs react-router


    【解决方案1】:

    您可以创建一个名为 Routers 的文件:

    const Routers =() => {
        return(
            <Router history={history}>
                <Switch>
                        <Route path="/" component={Comments} exact />
                        <Route path="/theme" component={Theme} exact />
                </Switch>
            </Router>
        )
    }
    

    在你的 App.js 中,只渲染路由器的组件:

    class App extends Component {
    
      render() {
        return (
            <Routers></Routers>
        )
      }
    }
    

    你需要包装你的应用程序:

    ReactDOM.render(
      <React.StrictMode>
        <BrowserRouter>
        <App />
        </BrowserRouter>
      </React.StrictMode>,
      document.getElementById('root')
    );
    

    创建一个名为 history 的文件

    import { createBrowserHistory as history} from 'history';
    
    export default history();
    

    现在导入上一个类

    import history from './history';
      <div><Button onClick={() => history.push('/theme')}>see</Button></div>
    

    我的依赖是:

    "dependencies": {
        "@testing-library/jest-dom": "^4.2.4",
        "@testing-library/react": "^9.5.0",
        "@testing-library/user-event": "^7.2.1",
        "bootstrap": "^4.4.1",
        "react": "^16.13.1",
        "react-bootstrap": "^1.0.1",
        "react-dom": "^16.13.1",
        "react-router-dom": "^5.1.2",
        "react-scripts": "3.4.1"
      },
    

    【讨论】:

      【解决方案2】:

      尝试将确切的属性添加到您的路线。

      <Route path="/calculo" component={CalculusIntro} exact></Route>
      
      

      【讨论】:

        猜你喜欢
        • 2019-05-27
        • 1970-01-01
        • 1970-01-01
        • 2019-02-15
        • 2021-03-27
        • 2021-09-18
        • 2016-07-14
        • 1970-01-01
        • 2022-07-19
        相关资源
        最近更新 更多