I am building a web application in ReactJS. I have the following:
const MyComponent = (props: { array: Array<Data> }) => {
const styles = mergeStyleSets({
container: {
backgroundColor: transparent,
},
item: {
backgroundColor: "#ccc",
},
itemContent: {
color: "#000",
},
});
return (
<div class={styles.container}>
{props.array.map((x, i) => (
<div key={i} class={styles.item}>
<div class={styles.itemContent}></div>
</div>
))}
</div>
)
};
This will render a container with some items in it all with the same background and same text color.
More complex selectors
Now I want to have alternating backgrounds and colors, therefore I try to use nth-child(odd)
and get a different backgroundColor
and color
for odd items:
const styles = mergeStyleSets({
container: {
backgroundColor: transparent,
},
item: {
backgroundColor: #ccc,
selectors: {
":nth-child(odd)": {
backgroundColor: "#ddd",
selectors: {
itemContent: {
color: "#fff",
},
},
},
},
},
itemContent: {
color: "#000",
},
});
As you can see, I need to reference class itemContent
inside the selector of item
. But my solution is not working. How can I achieve this?