the entire code is below… but I only have questions on some of the parts of it…
This is a sample code from one of the FreeCodeCamp YouTube Channel…
This section here:
Where do the props come from exactly?
and why is it props.exercise.username ? I would think the username comes from one of the fields on the collection list … but I am not clear on the logics of props.exercise.
const Exercise = props => (
<tr>
<td>{props.exercise.username}</td>
<td>{props.exercise.description}</td>
<td>{props.exercise.duration}</td>
<td>{props.exercise.date.substring(0,10)}</td>
<td>
<Link to={"/edit/"+props.exercise._id}>edit</Link> | <a href="#" onClick={() => { props.deleteExercise(props.exercise._id) }}>delete</a>
</td>
</tr>
)
The other section of the code is this below…
He is mapping this.estate.exercises which comes from the axios results , then he is returning Exercise which is the const just above with the props… what exactly is going on the rest of the code and how does it influence the const Exercise?
exerciseList() {
return this.state.exercises.map(currentexercise => {
return <Exercise exercise={currentexercise} deleteExercise={this.deleteExercise} key={currentexercise._id}/>;
})
}
Complete code here:
import React, { Component } from 'react';
import { Link } from 'react-router-dom';
import axios from 'axios';
const Exercise = props => (
<tr>
<td>{props.exercise.username}</td>
<td>{props.exercise.description}</td>
<td>{props.exercise.duration}</td>
<td>{props.exercise.date.substring(0,10)}</td>
<td>
<Link to={"/edit/"+props.exercise._id}>edit</Link> | <a href="#" onClick={() => { props.deleteExercise(props.exercise._id) }}>delete</a>
</td>
</tr>
)
export default class ExercisesList extends Component {
constructor(props) {
super(props);
this.deleteExercise = this.deleteExercise.bind(this)
this.state = {exercises: []};
}
componentDidMount() {
axios.get('http://localhost:5000/exercises/')
.then(response => {
this.setState({ exercises: response.data })
})
.catch((error) => {
console.log(error);
})
}
deleteExercise(id) {
axios.delete('http://localhost:5000/exercises/'+id)
.then(response => { console.log(response.data)});
this.setState({
exercises: this.state.exercises.filter(el => el._id !== id)
})
}
exerciseList() {
return this.state.exercises.map(currentexercise => {
return <Exercise exercise={currentexercise} deleteExercise={this.deleteExercise} key={currentexercise._id}/>;
})
}
render() {
return (
<div>
<h3>Logged Exercises</h3>
<table className="table">
<thead className="thead-light">
<tr>
<th>Username</th>
<th>Description</th>
<th>Duration</th>
<th>Date</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
{ this.exerciseList() }
</tbody>
</table>
</div>
)
}
}