Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialAri Misha
19,323 PointsAJAX calls
Hiya community! What are different ways or patterns to implement AJAX calls in React or Vue frameworks? One pattern i've noticed is implementing AJAX calls and promises in Store , but it has its limitations. So any help would be awesome! Thank you!
~ Ari
1 Answer
Michael Liendo
15,326 PointsI can't speak on Vue, but in React, it's best to have your call in componentDidMount
.
example
import React, {Component} from 'react'
class Header extends Component {
state = {
repoName= ''
}
componentDidMount() {
fetch('https://api.github.com/users/model3volution/repos')
.then(res => res.json())
.then(data => {
const repoName = data[0].name
this.setState({repoName})
}
}
render() {
return (
<div>
Name of repo: {this.state.repoName}
</div>
)
}
I'm not exactly sure what you mean by "patterns" or "limitations" but if you drop a comment, I'll be sure to get back to you.