0

I want to access an element using React.createRef() (which is inside a function). Below is the example of how I want to access it.

react:

someFunction = () => {
   let element = this.myRef.current;
   console.log(element)
}
render(){
  return(
     <FlexView>
        <div ref={this.myRef}>
          some text here 
        </div>
        {this.someFunction()}
     </FlexView>
  )
}

Here, I want to access the div element through someFunction(). But for some reason this.myRef.current is returning a null value in console - I guess the issue is related to react life cycle, but I just can't figure out why. However by using the button, I can able to access the div element without having any problem, but only when I try to use the above method to trigger a function it's returning null.

render(){
  return(
     <FlexView>
        <div ref={this.myRef}>
          some text here 
        </div>
        <button onClick={this.someFunction()}>click-here</button>
     </FlexView>
  )
}

Can someone please let me know how do it.

p.s. I'm new to React and Js

4
  • 1
    You need to call the function inside componentDidMount to get the data after comonent rendered like componentDidMount() { this.someFunction(); } . Working example here codesandbox.io/s/react-createref-forked-kwdbgd Commented Nov 18, 2022 at 11:41
  • 1
    I guess what you want here is an componentDidMount ... my bad thought a function component - A great visual reference here Commented Nov 18, 2022 at 11:42
  • Thanks a ton @ManirajMurugan componentDidMount worked. Commented Nov 18, 2022 at 12:10
  • 1
    Thanks for the visual reference you've shared @KcH. It'll be of great help. :) Commented Nov 18, 2022 at 12:11

2 Answers 2

1
  class Component extends React.Component{
      constructor(...args){
        super(args);
        this.ref = React.createRef();
      }
      state = {
        isLoaded = false;
      }
      componentDidMount(){
          this.setState({isLoaded:true})     
        }
      someFunction = () => {
         let element = this.myRef.current;
         console.log(element)
      }
      render(){
        <FlexView>
          <div ref={this.ref}>
            some text here 
          </div>
          <button onClick={this.someFunction}>click-here</button>
       </FlexView>
      }
        
    }
Sign up to request clarification or add additional context in comments.

Comments

0

My guess is that in the first case the function is expected to return some html i.e div and since the function does not return anything it returns null and nothing is executed inside the render.

4 Comments

Yeah! But in both the cases, function get's executed. While in the first case, it's printing the proper div on console. But in the second case it's returning null as it doesn't able to read the element properly.
yes you are right . This is strange why it doesnt work here .
Hmm yeahhh! However, componentDidMount worked for me in this. It works now. Thanks you so much @Tarmah
great to know :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.