简体   繁体   English

如何使用 Hooks 从一个组件到另一个组件创建 Link(href),从 Navbar 传递到我的组件?

[英]How do I make a Link(href) from one component to the other using Hooks, passing from Navbar to my component?

I would like to pass down a link to a component that is going to be rendered on the same page.我想传递一个指向将在同一页面上呈现的组件的链接。 I have a Navbar that has links and I want to set the href to '.Container-One' that's referenced in another component.我有一个带有链接'.Container-One' ,我想将 href 设置为在另一个组件中引用的'.Container-One' How should I do this if I'm using React Hooks functional components?如果我使用 React Hooks 功能组件,我应该怎么做?

Here is my Navbar.js这是我的 Navbar.js

import React from "react";
import { makeStyles } from "@material-ui/core/styles";
import AppBar from "@material-ui/core/AppBar";
import Toolbar from "@material-ui/core/Toolbar";
import Typography from "@material-ui/core/Typography";
import Button from "@material-ui/core/Button";

const useStyles = makeStyles(theme => ({
  root: {
      flexGrow: 1
  },
  menuButton: {
      marginRight: theme.spacing(2)
  },
  title: {
      flexGrow: 1
  }
  }));

export default function ButtonAppBar() {
  const classes = useStyles();

  return (
      <div className={classes.root}>
      <AppBar style={{ backgroundColor: "#343567" }} position="static">
          <Toolbar>
          <Typography variant="h6" className={classes.title}>
              Welcome
          </Typography>
          <Button
              onClick={e => e.preventDefault()}
              href={".Container-One"}
              color="inherit"
          >
              About
          </Button>
          <Button color="inherit">Experience</Button>
          <Button color="inherit">Contact</Button>
          </Toolbar>
      </AppBar>
      </div>
  );
}

Here is my component that I would like it to be linked too.这是我的组件,我也希望它被链接。

import React from "react";
import CssBaseline from "@material-ui/core/CssBaseline";
import Typography from "@material-ui/core/Typography";
import Container from "@material-ui/core/Container";
import styles from "./aboutme.module.css";
import myPhoto from "../../images/me.jpg";
import TrendingUpIcon from "@material-ui/icons/TrendingUp";

function AboutMe() {
  return (
    <div>
      <Container className={styles["Container-One"]}>
          <Typography className={styles["Content-One"]} component="div">
          <h3 className={styles["Container-One-Heading"]}>
              Hello! I'm lorem ipsum. Dont know me? Check me out at call lorem- 
              ipsum
          </h3>
          </Typography>
      </Container>
    </div>
  );
}

export default AboutMe;

An example of this might be in simple HTML you would pass a button with href to a container or another HTML class or id on the same page.这方面的一个例子可能是在简单的 HTML 中,您将一个带有 href 的按钮传递给同一页面上的容器或另一个 HTML 类或 id。

For example:例如:

<button href="#Container-Three"}>container-three section</button> 

I would like to do this in react, but I'm using different components.我想在反应中做到这一点,但我使用了不同的组件。

I was able to answer this one myself.我自己能够回答这个问题。 I found that if I set the props in the component that is rendering both of the components.我发现如果我在渲染两个组件的组件中设置道具。 First, the component aka.首先,组件又名。 Navbar that has the links.具有链接的导航栏。 Second, the component that is being displayed within the same component.其次,在同一组件中显示的组件。 That confused you, no worries here is an example.这让你感到困惑,不用担心,这里是一个例子。

EX:前任:

So for the example shown above, this is Navbar.js.因此,对于上面显示的示例,这是 Navbar.js。

return (
 <div className={classes.root}>
  <AppBar style={{ backgroundColor: "#343567" }} position="static">
    <Toolbar>
      <Typography variant="h6" className={classes.title}>
        Welcome
      </Typography>
      <Button href={props.hreftwo} color="inherit"> //Here I passed props- 
        //accessible from the parent prop
        About
      </Button>
      <Button href={props.hrefthree} color="inherit">
        Experience
      </Button>
      <Button color="inherit">Contact</Button>
    </Toolbar>
  </AppBar>
 </div>
);

This is the parent prop Home.js.这是父道具 Home.js。

Ex:前任:

import React from "react";
import AboutMe from "../components/AboutMe";
import Navbar from "../components/Navbar";

function Home() {
return (
    <div>
    //Below I am passing the hrefs relative to the containers that are shown 
    // in <AboutMe /> section
    <Navbar hreftwo="#Container-Two" hrefthree="#Container-Three" />
    <AboutMe />
    </div>
);
}

export default Home;

That's how I was able to access the id's of one component and then add links that go to that component in my Navbar.这就是我如何能够访问一个组件的 ID,然后在我的导航栏中添加指向该组件的链接。

There is also a great solution this issue here, StackOverflow React-Scroll这里也有一个很好的解决方案, StackOverflow React-Scroll

暂无
暂无

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

相关问题 使用 React Hooks,我如何制作一个可重用的组件和父组件相互利用? - Using React Hooks, how do I make a reusable component and parent component that utilize each other? 将值从一个组件传递到另一个组件 - Passing value from one component to other component 从 React 组件转换为使用一个钩子 - convert from React component to using one hooks 我如何在 react-bootstrap 中从组件 header 到组件 App 中的另一个组件做一个 href? - how do i do a href in react-bootstrap from the component header to another component in the component App? 如何使用 React 将状态从一个组件挂钩到另一个组件 - How to use React hooks state from one component to another Component 使用React-Hooks,如果其中一个兄弟姐妹更改了状态,如何防止从Array.map创建的组件重新呈现? - Using React-Hooks, how do I prevent a component created from Array.map from re-rendering if one of it's siblings changes state? 我想从一个组件访问我的 state 变量到另一个组件 - I want to access my state variable from one component to other 如何使用钩子在 REACTJS 中的不同 url 中将状态从一个组件获取到另一个组件? - How to get state from one component to other in a different url in REACTJS with hooks? 在 Angular 7 中,当将数据从一个组件传递到另一个组件时,我是否使用服务并在接收组件中订阅/收听? - In Angular 7 when passing data from one component to another, do I use a service and subscribe/listen in the receiving component? 如何使用 Grid Component 在不重叠的情况下使我的元素之一处于反应位置? - How do I make a one of my elements in react position without overlapping using Grid Component?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM