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

142
Views
Object key of past elements of states get rewritten when new state added to array of states

I have this strange issue that when I set a new state using spread operator only one object key get rewritten.

useEffect(() => {
    props.setCanvas([...props.canvas, {
                    block: 1,
                    innerBlock: !props.canvas.length ? 1 : (innerBlockChange(props.drop.blockType, props.canvas[props.canvas.length - 1].blockType) ? props.canvas[props.canvas.length - 1].innerBlock + 1 : props.canvas[props.canvas.length - 1].innerBlock),
                    blockType: (props.drop.blockType == 'center') ? 'center' : 'double',
                    class: 'placeholders',
                    position: props.drop.blockType,
                    size: 'regular',
                    entity: props.drag.element, // this one get rewritten
                    index: !props.canvas.length ? 1 : props.drop.main.querySelector('section.block:not(.mockup)').querySelectorAll('.placeholders').length + 1,
                    indexOfType: props.canvas.filter(el => el.entity = props.drag.element).length + 1,
                    connections: [],
                    properties: [],
                    comments: [],
                    embed: false
                }]);
},[props.drag]);

The entity key don't keep the stored value and changes when setState get executed again.

For example if for the first element it set to A When the setState run for second time with new entity value of B the first element of canvas states changes to B as well.

I tried to use a completely separate state (not the props.drag) for it but the issue still exist.

about 4 years ago · Juan Pablo Isaza
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!