Khurshid Ansari
Khurshid Ansari

Reputation: 5095

How to remove child component in react native

I am using react native mapbox and have several and dynamic child component(layers) in Map component.

Something like:

<MapboxGL.MapView ...>
   <MapboxGL.RasterSource>
   ...
   </MapboxGL.RasterSource>
  ...
</MapboxGL.MapView>

There so many child component dynamic and conditonal.

I want to remove unwanted component because update some component doest not support.

So any suggestion or idea

Upvotes: 1

Views: 215

Answers (2)

Gaurav
Gaurav

Reputation: 1

const Component = ()=>{
  const [showComponent, setShowComponent] = useState(false);

  return (
    <MapboxGL.MapView ...> 
      
      {showComponent && (
          <View>{.....}</View>
          : null }
      
    </MapboxGL.MapView>
  )
}

Upvotes: 0

Craques
Craques

Reputation: 1143

You can use state and conditionally render your items depending on the state e.g

const Component = ()=>{
  const [isVisible, setIsVisible] = useState(false);
  ...
  ...
  ...
  return (
    <MapboxGL.MapView ...>
      {...}
      // you can shortcircuit here
      {isVisible && <DynamicComponent>}
      // this should also work
      {isVisible ? <AnotherComponent/> : null }
    </MapboxGL.MapView>
  )
}

Upvotes: 1

Related Questions