pixijs / pixi-react

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

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

Closed sergioisidoro closed 4 months ago

sergioisidoro commented 5 months 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 4 months ago

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