if else syntax
See original GitHub issueI am trying to create button with styling attributes,
This is not very conveniant but it work.
const Button = styled.button`
/* Adapt the colors based on primary prop */
color: ${props =props.theme.atoms.Button.default.color};
background: ${
(props) =>
// if primary
props.primary ? props.theme.atoms.Button.primary.background :
// else if success
props.success ? props.theme.atoms.Button.success.background :
// else if success
props.info ? props.theme.atoms.Button.info.background :
// else if warning
props.warning ? props.theme.atoms.Button.warning.background :
// else if danger
props.danger ? props.theme.atoms.Button.danger.background :
// else default
props.theme.atoms.Button.default.background
}
font-size: 1em;
margin: 1em;
padding: 0.25em 1em;
border: 2px solid palevioletred;
border-radius: 3px;
`;
Also eslint is now complaining:
/workspace/github.com/mxstbr/react-boilerplate/app/components/atoms/Button/index.js
6:12 error Expected parentheses around arrow function argument arrow-parens
8:3 error Arrow function used ambiguously with a conditional expression no-confusing-arrow
10:5 error Do not nest ternary expressions no-nested-ternary
12:5 error Do not nest ternary expressions no-nested-ternary
14:5 error Do not nest ternary expressions no-nested-ternary
16:5 error Do not nest ternary expressions
I guess there must be a better way.
Thanks in advance for informing me
Issue Analytics
- State:
- Created 7 years ago
- Reactions:2
- Comments:21 (18 by maintainers)
Top Results From Across the Web
JavaScript if else else if - W3Schools
Use the else if statement to specify a new condition if the first condition is false. Syntax. if (condition1) { // block of...
Read more >C if...else Statement - Programiz
The if...else statement executes two different codes depending upon whether the test expression is true or false. Sometimes, a choice has to be...
Read more >if...else - JavaScript - MDN Web Docs - Mozilla
The if...else statement executes a statement if a specified condition is truthy. If the condition is falsy, another statement in the ...
Read more >C - if...else statement - Tutorialspoint
C - if...else statement, An if statement can be followed by an optional else statement, which executes when the Boolean expression is false....
Read more >C if else statement - Javatpoint
The if-else statement is used to perform two operations for a single condition. The if-else statement is an extension to the if statement...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Why not do something like this:
Then you can use your different button styles like this:
You might now think “but now people can pass wrong values (or even nothing!) into the
style
prop”, and you’d be right – except we’re talking about a react component, which can havepropTypes
anddefaultProps
! 🎉Boom, done, exact same behaviour as your really long ternary expression except much neater 😊
Does that answer the question?
*Edited to reflect the correct observation by @kopax that
style
is a reserved property in react, so I changed it tonature
as to not confuse future readers.`Your example to create a styled component involve a syntax like this :
It mean if you need to add method you will have to write something like this :
Instead of storing in a ES6 class. it would be nice to write the css inside a class syntax.