score:0
Accepted answer
you can do it by using nested ternary operations.
return (
<div classname="degree-box" style={
{
backgroundimage: time2 > 19 ? `url(${night})` :
( time2 > 16 ? `url(${afternoon})` : (
time2 > 7 ? `url(${morning})` : null ))
}} >
//code...
</div>
);
score:0
you don't need to overcomplicate things. as you can see even stackoverflow can't highlight your code as expected, because you already have your property name defined, all you need to supply is a value.
{
backgroundimage: time2 > 19 ? `url(${night})` :
time2 > 16 ? `url(${afternoon})` :
time2 > 7 ? `url(${morning})` : null
}
even shorter to avoid repeating yourself;
{
backgroundimage: `url(${time2 > 19 ? night : time2 > 16 ? afternoon : time2 > 7 ? morning:null})`
}
Source: stackoverflow.com
Related Query
- Change the background in a few hours
- How do I change the background color of the body?
- How do I change the background color of a selected react-bootstrap ToggleButton?
- Change the background color of a screen with react native
- Antd - is there anyway to change background color of the Card title?
- How to make the background image change every X seconds in React?
- How to change the text color and background color of ant-tooltip component
- randomly change the background color of a react app by a click
- How to change the selected table row background color with React
- How to change the selected table row background using Material-UI on React
- In React Is there a way to change the background color of a parent Label tag on a Radio Input checked?
- How to change the background color of an ant.design InputNumber?
- Change the background image dynamically
- How to change the background color of a parent component from inside a child component in react, gatsby
- How to change the background color in semantic ui react?
- ReactJS 7 - How to conditionally change the background color of table cell only (not the row) based on its value?
- react I want to change the background of globalstyle in chakra theme.ts by props
- How to change the background color of chart in amcharts
- How to change the background and text color within button using button onClick in React JS
- Semantic React UI : is there a way to change the background color of a checkbox toggle?
- how to change background color when I click the button using react hooks
- How do I change the default background color of notistack in MUI v5?
- How to change the background color of MUI Menu Popover of TextField with select property?
- I want to change variant or background of the clicked button (onClick) only in reactjs. How can i achieve it?
- Antd: Need to change the background color of collapse body
- Cannot change the background colour of <SideNav>
- in a map function, i want to set a state which let me change the background color when i click on a div
- How to change the background or text color of the countdown timer to green when there is only 30 minutes remaining to finish the countdown?
- Change a different Background Color of Chip(material-UI) in a Comments Module when the user is Admin
- Sass: Applying the same rule to two classes to ensure that the background colour of a span element doesn't change
More Query from same tag
- How to use Graphql typescript types in react
- Circular dependency for extracted styles with styled-components
- How to attach file using Nodemailer in React.js
- React js: Why I'm unable to create closure?
- After updating state, using set, the state still has empty array
- How to avoid reactjs to be included twice in a rails and reactjs environment?
- How to call var in return ()
- mobex, @inject only specific part of store
- Get a Ref to BlockListBlock in the block editor
- react router slug compared to part of array
- React Warning: Prop x did not match. Server: x Client: x
- Display all characters from API across multiple pages in REACT
- Higher-Order Component and accessing child of the wrapped element
- Exporting methods in react component
- Filter condition in Reactjs
- unable to make a package run in nextjs which is working in reactjs
- how to order by date one Object.keys array in react.js?
- REACT Component not returning axios data
- Nextjs - How to pass params to server router with Link
- React component not updating
- How to change the url when rerendering a component in React?
- dynamically generate headings and rows reactjs using map - nested
- is there any way to stick on the same page even after refreshing in react router?
- Basic React Tests Failing after Implementing Redux
- MUI Autocomplete change selected option when another field changes
- next.js dynamic import component from object
- ReactJS - JavaScript: How to zoom on customized marker after click on it
- Conditional output in dropdown component
- React: using handleclick to toggle class of mapped element by key
- Correct way to pass arguments to reducer in React+Redux?