Hello @kartik,
You should populate data with AJAX calls in the componentDidMount lifecycle method. This is so you can use setState to update your component when the data is retrieved.
Example from react documentation
class MyComponent extends React.Component {
constructor(props) {
super(props);
this.state = {
email: '',
userName: '',
userID: '',
error: null,
isLoaded: false
};
}
componentDidMount() {
fetch("http://localhost:8080/user/1")
.then(res => res.json())
.then(
(result) => {
this.setState({
email: result.email,
userName: result.userName,
userID: result.userID
});
},
// Note: it's important to handle errors here
// instead of a catch() block so that we don't swallow
// exceptions from actual bugs in components.
(error) => {
this.setState({
isLoaded: true,
error
});
}
)
}
render() {
const { error, isLoaded, email, userName, userID } = this.state;
return (
<ul>
{email}
{userName}
{userID}
</ul>
);
}
}
Hope this work!
If you need to know more about React, Its recommended to join React JS course today.
Thank You!!