Does React help me with local events that are not related with the state?
Consider the following HTML example.
<p id="func_123">it is me</p>
<p><span onmouseover="document.getElementById('func_123').style.color='orange'")>
There </span> you are.</p>
I try to do this in React. But I don’t want to establish the whole top-down unidirectional data flow approach for this event. That event does not have an impact to the data model so I don’t want to have it in the state. It is just a small visualisation thing.
Does React help me with this?
This is what I have so far:
import React from 'react';
export function LocalDiv(){
const hightlight = (node) => {node.style.color='orange'}
function P1() {
return <p>it is me</p>
}
function P2() {
return <p><span onMouseOver={hightlight}">There</span> you are</p>
}
return <div><P1/><P2/></div>;
}
export function App(props) {
return <LocalDiv></LocalDiv>
}
But I don’t manage to get the P1 node into the hightlight function. I tried a few things. But I don’t know if I am trying to achieve something which should better done completely in a different way.
2
Answers
I’m sorry if I don’t fully understand your needs, but here’s just a suggestion: you can achieve this with CSS. As you said "It is just a small visualization thing.", and that’s exactly what CSS is used for.
In React, if you want to handle local events and make small visual changes without involving the state, you can use the useRef hook to directly access and manipulate the DOM elements.
from this way you can apply visual changers, without affecting the component’s state.