How to fetch data asynchronously inside component and pass it as props to another component in render?

code83

I have a react app and I would like to fetch some data asynchronously, process it, change the current components state, and then pass it as props to another component in the render. I tried fetching it in componentWillMount, but seems like the render still happens before data is fetched. What is a working solution for that? I also tried fetching the data in a ES6 contructor, and the issue still persists.

Any help is greatly appreciated!

Shubham Khatri

Well an ideal place to fetch the data would be the componentWillMount function, however because of the async nature your child component may get rendered before the data is fetched and hence you can do two things.

Maintain a loading state that doesn't render the component till the result is fetched, like:

constructor() {
   super();
   this.state = {
        isLoading: true,
        // other states
   }
}

componentWillMount() {
    //your async request here
}

render() {
   if(this.state.isLoading) {
       return null; // or you can render laoding spinner here
   } else {
       return (
           //JSX here with the props
       )
   } 
}

Other way is to have an empty prop and perform for a check in the child component:

constructor() {
    super();
    this.state = {
        someProps: null; 
    }
} 

componentWillMount() {
     //your async request here
}

render() {

    return (
         <Child someProps={this.state.someProps}/>
    )    
}

Child

render() {

  if(this.props.someProps == null) 
        return null;
  else {
      return (//JSX contents here);
  }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to pass a component type to render to another component in Angular?

From Dev

VueJs: Pass Dynamic Components as props to another Component and render them

From Dev

How to pass props from component to another component

From Dev

How to pass the props data to another component correctly in Vue.js

From Dev

How to pass data from a component to another component

From Dev

React render component asynchronously, after data is fetched

From Dev

Fetch data into React component and render it

From Dev

React: How to pass State value to another component and use it to render a component

From Dev

React how to pass a component with its props to another component

From Dev

Pass props from a Component to another Component on React

From Dev

Redux - Fetch data, but render in another component

From Dev

Pass a component to another inside a props array

From Dev

How to pass props to form in another component in Vue

From Dev

Dynamically render a component inside a another component in React

From Dev

How to pass a component as props to another component in react js?

From Dev

Pass data to another component

From Dev

How can i pass a component to another component via props

From Dev

How to pass image url to another component as props?

From Dev

How to render react component inside function in another component?

From Dev

How to pass data to component to another component

From Dev

How to pass props to styled component inside a loop

From Dev

How to display props and render on the view from component to another?

From Dev

React Native - How to pass data to component that's an attribute inside another component

From Dev

How to call a react component from another component and pass required props

From Dev

How to pass props to component

From Dev

How to pass Boolean props to another component in vuejs

From Dev

How to Fetch Data inside SvelteKit Component that Is Not a Page

From Dev

How to pass mui-icon component as props to another component?

From Dev

How to pass props to a component

Related Related

  1. 1

    How to pass a component type to render to another component in Angular?

  2. 2

    VueJs: Pass Dynamic Components as props to another Component and render them

  3. 3

    How to pass props from component to another component

  4. 4

    How to pass the props data to another component correctly in Vue.js

  5. 5

    How to pass data from a component to another component

  6. 6

    React render component asynchronously, after data is fetched

  7. 7

    Fetch data into React component and render it

  8. 8

    React: How to pass State value to another component and use it to render a component

  9. 9

    React how to pass a component with its props to another component

  10. 10

    Pass props from a Component to another Component on React

  11. 11

    Redux - Fetch data, but render in another component

  12. 12

    Pass a component to another inside a props array

  13. 13

    How to pass props to form in another component in Vue

  14. 14

    Dynamically render a component inside a another component in React

  15. 15

    How to pass a component as props to another component in react js?

  16. 16

    Pass data to another component

  17. 17

    How can i pass a component to another component via props

  18. 18

    How to pass image url to another component as props?

  19. 19

    How to render react component inside function in another component?

  20. 20

    How to pass data to component to another component

  21. 21

    How to pass props to styled component inside a loop

  22. 22

    How to display props and render on the view from component to another?

  23. 23

    React Native - How to pass data to component that's an attribute inside another component

  24. 24

    How to call a react component from another component and pass required props

  25. 25

    How to pass props to component

  26. 26

    How to pass Boolean props to another component in vuejs

  27. 27

    How to Fetch Data inside SvelteKit Component that Is Not a Page

  28. 28

    How to pass mui-icon component as props to another component?

  29. 29

    How to pass props to a component

HotTag

Archive