Hi Guys,
when I try to lift the state from Counters to App and I save i see the following error in the dev tool:
Uncaught TypeError: Cannot read properties of undefined (reading ‘map’)
It looks like my properties from Counters component in the App class is empty (I have tried to log it in the console unsuccessfully). I tried to reverse the change and everything works fine.
Please find below my App Class:
import NavBar from ‘./components/navbar’;
import ‘./App.css’;
import Counters from ‘./components/counters’;
import React, { Component } from ‘react’;
class App extends Component {
state = {
counters: [
{ id: 1, value: 1 },
{ id: 2, value: 0 },
{ id: 3, value: 0 },
{ id: 4, value: 1 },
],
};
handleDelete = (counterId) => {
// console.log("Delete button clicked", counterId);
const counters = this.state.counters.filter((c) => c.id !== counterId);
this.setState({ counters });
};
handleReset = () => {
const counters = this.state.counters.map((c) => {
c.value = 0;
return c;
});
this.setState({ counters });
};
handleIncrement = (counter) => {
const counters = [...this.state.counters];
const index = counters.indexOf(counter);
counters[index] = { ...counter };
counters[index].value++;
this.setState({ counters });
};
render(){
return (
<React.Fragment>
<NavBar/>
<main className='container'>
<Counters
counters = {this.state.counters}
onReset = {this.handleReset}
onIncrement = {this.handleIncrement}
onDelete = {this.handleDelete}
/>
</main>
</React.Fragment>
);}
}
export default App;
Here my Counters class:
import React, { Component } from “react”;
import Counter from “./counter”;
class Counters extends Component {
render() {
return (
<div>
<button
onClick={this.props.onReset}
className="btn btn-primary btn-sm m-2"
>
Resets
</button>
{this.props.counters.map((counter) => (
<Counter
key={counter.id}
onDelete={this.props.onDelete}
counter={counter}
onIncrement={this.props.onIncrement}
/>
))}
</div>
);
}
}
export default Counters;
Please help me understand how can I have the “this.props.counters.map” to not fail