Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

151
Views
How to connect two components in a same component created on click of add button

I am creating multiple counters on click and I want to connect the two counters, When I increase the value in the first counter it should automatically decrease in the second counter. Can you suggest any solution where I can communicate with multiple counters as I can generate multiple counters on click?

const Counter = ({ value, onIncrement, onDecrement, hideIncrement }) => {
      return (
        <div>
          <span>{value}</span>
          {value > 0 && (
            <button
              onClick={() => {
                onDecrement();
              }}
            >
              -
            </button>
          )}
          {hideIncrement === false && value < 10 && (
            <button
              onClick={() => {
                onIncrement();
              }}
            >
              +
            </button>
          )}
        </div>
      );
    };
    
    const Counters = () => {
      const [counters, setCounters] = React.useState([4, 0, 0, 5]);
    
      const sum = counters.reduce((acc, item) => acc + item, 0);
    
      return (
        <div>
          <p>Sum: {sum}</p>
          <button
            onClick={() => {
              setCounters([...counters, 0]);
            }}
          >
            Add counter
          </button>
          <br />
          <div>
            {counters.map((value, index) => (
              <Counter
                value={value}
                hideIncrement={sum >= 20}
                onIncrement={() => {
                  const countersCopy = [...counters];
                  countersCopy[index] += 1;
                  setCounters(countersCopy);
                }}
                onDecrement={() => {
                  const countersCopy = [...counters];
                  countersCopy[index] -= 1;
                  setCounters(countersCopy);
                }}
              />
            ))}
          </div>
        </div>
      );
    };
    
    const rootElement = document.getElementById("root");
    ReactDOM.render(<Counters />, rootElement);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.1/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.1/umd/react-dom.production.min.js"></script>
<div id="root"></div>

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can pass state to Couter component: try this:

const Counter = ({value, onIncrement, onDecrement, hideIncrement, 
addStatus, counterIndex, index}) => {
    useEffect(() => {
        if (counterIndex === index - 1) onDecrement(addStatus);
    }, [counterIndex, addStatus])
    return (
        <div>
            <span>{value}</span>
            {value > 0 && (
                <button
                    onClick={() => {
                        onDecrement();
                    }}
                >
                    -
                </button>
            )}
            {hideIncrement === false && value < 10 && (
                <button
                    onClick={() => {
                        onIncrement();
                    }}
                >
                    +
                </button>
            )}
        </div>
    );
};

const Counters = () => {
    const [counters, setCounters] = React.useState([4, 4, 4, 5]);
    const [addStatus, setAddStatus] = useState(0);
    const [counterIndex, setCounterIndex] = useState(0);
    const sum = counters.reduce((acc, item) => acc + item, 0);
    return (
        <div>
            <p>Sum: {sum}</p>
            <button
                onClick={() => {
                    setCounters([...counters, 0]);
                }}
            >
                Add counter
            </button>
            <br/>
            <div>
                {counters.map((value, index) => (
                    <Counter
                        value={value}
                        index={index}
                        addStatus={addStatus}
                        counterIndex={counterIndex}
                        hideIncrement={sum >= 20}
                        onIncrement={() => {
                            const countersCopy = [...counters];
                            countersCopy[index] += 1;
                            setAddStatus(a => a + 1)
                            setCounterIndex(index)
                            setCounters(countersCopy);
                        }}
                        onDecrement={() => {
                            const countersCopy = [...counters];
                            countersCopy[index] -= 1;
                            setCounters(countersCopy);
                        }}
                    />
                ))}
            </div>
        </div>
    );
};
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!