I am still fairly new to React. I am trying to map the contents of an array in ReactJS. Currently, I am having no results render. Here is the section of code I am using to map the array contents:
return (
<div>
{this.state.errors.map( (error, index) =>
<div key={index} className="alert alert-danger" role="alert">
{error}
</div>
)}
</div>
)
Here is how I am initializing the state:
this.state = {
workoutId:'',
daysOfWeek:[],
name:'',
trainingPlan:'',
errors: []
}
And here is where I am populating the errors array:
if(this.state.daysOfWeek.length < 1) {
this.state.errors.push('Must select at least 1 day to perform workout');
}
if(this.state.workoutId === '') {
this.state.errors.push('Please select a workout to edit');
}
console.log(this.state.errors);
I am seeing that the array is being populated with those strings on the console.log. My understanding of React is that the component will re-render itself when changes are made. Is that correct? Anyway, can someone explain to me why my array contents will not render? I have tried mapping based on if the length of the errors array is greater than 0, which did not work either.