I have a simple React code as shown below:
export class TodoList extends Component {
constructor(props) {
super(props)
console.log(this)
}
addTask() {
// why is this null
console.log(this.textInput)
}
render() {
return (
<div>
<input type="text" ref={(input) => {this.textInput = input}} />
<button onClick={this.addTask}>Add New Task</button>
<h6>Pending Tasks</h6>
<PendingTaskList />
</div>
)
}
}
Inside the addTask function the value of "this" is always null. I know I can fix this by binding it but my question is why is it null in the first place.