React - How to track useRef children change

Dvi N

I have a component (Comp) that I've added a useRef (compRef) to its parameters. As part of the main useEffect - I'm setting the data of this component. That data contains an array of strings, and the Comp displaying all of them as inputs -

<Comp ref={compRef}>
     <input type="text"/>
</Comp>

and

data = ["HIDDEN", "data1", "data2", "data3", "data4"]

useEffect(() => { 
     compRef.current.refElement.value = data;
}, []);

so now the Comp will look like this -

<Comp>
<generatedWrap> // should be hidden
    <input value="HIDDEN" />
</generatedWrap>

<generatedWrap>
    <input value="data1" />
</generatedWrap>

<generatedWrap>
    <input value="data2" />
</generatedWrap>

<generatedWrap>
    <input value="data3" />
</generatedWrap>

<generatedWrap>
    <input value="data4" />
</generatedWrap>
</Comp>

I want to hide the first input with this hide command -

compRef.current.refElement.children[0].style.display = "none";

The problem is, that with every method (useEffect) I'm using during the mount of the screen, the children are empty. Only after the screen is ready It's working (i.e. triggered by a <button>)

I need a way to track the number of children of the ref before the view is mounted, and once it's greater than 1 to fire the hide command.

How can I do that?

Dvi N

Ok

So after a lot of testing - the only option I found is to add setTimeout(() => compRef.current.refElement.children[0].style.display = "none") after the render of the Comp.

(After the render there is still only 1 child but the fact that the setTimeout is a-synchronic make it work)

Thank you all for the help :)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

React: How to view children nodes with useRef?

React | How to change the style of an element using useRef?

React, useRef to find the number of children

How to react on a change of visibility of children?

How to understand the useRef in react

In react, how to get noticed when children change?

How to change state of multiple React children?

How to apply useRef() in React TS?

how to preserve signature on react with useRef

React useRef : Null (Cannot read property 'children' of null)

react-custom-scrollbars - how to change horizontal scroll track color?

React useRef() not working onMouseOver to change backgroundColor of div

React re-evaluate useRef if props change

How to keep track of children count in Firebase

How can I change the onClick function of a button saved in a useRef object in a react component?

How to track React hooks?

How to check if parent element include children with spesific class with useEffect and UseRef?

How to get children div via parent useRef in Reactjs

How to change alpha of children

How to change opacity on children

How to use useRef to change the style of a element?

REACT.JS - how to update multiple children on parents state change by one of children

Change the children of a component dynamically in React

How to find an element in JSX using useRef in React

How to use React useRef hook with typescript?

How to reset input field from useRef in React?

How to use the useRef hook for setNativeProps in React Native?

react/Jest: How to use `useRef` in a test

How to change the track color of a SwitchCompat