Answer the question
In order to leave comments, you need to log in
How to set if-else in styles?
There is this:
<p
style={{ bottom: (669 * size) / SIZES.MAX }}
>
<p
style={SIZES.MAX > 0
?
{ bottom: (669 * size) / SIZES.MAX }
:
{ bottom: (669 * size) }
}
>
Answer the question
In order to leave comments, you need to log in
As Valery
noted , the error is most likely because you are passing a number. Try passing a string (you can use "back quotes "):
<p
style={
SIZES.MAX > 0 ? {
bottom: `${(669 * size) / SIZES.MAX}px`,
color: `red`
} : {
bottom: `${(669 * size)}px`,
color: `blue`
}
}
>
class MyParagraph extends React.Component {
render() {
//init values
const SIZES = {MAX: 45};
const size = 1;
//get styles
function getParagraphStyles(condition) {
if (condition > 0) {
return {
bottom: `${(669 * size) / SIZES.MAX}px`,
color: `red`,
}
} else {
return {
bottom: `${(669 * size)}px`,
color: `blue`,
}
}
}
return (
<p style={getParagraphStyles(SIZES.MAX)}>
Paragraph
</p>
)
}
};
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question