【问题标题】:conditional rendering react router dom条件渲染反应路由器dom
【发布时间】:2020-06-21 12:42:34
【问题描述】:

我正在制作一个带有可重用组件的广告网站,这些组件根据页面的不同加载不同的图形。我正在使用 react-router-dom Route 确切路径。我想我需要能够用我的 useEffect,[] 来读取它的状态。那么我如何传递状态以便组件能够读取它。

请注意组件 Hero 出现在主页、顾问和解决方案的功能中


const Hero = props => {
 useEffect(() => {
  console.log(props);
 }, []);
 return (
  <Fragment>
   <div className='grid-hero'>
    <Fragment>
     <div className='overlay'>
      <div>
       <p className='bg-dark'></p>
       <img src={{ homeimg } || { consimg } || { solsimg }} alt='' />
      </div>
     </div>
    </Fragment>
  <Router>
   <Fragment>
    <Navbar />

    <Switch>
     <Route exact path='/' component={Home} />
     <Route exact path='/consultants' component={Consultants} />
     <Route exact path='/solutions' component={Solutions} />
     <Route exact path='/contactus' component={ContactUs} />"
    </Switch>
   </Fragment>
  </Router>
    <div
    className={
     {pathname === "/" && ("grid-home")}
     {pathname === "/consultants" && ("grid-consultants")}
     {pathname === "/solutions" && ("grid-solutions")}
    }>

【问题讨论】:

  • 这里看不到任何状态。
  • 我想我需要把它钻进去
  • 什么是“应用级别”。不清楚具体问题是什么。您不确定如何将 props 传递给 Route 组件?
  • 我只需要能够在我打开 /solutions 时查看并看到,好吧,这里传递的 react 路由器表示它已加载 /solutions。我不认为我必须使用上下文来做,所以基本上是的,我喜欢将路由属性传递到挂载上的英雄状态
  • Hero 在哪里适合这个?应该是Home

标签: javascript reactjs react-router-dom


【解决方案1】:

也许这就是你要找的东西:

我已将 Hero 添加为您的图像将切换的顶级图像组件。我没有图像,所以我使用了字符串,但是如果您导入 JSX 之类的图像,您将能够通过删除我的 Hero 虚拟组件中的 img 字符串来使用此代码。

添加参考图像导入语法以获得帮助:

import img from './img.png';

完整示例:

import React, { Fragment } from "react";
import {
  BrowserRouter as Router,
  Route,
  Link,
  useLocation
} from "react-router-dom";

function BasicExample() {
  return (
    <Router>
      <div>
        <ul>
          <li>
            <Link to="/">Home</Link>
          </li>
          <li>
            <Link to="/contactus">ContactUs</Link>
          </li>
          <li>
            <Link to="/solutions">Solutions</Link>
          </li>
        </ul>

        <hr />
        <Hero />
        <Route exact path="/" component={Home} />
        <Route path="/contactus" component={ContactUs} />
        <Route path="/solutions" component={Solutions} />
      </div>
    </Router>
  );
}

const Hero = () => {
  const homeimg = "HOME_IMAGE";
  const consimg = "CONTACT_US_IMAGE";
  const solsimg = "SOLUTIONS_IMAGE";
  let img = null;

  const location = useLocation();
  if (location.pathname === "/") img = homeimg;
  else if (location.pathname === "/contactus") img = consimg;
  else if (location.pathname === "/solutions") img = solsimg;

  return (
    <Fragment>
      <h3>{location.pathname}</h3>
      <div className="grid-hero">
        <Fragment>
          <div className="overlay">
            <div>
              <p className="bg-dark" />
              {img}
            </div>
          </div>
        </Fragment>
      </div>
    </Fragment>
  );
};

function Home() {
  return (
    <div>
      <h2>Home</h2>
    </div>
  );
}

function ContactUs() {
  return (
    <div>
      <h2>ContactUs</h2>
    </div>
  );
}

function Solutions() {
  return (
    <div>
      <h2>Solutions</h2>
    </div>
  );
}

export default BasicExample;

codepen-link

【讨论】:

  • 我喜欢它... useLocation... 我在 fork hooks 上搜索了几个小时以进行条件渲染:X
  • 嗨,它有帮助,但这是我遇到的问题。我现在需要使用它来动态插入 className 字符串值,并且我尝试了字符串或变量的每次迭代,但没有任何效果。我的最新代码见底部的原始帖子
  • @Mickey Gray 创建一个与原始问题无关的新问题
猜你喜欢
  • 2019-04-25
  • 2018-06-02
  • 2021-04-23
  • 2017-12-23
  • 2018-07-26
  • 1970-01-01
  • 2023-04-03
  • 2019-03-02
相关资源
最近更新 更多