Skip to content
Advertisement

React – Ajax data not being passed into Child Component

I have two components, one parent one child. I am using the fetch method in componentDidMount() callback. Once I do this, I set the state with key items to that data that is pulled from the api. Once I do this it should be able to be console logged in the child component as a prop. However this is not working. What am I doing wrong here?

Parent Component:

JavaScript

Child Component:

JavaScript

Advertisement

Answer

The constructor for a component only runs one time during a lifecycle. When it does, props.items is undefined because your ajax request is in-flight, so console.log(props.items) doesn’t show anything.

If you change your constructor to console.log("constructed");, you’ll see one-time output (stack snippets may not show this–look in your browser console). Henceforth, componentDidUpdate() can be used to see the new props that were set when your ajax request finishes.

You could also log the props inside the render method, which will run once before the ajax request resolves and again afterwards when props.items changes.

As a side point, you have <Map list=... but the component tries to render this.props.name, which is undefined.

Also, if you aren’t doing anything in the constructor (initializing state or binding functions) as here, you don’t need it.

JavaScript
JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement