(react)我的主页上有两个按钮,但只能样式一个按钮
我的主页上有两个按钮。
按钮ONE用于滚动到主页上的元素。
按钮二是链接到我网站上的其他页面的链接。
按钮1来自React-Scroll,而按钮2来自React-Router-dom
当前按钮1滚动正确,并正确显示样式。 但是,按钮2链接到其他页面,但没有显示任何样式,并显示“ navtopage”的属性,而不是“ buttonlabel”的值。
我该如何纠正此功能,以使两个按钮具有样式和效能性?
这是来自data.js的代码,以index.js为
export const homeObjOne = {
id: "about",
lightBg: false,
lightText: true,
lightTextDesc: true,
topLine: "Freeman Ltd",
headline: "Professional & Affordable Services",
description:
"We enjoy helping our clients by utilizing over 20 years of experience.",
buttonLabel: "Get started",
imgStart: false,
img: require("../../images/scales.jpg"),
alt: "North West",
dark: true,
primary: true,
darkText: false,
navToPage: "services",
};
export const homeObjTwo = {
id: "services",
lightBg: true,
lightText: false,
lightTextDesc: false,
topLine: "FAQ",
headline: "We are here to help!",
description:
"Have Questions? We have answers.",
buttonLabel: "Learn More!",
imgStart: true,
img: require("../../images/faq.jpg"),
alt: "FAQ",
dark: false,
primary: false,
darkText: true,
navToPage: "/faq",
};
index.js,
import React from "react";
import { Button } from "../ButtonElements";
import { PageButton } from "../ButtonPageElements";
import { Link } from "react-router-dom";
import {
InfoContainer,
InfoWrapper,
InfoRow,
Column1,
Column2,
TextWrapper,
TopLine,
Heading,
Subtitle,
BtnWrap,
ImgWrap,
Img,
} from "./infoElements";
const InfoSection = ({
lightBg,
id,
imgStart,
topLine,
lightText,
headline,
darkText,
description,
buttonLabel,
img,
alt,
primary,
dark,
dark2,
navToPage,
}) => {
return (
<>
<InfoContainer lightBg={lightBg} id={id}>
<InfoWrapper>
<InfoRow imgStart={imgStart}>
<Column1>
<TextWrapper>
<TopLine>{topLine}</TopLine>
<Heading lightText={lightText}>{headline}</Heading>
<Subtitle darkText={darkText}>{description}</Subtitle>
<BtnWrap>
{navToPage && navToPage.startsWith("/") ? (
//if it is linked to another page use router link
<Link to={navToPage} style={{}}>
{navToPage}
</Link>
) : (
//else use the smart link component
<Button
to={navToPage}
smooth={true}
duration={500}
spy={true}
exact="true"
offset={-80}
primary={primary ? 1 : 0}
dark={dark ? 1 : 0}
dark2={dark2 ? 1 : 0}
>
{buttonLabel}
</Button>
)}
</BtnWrap>
</TextWrapper>
</Column1>
<Column2>
<ImgWrap>
<Img src={img} alt={alt} />
</ImgWrap>
</Column2>
</InfoRow>
</InfoWrapper>
</InfoContainer>
</>
);
};
export default InfoSection;
的代码。
import styled from "styled-components";
import { Link } from "react-scroll";
export const Button = styled(Link)`
border-radius: 50px;
background: ${({ primary }) => (primary ? "#ca1f27" : "#010606")};
white-space: nowrap;
padding: ${({ big }) => (big ? "14px 48px" : "12px 30px")};
color: ${({ dark }) => (dark ? "#010606" : "#fff")};
font-size: ${({ fontBig }) => (fontBig ? "20px" : "16px")};
outline: none;
border: none;
cursor: pointer;
display: flex;
justify-content: center;
align-items: center;
transition: all 0.2s ease-in-out;
&:hover {
transition: all 0.2s ease-in-out;
background: ${({ primary }) => (primary ? "#fff" : "#ca1f27")};
}
`;
这是buttonelements.js
import styled from "styled-components";
import { Link } from "react-router-dom";
export const PageButton = styled(Link)`
border-radius: 50px;
background: ${({ primary }) => (primary ? "#ca1f27" : "#010606")};
white-space: nowrap;
padding: ${({ big }) => (big ? "14px 48px" : "12px 30px")};
color: ${({ dark }) => (dark ? "#010606" : "#fff")};
font-size: ${({ fontBig }) => (fontBig ? "20px" : "16px")};
outline: none;
border: none;
cursor: pointer;
display: flex;
justify-content: center;
align-items: center;
transition: all 0.2s ease-in-out;
&:hover {
transition: all 0.2s ease-in-out;
background: ${({ primary }) => (primary ? "#fff" : "#ca1f27")};
}
`;
I have two buttons on my main pages.
Button one is for scrolling to elements on the main page.
Button two is for linking to other pages on my website.
Button 1 is from react-scroll while Button 2 is from react-router-dom
Currently button 1 scrolls properly and displays styling correctly.
However button 2 links to other pages but does not display any styling and displays the property of "navToPage" rather than the value of "buttonLabel".
How can I correct this so that both buttons have styling and fuctionality?
Here is the code from Data.js that feeds into index.js
export const homeObjOne = {
id: "about",
lightBg: false,
lightText: true,
lightTextDesc: true,
topLine: "Freeman Ltd",
headline: "Professional & Affordable Services",
description:
"We enjoy helping our clients by utilizing over 20 years of experience.",
buttonLabel: "Get started",
imgStart: false,
img: require("../../images/scales.jpg"),
alt: "North West",
dark: true,
primary: true,
darkText: false,
navToPage: "services",
};
export const homeObjTwo = {
id: "services",
lightBg: true,
lightText: false,
lightTextDesc: false,
topLine: "FAQ",
headline: "We are here to help!",
description:
"Have Questions? We have answers.",
buttonLabel: "Learn More!",
imgStart: true,
img: require("../../images/faq.jpg"),
alt: "FAQ",
dark: false,
primary: false,
darkText: true,
navToPage: "/faq",
};
Here is index.js
import React from "react";
import { Button } from "../ButtonElements";
import { PageButton } from "../ButtonPageElements";
import { Link } from "react-router-dom";
import {
InfoContainer,
InfoWrapper,
InfoRow,
Column1,
Column2,
TextWrapper,
TopLine,
Heading,
Subtitle,
BtnWrap,
ImgWrap,
Img,
} from "./infoElements";
const InfoSection = ({
lightBg,
id,
imgStart,
topLine,
lightText,
headline,
darkText,
description,
buttonLabel,
img,
alt,
primary,
dark,
dark2,
navToPage,
}) => {
return (
<>
<InfoContainer lightBg={lightBg} id={id}>
<InfoWrapper>
<InfoRow imgStart={imgStart}>
<Column1>
<TextWrapper>
<TopLine>{topLine}</TopLine>
<Heading lightText={lightText}>{headline}</Heading>
<Subtitle darkText={darkText}>{description}</Subtitle>
<BtnWrap>
{navToPage && navToPage.startsWith("/") ? (
//if it is linked to another page use router link
<Link to={navToPage} style={{}}>
{navToPage}
</Link>
) : (
//else use the smart link component
<Button
to={navToPage}
smooth={true}
duration={500}
spy={true}
exact="true"
offset={-80}
primary={primary ? 1 : 0}
dark={dark ? 1 : 0}
dark2={dark2 ? 1 : 0}
>
{buttonLabel}
</Button>
)}
</BtnWrap>
</TextWrapper>
</Column1>
<Column2>
<ImgWrap>
<Img src={img} alt={alt} />
</ImgWrap>
</Column2>
</InfoRow>
</InfoWrapper>
</InfoContainer>
</>
);
};
export default InfoSection;
Here is the code for ButtonElements.js
import styled from "styled-components";
import { Link } from "react-scroll";
export const Button = styled(Link)`
border-radius: 50px;
background: ${({ primary }) => (primary ? "#ca1f27" : "#010606")};
white-space: nowrap;
padding: ${({ big }) => (big ? "14px 48px" : "12px 30px")};
color: ${({ dark }) => (dark ? "#010606" : "#fff")};
font-size: ${({ fontBig }) => (fontBig ? "20px" : "16px")};
outline: none;
border: none;
cursor: pointer;
display: flex;
justify-content: center;
align-items: center;
transition: all 0.2s ease-in-out;
&:hover {
transition: all 0.2s ease-in-out;
background: ${({ primary }) => (primary ? "#fff" : "#ca1f27")};
}
`;
Here is the code for ButtonPageElements.js
import styled from "styled-components";
import { Link } from "react-router-dom";
export const PageButton = styled(Link)`
border-radius: 50px;
background: ${({ primary }) => (primary ? "#ca1f27" : "#010606")};
white-space: nowrap;
padding: ${({ big }) => (big ? "14px 48px" : "12px 30px")};
color: ${({ dark }) => (dark ? "#010606" : "#fff")};
font-size: ${({ fontBig }) => (fontBig ? "20px" : "16px")};
outline: none;
border: none;
cursor: pointer;
display: flex;
justify-content: center;
align-items: center;
transition: all 0.2s ease-in-out;
&:hover {
transition: all 0.2s ease-in-out;
background: ${({ primary }) => (primary ? "#fff" : "#ca1f27")};
}
`;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论