pixijs / pixi-react

Write PIXI apps using React declarative style
https://pixijs.io/pixi-react/
MIT License
2.24k stars 172 forks source link

`Cannot insert node before itself` on conditional rendering #495

Closed sergioisidoro closed 7 hours ago

sergioisidoro commented 1 week ago

Current Behavior

When doing a conditional rendering that adds a new child node to a parent that already has children nodes, we get the error.

Uncaught Invariant Violation: Cannot insert node before itself
    invariant invariant.js:26
    insertBefore insertBefore.js:15
    insertOrAppendPlacementNode react-reconciler.development.js:15581
    commitPlacement react-reconciler.development.js:15525
    commitReconciliationEffects react-reconciler.development.js:16308

Expected Behavior

Same behaviour as a normal React renderer - allowing for this kind of case

Steps to Reproduce

const [selectedTool, setSelectedToo] = useState(null);
{/* ... */}
// ERROR
{someArray.map((train, index) => {
  return (
    <pixiContainer key={index} y={index * 100}>
      {/* Throws reconciliation error, when selected tool changes to 'move' */}
      { selectedTool == 'move' &&
        <pixiContainer x={-50}>
            <pixiSprite  eventMode='dynamic' texture={moveIcon}/>
        </pixiContainer>
      }
      {/* SOME OTHER COMPONENTS HERE - parent is never empty */}
    </pixiContainer>
    )
  })
}

Environment

Possible Solution

No response

Additional Information

Wrapping the entire condional rendering in a container seems to resolve the issue

// OK
{someArray.map((train, index) => {
  return (
    <pixiContainer key={index} y={index * 100}>
      {/* Works if I wrap the conditional rendering in a container */}
      <pixiContainer x={-50}>
        { selectedTool == 'move' && 
          <pixiSprite  eventMode='dynamic' texture={moveIcon}/>
        }
      </pixiContainer>
      {/* SOME OTHER COMPONENTS HERE - parent is never empty */}
    </pixiContainer>
    )
  })
}
trezy commented 7 hours ago

This is fixed in the latest dev build: 8.0.0-dev.500919f