S
S
Senbonzakuraa2020-10-04 16:22:20
React
Senbonzakuraa, 2020-10-04 16:22:20

How to remove the class from another instance of the component on click?

There is this component:

export const ButtonRoom = (props) => {
        const [activeRoom, setActiveRoom] = useState(false)
        const setRoom = () => {
            setActiveRoom(!activeRoom)
        }
        return(
            <button onClick={()=>setRoom()} className={activeRoom ? "room__active button" : "button"}>
            </button>
        )
    }

At the moment, I can assign/remove the class on click only for the element on which the click is made, but how can I remove the class from another instance of the component?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladimir Korotenko, 2020-10-04
@firedragon

I commented above, but in theory the component from the mountain should not do this at all. Or he sends a message to the parent, and he drives either the general state for the application or the event bus

T
tdtdtd, 2020-10-05
@tdtdtd

If the component whose class should change is located much higher than the component that should initialize the class change, then either props through the chain, or use the context, or fasten some storage ala redux.
But React.createContext is probably what you need.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question