Tengo el siguiente useRef que contiene 4 hijos.
Estoy tratando de eliminar a un niño de aquí en función de una verificación if.
Pero obteniendo el error:
typeError: myRef.current.removeChild is not a function.Pero mirando las respuestas aquí, esto parece ser correcto. ¿Qué estoy haciendo mal?
¿Cómo eliminar el elemento secundario de un div en reaccionar usando useRef hook?
//This is coming from another file. Just for reference. const myRef = React.useRef(); export const postData = ( myRef, token, id1, id2, ) => { myRef.current.children[0].value = token; myRef.current.children[1].value = id1; myRef.current.children[2].value = id2; [...myRef.current.children].forEach((child) => { if (!child.value) { // error on this line myRef.current.removeChild(child); } }); };puede usar useEffect hook aquí, tal vez esto resuelva su problema.
... const myRef = useRef(null) useEffect(() => { myRef.current.children[0].value = token; myRef.current.children[1].value = id1; myRef.current.children[2].value = id2; [...myRef.current.children].forEach((child) => { if (!child.value) { myRef.current.removeChild(child); } }); }, [myRef.current, child.value]) return ( <div ref={myRef}> ... </div> ) ...