简体   繁体   中英

Styled-Component Button href/onclick not working

I'm learning JS/trying to get a website up using gatsby and need to use buttons and have been using styled-components.

the buttons show up and when I hover over it does turn yellow but I i dont get a grabber mouse when I hover nor when I click do the buttons open up google

please help :)

I tried just using an href attribute like https://www.styled-components.com/ was used on the main website

<Button
     href="https://github.com/styled-components/styled-components"
     target="_blank"
     rel="noopener"
     primary
   >

but didn't work either

here is the styled button component

const Button = styled.button`
  border: none;
  height: 3rem;
  width: 50%;
  border-radius: .5rem;
  margin: 2% 25% 3% 25%;
  z-index:5;

  background-color: ${props => props.theme.grey};
  color: ${props => props.theme.offWhite};
  &:hover {
    background: ${props => props.theme.yellow};
    color: ${props => props.theme.grey};
  }

and here is the offending code

<button onclick="window.open('https://www.google.com', '_blank');"> work</button>
<Button click={() => 'https://www.google.com';}}>pleaseWork </Button>

You should add styled.a not styled.button to have access to href prop.

Now you can add Styled Component like the website:

<Button
     href="https://github.com/styled-components/styled-components"
     target="_blank"
     rel="noopener"
     primary
   >

See this demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM