Answer the question
In order to leave comments, you need to log in
How to change the style of an element based on a certain condition?
I need to recolor the adjacent div in blue on button click. The button component and the div component are on the same level.
<App>
<ComponentButton onClick = {..} />
<ConmponentDiv color = {..}/>
</App
const ComponentDiv = () => {
const style = {
color: "blue"
}
return (
<div style={style}>
hello
</div>
)
}
export default ComponentDiv;
const ComponentButton = () => {
return (
<button
onClick={...}
className="btn">click me
</button>
);
};
export default ComponentButton;
Answer the question
In order to leave comments, you need to log in
const ComponentDiv = () => {
const styleBlue = {
color: "blue"
}
const styleRed = {
color: "red"
}
return (
<div style={true?styleBlue:styleRed}> //вместо true доставьте свою переменную
hello
</div>
)
}
export default ComponentDiv;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question