React-createRef() Api - this.child.Current is null

Asked

Viewed 128 times

2

In order to allow mine parent component access values of my child component (Display), I started working with the createRef() API that came out in this patch 16.3.

Following the example "Adding a Ref to a Class Component" which is in this documentation i made the following changes to my code:

class JsonFetcher extends Component {
    constructor(props) {
        super(props);
        this.child = React.createRef();
        this.state = {
            data: [],
        }
    }

    componentDidMount() {   
        this.updateContent(this.props.mainUrl)
    }

    updateContent(mainUrl){
        fetch(mainUrl)
            .then((responseJsonAnyUrl) => responseJsonAnyUrl.json())
            .then((responseJsonAnyUrl) => {
                this.setState({
                    mainUrl: mainUrl,
                    jsonObject: responseJsonAnyUrl
                },
                function () {
                    this.timeout = setTimeout(
                        function(){
                            //let ind = this.child.current.getCurrentIndex
                            //tyring to get values from child...
                            //if index === length-1
                            this.updateContent(mainUrl)
                            //else

                            //retry this function in 5 seconds
                            // 
                        }.bind(this, mainUrl)
                        ,
                        20000)
                }.bind(this))
            })
    }

    interpretJson() {
        /*
        *some code
        */            
        return(
            <Display content={contentListArray} ref={this.child}/>
        )
    }

    render() {
        if(this.state.jsonObject){
            return (
                <div>
                    <div> {this.interpretJson()} </div>
                </div>
            )
        }else
            return(
                null
            )                
    }
}

Therefore, I create the ref in the constructor, associate it with Child Component Display at the end function interpretJson() and try to use a method of child in my role of timeOut(), but I get the error:

"Typeerror: Cannot read Property 'getCurrentIndex' of null "

What am I doing wrong so I can’t call the methods of Display in order to simulate the pseudo-code I have in?

EDIT: Here is the Display

class Display extends Component{
    constructor(props){
        console.log('Display constructor was called')
        super(props);
        this.state = {
            contentIndex: -1
        }
    this.switchContent = this.switchContent.bind(this)
    }

    /*some methods */

    getCurrentIndex(){
        return this.state.contentIndex
    }

    getZoneLengths(){
        let zoneLengths = []
        this.props.content.map((zoneX, index) => {
            zoneLengths.push(zoneX.length)
        })        
        return zoneLengths

    }

     render(){ /* some code */ }
  • How the component was defined Display? Also include it in the question. Citing the documentation, it will not work if your component is functional.

  • Done, thank you

  • Note that in the documentation the ref={...} always comes in the render, and I see that in your example is in interpretJson.

  • I managed to change the 'Display' call to render it to the error persisted

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.