Skip to content

Commit ba460c3

Browse files
authored
Update README.md
1 parent 86442f9 commit ba460c3

File tree

1 file changed

+72
-1
lines changed

1 file changed

+72
-1
lines changed

README.md

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,4 +312,75 @@ export default Button;
312312

313313
```
314314

315-
5. Start your server `npm start` and check if your bootstrap import works as well as the test button!
315+
5. Start your server `npm start` and check if your bootstrap import works as well as the test button! If all is displaying then we are almost done. We are sooooo close!
316+
317+
We want our button to actually call the API now so lets create the function with the appropriate call. We want to add an onclick event to the button html like so:
318+
319+
```javascript
320+
<button class="btn btn-primary" onClick={this.callApi}>Test Call!</button>
321+
```
322+
323+
and our custom function PLUS the initial state set to null
324+
325+
```javascript
326+
327+
state = {
328+
329+
results: []
330+
}
331+
332+
callApi = async() => {
333+
334+
const api_call = await fetch('http://localhost:3000/api/v1/movies');
335+
336+
const data = await api_call.json();
337+
338+
this.setState({
339+
340+
results: data
341+
});
342+
343+
}
344+
345+
```
346+
347+
After copying and pasting both snippets inside the `Button.js` file, we fetch from the url `http://localhost:3000/api/v1/movies` in a json format and once we return a true response we will set the response to the data variable and console log the results into the browser.
348+
349+
This should be the complete Button.js file below!
350+
```javascript
351+
352+
import React, { Component } from 'react';
353+
354+
class Button extends Component {
355+
356+
callApi = async() => {
357+
358+
const api_call = await fetch('http://localhost:3000/api/v1/movies');
359+
360+
const data = await api_call.json();
361+
362+
console.log(data);
363+
}
364+
365+
366+
render() {
367+
return (
368+
<div>
369+
<div class="card container mt-3">
370+
<div class="card-body">
371+
<div class="row">
372+
<center>
373+
<button class="btn btn-primary" onClick={this.callApi}>Test Call!</button>
374+
</center>
375+
</div>
376+
</div>
377+
</div>
378+
</div>
379+
);
380+
}
381+
}
382+
383+
export default Button;
384+
385+
386+
```

0 commit comments

Comments
 (0)