javascript - How do you go to another page on button click in react? - Stack Overflow

FYI I'm learning reactjs.I have a quick question. I am developing a restaurant website and I want

FYI I'm learning reactjs. I have a quick question. I am developing a restaurant website and I want to make my "View Full Menu" button on my homepage(Home.js) take me to my menu.js file. I was able to redirect to the menu page on my App.js file, which is essentially where I have my functional navbar ponents. Please let me know if I need to show more code for this question. Also, I know that I probably have to use "onClick" on the button, but after doing some research, I cannot figure out how to go to the menu.js page. Thank you in advance.

            <div>
                <Card className="text-center">
                    <Card.Header>Featured</Card.Header>
                    
                    <Card.Body>
                        <Card.Title>Menu</Card.Title>
                        <Card.Text>
                            orem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. At consectetur lorem donec massa sapien. Euismod lacinia at quis risus sed vulputate odio ut.
                        </Card.Text>
                        <Button variant="primary">View Full Menu</Button>
                    </Card.Body>
                    <Card.Footer className="text-muted"></Card.Footer>
                </Card>
            </div>

Here is my working "menu" button on my App.js file.

function App () {
  return (
    
    <BrowserRouter>
    <div className="App">
      <Navigation />
      <Routes>
        <Route path="/" element={<Home />} />
        <Route path="/about" element={<About />} />
        <Route path="/menu" element={<MenuApp />} />
        <Route path= "/fire" element={<Fire />}/>
      </Routes>
    </div>
  </BrowserRouter>
  
  
  );
}

FYI I'm learning reactjs. I have a quick question. I am developing a restaurant website and I want to make my "View Full Menu" button on my homepage(Home.js) take me to my menu.js file. I was able to redirect to the menu page on my App.js file, which is essentially where I have my functional navbar ponents. Please let me know if I need to show more code for this question. Also, I know that I probably have to use "onClick" on the button, but after doing some research, I cannot figure out how to go to the menu.js page. Thank you in advance.

            <div>
                <Card className="text-center">
                    <Card.Header>Featured</Card.Header>
                    
                    <Card.Body>
                        <Card.Title>Menu</Card.Title>
                        <Card.Text>
                            orem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. At consectetur lorem donec massa sapien. Euismod lacinia at quis risus sed vulputate odio ut.
                        </Card.Text>
                        <Button variant="primary">View Full Menu</Button>
                    </Card.Body>
                    <Card.Footer className="text-muted"></Card.Footer>
                </Card>
            </div>

Here is my working "menu" button on my App.js file.

function App () {
  return (
    
    <BrowserRouter>
    <div className="App">
      <Navigation />
      <Routes>
        <Route path="/" element={<Home />} />
        <Route path="/about" element={<About />} />
        <Route path="/menu" element={<MenuApp />} />
        <Route path= "/fire" element={<Fire />}/>
      </Routes>
    </div>
  </BrowserRouter>
  
  
  );
}
Share Improve this question asked Apr 2, 2022 at 22:44 saulcinhosaulcinho 411 silver badge5 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 2

As well as the answers above, you could use the React Router Link ponent like this:

<Link to="/menu">
    View Full Menu
</Link>

Or the useHistory() hook as in this example from the docs:

import { useHistory } from "react-router-dom";

function HomeButton() {
  let history = useHistory();

  function handleClick() {
    history.push("/home");
  }

  return (
    <button type="button" onClick={handleClick}>
      Go home
    </button>
  );
}

You can use the useNavigate hook to navigate programmatically from one route to another.

import { useNavigate } from "react-router-dom";

const Component = () => {
  const navigate = useNavigate();
  const handleGoToMenu = () => navigate("menu");

  return (
    <Button variant="primary" onClick={handleGoToMenu}>View Full Menu</Button>
  );
}

Using react-router v6 you can navigate imperatively using useNavigate hook:

 const navigate = useNavigate();
 const handleMenuClick = () => navigate("/menu", { replace: true });

         <div>
            <Card className="text-center">
                <Card.Header>Featured</Card.Header>   
                <Card.Body>
                    <Card.Title>Menu</Card.Title>
                    <Card.Text>
                        orem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. At consectetur lorem donec massa sapien. Euismod lacinia at quis risus sed vulputate odio ut.
                    </Card.Text>
                    <Button variant="primary" onClick={handleMenuClick}>View Full Menu</Button>
                </Card.Body>
                <Card.Footer className="text-muted"></Card.Footer>
            </Card>
        </div>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745307444a4621797.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信