import type { Fiber } from 'react-reconciler' ; import { Env } from '../env/env' ; import { getFiberFromPath } from '../fiber/getFiber' ; import { removeChildFiber , removeChildFiberAt } from '../fiber/removeFiber' ; import { updateFibersIndex } from '../fiber/updateFiber' ; import { invariant } from '../invariant' ; import { warning } from '../warning' ; param parent param childSelector param skipUpdate returns export function removeChild ( parent , childSelector , skipUpdate ? ) { invariant ( typeof childSelector !== 'number' || childSelector >= 0 , `The index provided to remove the child must be ` + `greater than or equal to 0, found: ${ childSelector }.` ) ; let child = null ; if ( typeof childSelector === 'number' ) { child = removeChildFiberAt ( parent , childSelector ) ; } else { child = removeChildFiber ( parent , childSelector ) ; } if ( child === null ) { if ( __DEV__ ) { if ( typeof childSelector === 'number' ) { warning ( `Cannot find and remove the child at index: ${ childSelector }.` ) ; } else { warning ( `No child with the key: '${ childSelector }' has been found, ` + `the child cannot be removed.` ) ; } } return null ; } if ( child . sibling !== null ) { updateFibersIndex ( child . sibling , child . index ) ; } if ( child . alternate !== null && parent . alternate !== null ) { let alternate = null ; if ( typeof childSelector === 'number' ) { alternate = removeChildFiberAt ( parent . alternate , childSelector ) ; } else { alternate = removeChildFiber ( parent . alternate , childSelector ) ; } invariant ( alternate !== null , 'The alternate child has not been removed. ' + 'This is a bug in React-reparenting, please file an issue.' ) ; if ( alternate . sibling !== null ) { updateFibersIndex ( alternate . sibling , alternate . index ) ; } } if ( skipUpdate ) { return child ; } const containerFiber = getFiberFromPath ( parent , ( fiber ) => fiber . return , ( fiber ) => Env . isElement ( fiber . elementType , fiber . stateNode ) ) ; const elementFiber = getFiberFromPath ( child , ( fiber ) => fiber . child , ( fiber ) => Env . isElement ( fiber . elementType , fiber . stateNode ) ) ; if ( containerFiber === null ) { if ( __DEV__ ) { warning ( 'Cannot find the container element, neither the parent nor any ' + 'component before it seems to generate an element instance. ' + 'You should manually send the element and use the `skipUpdate` option.' ) ; } return child ; } if ( elementFiber === null ) { if ( __DEV__ ) { warning ( 'Cannot find the child element. ' + 'You should manually send the element and use the `skipUpdate` option.' ) ; } return child ; } const container = containerFiber . stateNode ; const element = elementFiber . stateNode ; Env . removeChildFromContainer ( container , element ) ; return child ; }