Question: I'm trying to conditionally hide a `DatePicker` in SwiftUI. However, I'm having any issue with mismatched types:

```
var datePicker = DatePicker($datePickerDate)
if self.showDatePicker {
    datePicker = datePicker.hidden()
}

```

In this case, `datePicker` is a `DatePicker<EmptyView>` type but `datePicker.hidden()` is a `_ModifiedContent<DatePicker<EmptyView>, _HiddenModifier>`. So I cannot assign `datePicker.hidden()` to `datePicker`. I've tried variations of this and can't seem to find a way that works. Any ideas?

**UPDATE**

You can unwrap the `_ModifiedContent` type to get the underlying type using it's `content` property. However, this doesn't solve the underlying issue. The `content` property appears to just be the original, unmodified date picker.

Answer: 
✅ The correct and Simplest Way:
===============================

You can set the alpha instead, this will preserve the layout space of the view too and does not force you to add dummy views like the other answers:

```
.opacity(isHidden ? 0 : 1)

```

### Demo

[![Demo](https://i.stack.imgur.com/7EgSY.gif)](https://i.stack.imgur.com/7EgSY.gif)

---

 Cleaner Way! - Extend original `hidden` modifier:
==================================================

Also, you can implement a custom function to get the visibility state as an argument:

```
extension View {
    func hidden(_ shouldHide: Bool) -> some View {
        opacity(shouldHide ? 0 : 1)
    }
}

```

Now just pass the `bool` to the modifier:

```
DatePicker($datePickerDate)
    .hidden(showDatePicker)

```

**Note that** unlike the original behavior of the `hidden` modifier, both of these methods preserve the frame of the hiding view.

---

⛔️ Don't use bad practices !!!
==============================

All other answers (including the accepted answer by @Jake) use branches instead of dependent code that cause a performance hit.

###  Branch example:

[![Branch](https://i.stack.imgur.com/xfgzt.png)](https://i.stack.imgur.com/xfgzt.png)

### ✅ Dependent Code example:

[![Dependent Code example](https://i.stack.imgur.com/IpWIp.png)](https://i.stack.imgur.com/IpWIp.png)

Returning *logical **SAME** view* for different states causes the SwiftUI to render engine to re-render and initial a view again and cause a performance hit! (see more at **[this WWDC session](https://developer.apple.com/wwdc21/10022)**)
The bad part of the above solution `.isHidden(true, remove: true)` is onAppear callback will not be called when `remove = true`.

`.opacity(isHidden ? 0 : 1)` is definitely the right way.