.if(let:then:)

extension View {
    /// usage: `view.if(let: value) { (view, value) in ... }`
    @ViewBuilder func if<Value, Content: View>(
        `let` value: Value?, 
        @ViewBuilder then modifySelfWithValue: (Self, Value) -> Content 
    ) -> some View {
        if let value = value { modifySelfWithValue(self, value) } 
        else { self }
    }
}
  • ็”จๆณ•๏ผš

//  type:  `Value?`         `Value`
//             โ†“               โ†“
view.if(let: value) { (view, value) in
    // modify the view with value (โญ๏ธ @ViewBuilder closure)
}

Last updated