The way to do it with refs is
declare a ref variable const fieldRef = React.useRef()
ref
const fieldRef = React.useRef()
To access that ref use fieldRef.current
fieldRef.current
But this is inside the same component, but I want to access it through a ref in the main component, so outside of the component that is containing the input field
You can do something like this
function Main() { const ref = React.useRef(); React.useEffect(() => { if (ref.current) { // Do something } }); return <ChildComponent setRef = {ref}> }
function Child(props) { return <input ref={props.setRef} /> }
Ended up doing something I did not know the existence of and followed the react docs on this: reactjs.org/docs/forwarding-refs.h...
Yup this is exactly what I was talking about.
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
The way to do it with refs is
declare a
ref
variableconst fieldRef = React.useRef()
To access that ref use
fieldRef.current
But this is inside the same component, but I want to access it through a ref in the main component, so outside of the component that is containing the input field
You can do something like this
Main Component
Child Component
Ended up doing something I did not know the existence of and followed the react docs on this: reactjs.org/docs/forwarding-refs.h...
Yup this is exactly what I was talking about.