M
M
Miracle Live @ Miracle2021-01-27 19:28:57
JavaScript
Miracle Live @ Miracle, 2021-01-27 19:28:57

Cannot read property 'bind' of undefined. React.js how to fix?

I get this error TypeError: Cannot read property 'bind' of undefined

import React, { useContext } from "react";
import PropTypes from "prop-types";
import Context from "../ToDo/context";

const styles = {
  li: {
    display: "flex",
    justifyContent: "space-between",
    alignItmes: "center",
    padding: ".5rem 1rem",
    border: "1px solid #ccc",
    borderRadius: "4px",
    marginBottom: ".5rem",
  },
  input: {
    marginRight: "1rem",
  },
};

function ToDoItem({ todo, index, onChange }) {
  const { removeTodo } = useContext(Context);
  const classes = [];
  if (todo.completed) {
    classes.push("done");
  }
  return (
    <li style={styles.li}>
      <span className={classes.join(" ")}>
        <input
          type="checkbox"
          checked={todo.completed}
          style={styles.input}
          onChange={() => onChange(todo.id)}
        />
        <strong>{index + 1}</strong>
        &nbsp;
        {todo.title}
      </span>

      <button className="rm" onClick={removeTodo.bind(null, todo.id)}>
        &times;
      </button>
    </li>
  );
}

ToDoItem.propTypes = {
  todo: PropTypes.object.isRequired,
  index: PropTypes.number,
  onChange: PropTypes.func.isRequired,
};

export default ToDoItem;

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vlad, 2021-01-27
@vchpro

Make a class component and get rid of the bind by including babel and creating an arrow function.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question