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

1 Answers1

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>   
    );
}

awran5
  • 4,333
  • 2
  • 15
  • 32
  • 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 –  Dec 27 '19 at 19:03
  • Sorry i didn't understand would you explain more? The main question was about changing the div width not height? – awran5 Dec 27 '19 at 19:45