Goal:
Display the total sum of all id value after the text "Total sum of all id value is:"
The id value is from the API data.
Problem:
I do not know how to do it and is it possible to do it?
Info:
I'm a newbie in React JS
Stackblitz:
https://stackblitz.com/edit/react-f7chuv?
Thank you!
import React, { Component } from 'react';
import { render } from 'react-dom';
import './style.css';
class App extends Component {
constructor() {
super();
this.state = {
name: 'React',
data: null
};
}
componentDidMount = () => {
fetch('https://jsonplaceholder.typicode.com/users')
.then(res => res.json())
.then(data => {
this.setState(prevState => ({
...prevState,
data
}));
});
};
render() {
return (
<div>
<p>Start editing to see some magic happen :)</p>
<ul>
{this.state.data &&
this.state.data.map(user => (
<li key={user.id} id={user.id}>
{user.id}
</li>
))}
</ul>
<div>
Total sum of all id value is:
</div>
</div>
);
}
}
render(<App />, document.getElementById('root'));