Home > Mobile >  How do I link button to another react page
How do I link button to another react page

Time:06-01

import React, { useEffect, useState } from 'react';
import styled from 'styled-components';
import manImage from '../utils/man.png';
import faceImage from '../utils/sad-face.png';
import axios from 'axios';
import { quizData } from './../data/data';
import {
  BrowserRouter as Router,
  Switch,
  Route,
  Link,
  Routes,
  useNavigate
} from 'react-router-dom';
import Quiz from './Quiz';

const Container = styled.div`
  width: 44vw;
  height: 80vh;
  background: #ffffff;
  box-shadow: 0px 15px 40px 5px #c4c4c4;
  border-radius: 20px;
  position: absolute;
  top: 50%;
  left: 65%;
  transform: translate(-50%, -50%);
  display: flex;
  flex-direction: column;
  align-items: center;
`;

const Text = styled.span`
  font-family: 'Poppins';
  font-style: normal;
  line-height: 80px;
  color: #004360;
  font-weight: 500;
  font-size: 32px;
  width: fit-content;
  margin-top: 40px;
`;

const Image = styled.img`
  background-repeat: no-repeat;
  width: 100%;
  max-width: 32vw;
  height: auto;
  display: flex;
  position: absolute;
  top: 60%;
  left: 20%;
  : translate(-50%, -50%);
`;

const ScrollBar = styled.span`
  margin-top: 15px;
  margin-bottom: 18px;
  top: 20px;
  left: 20px;
  bottom: 20px;
  padding: 0 15px;
  overflow-y: scroll;
  overflow-x: hidden;
  &::-webkit-scrollbar {
    border-radius: 5px;
    width: 10px;
    height: 40px;
    background-color: #f5f5f5;
  }
  &::-webkit-scrollbar-thumb {
    border-radius: 5px;
    background: #c6dae1;
    height: 10px;
  }
`;

const ButtonWrapper = styled.div`
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  margin-bottom: 15px;
`;

const Button = styled.button`
  border-radius: 5px;
  justify-content: center;
  align-items: center;
  padding: 10px 15px;
  font-size: 20px;
  outline: 0;
  width: 35vw;
  height: 6.2vh;
  background-color: rgba(255, 209, 123, 0.86);
  border: none;
  color: #004360;
  filter: drop-shadow(0px 4px 4px rgba(0, 0, 0, 0.25));
  transition: 0.4s;
  :hover {
    cursor: pointer;
    box-shadow: 0px 4px 14px -1px rgba(255, 209, 123, 0.86);
    background-color: rgba(255, 237, 204, 0.86);
    color: #f7a32a;
  }
`;

const NoQuizzes = styled.div`
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
`;

const Text2 = styled.span`
  font-family: 'Poppins';
  font-style: normal;
  line-height: 75px;
  color: #f7a32a;
  font-weight: 500;
  font-size: 45px;
  width: fit-content;
  margin-top: 65px;
`;

const Image2 = styled.img`
  background-repeat: no-repeat;
  width: 100%;
  max-width: 30vw;
  height: auto;
`;

function QuizList() {
  return (
    <>
      <Container>
        {quizData.length > 0 && (
        <>
        <Text>Select Quiz</Text>
        <ScrollBar>
          {quizData.map((data, id) => (
             <Link to="/Quiz">
            <ButtonWrapper key={id}>
              
                {/* <Routes>
                  <Route path="../Pages/Quiz" component={Quiz} />
                </Routes> */}
               
                  <Button component={Link} to = '/Quiz'>{data.title}</Button>
                
              
            </ButtonWrapper>
            </Link>
          ))}
        </ScrollBar>
      </>
    )}
    {quizData.length === 0 && (
      <NoQuizzes>
        <Text2>No Quizzes Assigned</Text2>
        <Image2 src={faceImage} />
      </NoQuizzes>
    )}
  </Container>
  <Image src={manImage} />
</>
  );
}

export default QuizList;

I tried adding a Link tag so when the user clicks on the button it'll take them to another page, however it does not take them there. I also tried a routes path but that also did not link the buttons. Where am I going wrong? How do I link the buttons to another page? Do I use react router or am I supposed to use an <a> tag?

CodePudding user response:

You can use new enter image description here

I suggest simply wrapping the styled Button component in a Link.

Example:

<Link to="/Quiz">
  <Button>{data.title}</Button>
</Link>

enter image description here

Edit how-do-i-link-button-to-another-react-page

Full Code:

function QuizList() {
  return (
    <>
      <Container>
        {!!quizData.length && (
          <>
            <Text>Select Quiz</Text>
            <ScrollBar>
              {quizData.map((data, id) => (
                <ButtonWrapper key={id}>
                  <Link to="/Quiz">
                    <Button>{data.title}</Button>
                  </Link>
                </ButtonWrapper>
              ))}
            </ScrollBar>
          </>
        )}
        {!quizData.length && (
          <NoQuizzes>
            <Text2>No Quizzes Assigned</Text2>
            <Image2 src={faceImage} />
          </NoQuizzes>
        )}
      </Container>
      <Image src={manImage} />
    </>
  );
}
  • Related