Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React app componentDidMount not getting props from parent

I'm trying to pass props from a parent component to a child component and even though its getting called twice (dunno why, componentDidMount should get called only once) the props seem to be empty.

Parent component:

class Members extends Component{
    constructor(props){
        super(props);
        this.state = {
            interests: []
        }
    }

    componentDidMount(){
        fetch(interestUrl, {
            method: 'GET',
            headers: {
              "Content-Type": "application/json",
              "Authorization": this.props.authToken
            }
        })
        .then((response) => response.json())
        .then((json) => {this.setState({interests: json})})
        .catch(error => {console.log("Error: " + error)})
    };

    render(){
        return(
            <div className="Members-body">
                <div className="Menu-sidebar">
                    <Menu interestList = {this.state.interests}/>
                </div>
                <div className="Main-container">
                    <Main/>
                </div>
            </div>
        )
    }

}
export default Members;

Child component:

class Menu extends Component {
    constructor(props){
        super(props);
    }

    componentDidMount(){
        console.log("interestList: "  + this.props.interestList);
    }

    render() {
        return(
            <div className="Menu-container">
                este es el menu de la aplicacion
            </div>
        )
    }
}

export default Menu;

The console log from componentDidMount inside Menu component prints:

interestList: 
interestList: 

Any ideas to point me in the right direction? Greatly appreciated!

like image 234
Dieguinho Avatar asked Apr 30 '18 05:04

Dieguinho


People also ask

Does componentDidMount have access to props?

props is available in the entire class.

How do I pass Props to componentDidMount?

Or you could directly seed the state or if you don't even need to set the state, just pass the props directly to the function. You shouldn't declare a function inside render as it makes a new copy of it every time the component renders but in this case, you need to change the state or data every time the props change.

How do you get props from parent to child in React?

To pass data from a child component to its parent, we can call a parent function from the child component with arguments. The parent function can be passed down to the child as a prop, and the function arguments are the data that the parent will receive.

How to pass all props from parent to child component in react?

How to pass all props from parent to child component in React? Props can be passed from parent to child components explicitly as attributes or using spread operator ... in JSX Let’s explore both the ways. To begin the tutorial we have created following App and Image components In App component we have array of images.

Why are props read-only in react?

This behavior makes sense because the purpose of props is just to pass data from one component to another, i.e., only from a parent component to a child component. If we used only props and passed them from one component to another, they wouldn’t make our component interactive as we cannot change the props. Remember, props are read-only.

Why can’t I pass the button component props to the app component?

Since the Button component is managing the isDisplayed property, it is not possible to pass it up to the parent as props to our App component. The App component uses the isDisplayed property for conditionally rendering the HelloReact component. Our app wouldn’t work this way at the moment.

What is componentdidmount in react JS?

ReactJS componentDidMount () Method. The componentDidMount () method allows us to execute the React code when the component is already placed in the DOM (Document Object Model). This method is called during the Mounting phase of the React Life-cycle i.e after the component is rendered.


1 Answers

For the answer look at @azium's comments:

no it shouldn't show in the console precisely because componentDidMount only runs once, when the component mounts. when Members component calls setState and pass new props to Menu, it has already mounted so that function and your console log won't be called again with the populated array. you can test this easily by moving your console log into the render function
The component is receiving the props before being created, but the prop is an empty array. You explicitly say that in your Members constructor. Then after Members mounts you call setState which triggers another render with the populated array. Read the docs for a full explanation/

like image 172
Dieguinho Avatar answered Nov 15 '22 07:11

Dieguinho