R
R
Richard Millie2020-01-25 17:38:41
JavaScript
Richard Millie, 2020-01-25 17:38:41

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

How to correctly create an action that causes a click, and also store the color that I pass to the ConmponentDiv in redux?
div component:
const ComponentDiv = () => {
    const style = {
        color: "blue"
    }

    return (

        <div style={style}>
            hello
        </div>

    )
}

export default ComponentDiv;

button component:
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

1 answer(s)
R
Roman Alexandrovich, 2020-01-25
@RomReed

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 question

Ask a Question

731 491 924 answers to any question