0

My styled component:

const Button = styled.button
  /* Adapt the colors based on primary prop */
  background: ${props => props.primary ? "grey" : "white"};
  color: ${props => props.primary ? "white" : "grey"};

  font-size: 1em;
  margin: 1px;
  padding: 0.25em 1em;
  border: 1px solid grey;
  cursor: pointer;
  border-radius: 3px;
`;`

And this is my return

 return (

  <div className="App">

    <Button >Lunch</Button>
    <Button primary>Dinner</Button>
    </div>
  </div>
)

the button look like this the button when the button "Lunch" click design button swap with "Dinner", how to make button onclik (like button active and inactive), i'm newbie with reactjs and styled-component.

8
  • your style component looks good. What do you mean with swap? Lunch gets Dinner style and vice versa? Commented Sep 11, 2020 at 13:04
  • you can have a ternary operator for adding that "primary" attribute, so when "Lunch" button is clicked toggle the styling and apply it to the "Dinner" btn and vice versa. Commented Sep 11, 2020 at 13:08
  • What is the question? Commented Sep 11, 2020 at 13:13
  • @MohitArora what the mean ternary operator ?, and i mean what is it ?, haha i really dun no
    – dipretelin
    Commented Sep 11, 2020 at 18:21
  • @dipretelin check the MDN docs developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… You can use ternary operator to evaluate a condition and perform what action has to be performed when condition evaluates to true or false. In your case you can apply the color to button Commented Sep 12, 2020 at 4:56

0

Your Answer

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

Browse other questions tagged or ask your own question.