Reputation:
I am pretty new to Javascript and ReactJS and got stuck while trying to make my code make API calls everytime the state gets changed.
I have two classes Searchform(parent) and DisplayData (child) .
Inside the DisplayData class, I have a componentDidMount method which makes a call to the GitHub API, and the returned data is displayed.
I pass the state of the Searchform class to DisplayData as props and wish for the API calls to be redone(thereby changing the displayed data).
However, while the props are being passed properly every time the state of the parent class changes, I can't seem to redo the API calls in the child class everytime this happens. Any help would be appreciated.
Upvotes: 0
Views: 230
Reputation: 2755
Use the componentDidUpdate
lifecycle method: https://reactjs.org/docs/react-component.html#componentdidupdate
This will probably look very similar to your componentDidMount
:
class DisplayData extends React.Component {
componentDidMount() {
... AJAX stuff
}
componentDidUpdate(prevState, prevProps) {
... AJAX suff
}
....
}
Upvotes: 2