In the below code snippet, I have to pass the method 'handleChange' to another component 'TodoList'. I have tried binding the method, but it didn't solve the problem.
import React from "react";
import TodoList from "./TodoItem";
import "./TodoStyles.css";
import {list} from "./TodoList"
class TodoApp extends React.Component {
constructor(){
super();
this.state = {
todos : list
}
this.handleChange = this.handleChange.bind(this);
}
handleChange(id){
console.log("Changed",id);
}
render(){
const todoArr = this.state.todos.map(
function(i){
return <TodoList key = {i.id} arr = {i} handleChange = {this.handleChange}/>
}
);
return(
<div className = 'todo-list'>
{todoArr}
</div>
)
}
}
export default TodoApp
But using an arrow function instead of a regular function,it solves the issue.
const todoArr = this.state.todos.map(
(i) => {
return <TodoList key = {i.id} arr = {i} handleChange = {this.handleChange}/>
}
);
My question is, can we pass the function "handleChange" to other component using the regular function?
I am working in ES6 and react for the past 1 week, I have tried to search for a solution online, but didn't get any that can solve my problem. So, please be gentle.