Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

154
Views
React expose component function and getting data with ComponentDidMount

This is regarding a question that I have just asked here : React expose component function

So when using componentDidMount on the 2 components where the functions are exposed, it looks like the componentDidMount for the data function takes some time to load, and then this causes returning an empty array. I am pretty new to react so I am not sure if this is the right way to use them.

class Data extends React.Component {
    constructor() {
        super();
        this.state = {
            names: []
        }
    }
    componentDidMount() {
        $.get('data.json', function (result) {
            this.setState({
                names: result.names
            });
        }.bind(this));
    }

    getNames(){
        return this.state.names;
    }

    render(){
        return (<div></div>);
    }

}


class Layout extends React.Component {
    constructor(){
        super();
        this.state = {
            test: []
        };

    }

    componentDidMount() {
        this.state.test = this.refs.hello.getNames();
        console.log(this.refs.hello.getNames());
    }

    something(){
        console.log(this.state.test);
    }


    render(){
        return(
            <div>
                <Data ref='hello' />
                {this.something()}
            </div>
        )
    }
}

const app = document.getElementById('app');

ReactDOM.render(<Layout />,  app);
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

You have done a mistake of assigning value to a state in the layout component like this.state.test = this.refs.hello.getNames();, where you should be using setState(); Also it better to retrieve the data in the Layout component and if you want to use the data in Data component too then you can pass it as a prop to the same like

class Data extends React.Component {
    constructor() {
        super();
    }
    render(){
        console.log(this.props.names)
        return (<div></div>);
    }

}


class Layout extends React.Component {
    constructor(){
        super();
        this.state = {
            test: []
        };

    }

    componentDidMount() {
        $.get('data.json', function (result) {
            this.setState({
                test: result.names
            });
        }.bind(this));

    }

    something(){
        console.log(this.state.test);
    }


    render(){
        return(
            <div>
                <Data ref='hello' names={this.state.test}/>
                {this.something()}
            </div>
        )
    }
}

const app = document.getElementById('app');

ReactDOM.render(<Layout />,  app);
over 4 years ago · Santiago Trujillo Report

0

The problem here is that you are fetching in componentDidMount, you should be fetching in componentWillMount, that way you have the data before the component is rendered

Per the docs:

componentWillMount() is invoked immediately before mounting occurs. It is called before render(), (therefore setting state in this method will not trigger a re-rendering. Avoid introducing any side-effects or subscriptions in this method.)

All you need to do is change componentDidMount to componentWillMount

https://facebook.github.io/react/docs/react-component.html#componentwillmount

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!