D
D
doooktooor2021-12-09 21:09:10
React
doooktooor, 2021-12-09 21:09:10

How to pass handleIncrement/handleDecrement to another file's function via props?

I have 2 files first

const handleIncrement = (value) => {
    let countersIncrement = counters.map((i) => {
      return (i.value = value + 1);
    });
    setCounters(countersIncrement);
  };

  const handleDencrement = (value) => {
    let countersDencrement = counters.map((d) => {
      return (d.value = value - 1);
    });
    setCounters(countersDencrement);
  };
  return (
    <>
      {counters.map((count) => {
        return (
          <Counter
            key={count.id}
            onDelete={handleDelete}
            onIncrement={handleIncrement}
            onDecrement={handleDencrement}
            {...count}
          />

Second
const handleIncrement = () => {
    props.onIncrement(props.value);
    // setValue((prevState) => prevState + 1);
  };

  const handleDencrement = () => {
    props.onDencrement(props.value);
    // setValue((prevState) => prevState - 1);
  };

  return (
    <div>
      <span>{props.name}</span>
      <span className={getBageClasses()}>{formatValue()}</span>
      <button
        className="btn btn-primary btn-sm m-2"
        onClick={() => props.onIncrement(props.value)}
      >
        +
      </button>
      <button
        className="btn btn-primary btn-sm m-2"
        onClick={() => props.onDencrement(props.value)}
      >
        -
      </button>

Here are their parts, I don’t understand how to pass through props so that by pressing one it increases, and by pressing the second it decreases the value from the array with objects only for the value field
[
    { id: 0, value: 0, name: "Ненужная вещь" },
    { id: 1, value: 4, name: "Ложка" },
    { id: 2, value: 0, name: "Вилка" },
    { id: 3, value: 0, name: "Тарелка" },
    { id: 4, value: 0, name: "Набор минималиста" },
  ];


It turned out only that all values ​​​​increased simultaneously and then an error

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question