0

my style import React from 'react'; import styled from 'styled-components';

export const DivMenuButton = styled.div`
    border: 0px;
    backgroundColor: #000;
    height: 400px;
    width: 200px;
`;

my return:

import { DivMenuButton } from './styles';

export default function Menu() {
    const [open, setOpen] = useState(0); 
    const handleClick = e => {
      e.preventDefault();
      setOpen(!open);
    };
    return (
      <DivMenuButton>
        <Button
          style={{ margin:0, padding: 0, height: "30px", width: "100%", borderRadius:'0px' }}
          onClick={handleClick}
        >
          Button
        </Button>
      </DivMenuButton>  
    );
}

I would also like to know how I could do the following:

I have a state open

my div will start with 400 px clicking the button will get it 30px but I don't know how I can do this with styled components

2
  • See this SO answer Commented Dec 27, 2019 at 16:56
  • there he went on to active = prop active can i create any name? how open and prop open? Commented Dec 27, 2019 at 17:33

1 Answer 1

1

Use styled-components props

Try this:

export const DivMenuButton = styled.div`
    border: 0px;
    background-color: #000; // was wrong syntax
    height: 400px;
    width: ${props => props.width}
`;

export default function Menu() {
    const [open, setOpen] = useState(false); 
    const handleClick = e => {
      // e.preventDefault(); no need 
      setOpen(!open);
    };

    return (
      <DivMenuButton width={open ? '30px' : '400px'}>
        <button
          style={{ margin:0, padding: 0, height: "30px", width: "100%", borderRadius:'0px' }}
          onClick={handleClick}
        >
          Button
        </button>
      </DivMenuButton>   
    );
}

Sign up to request clarification or add additional context in comments.

2 Comments

Hello this way when I click my button the div would continue with the same prop and would not change. example she starts with 400 height i clicking a button she would go to 40px
Sorry i didn't understand would you explain more? The main question was about changing the div width not height?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.