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!
props is available in the entire class.
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.
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? 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.
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.
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.
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.
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. whenMembers
component callssetState
and pass new props toMenu
, 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 therender
function
The component is receiving the props before being created, but the prop is an empty array. You explicitly say that in yourMembers
constructor. Then afterMembers
mounts you callsetState
which triggers another render with the populated array. Read the docs for a full explanation/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With