fable-compiler / fable-react

Fable bindings and helpers for React and React Native
MIT License
275 stars 67 forks source link

Add nothing to react helpers #113

Closed vbfox closed 5 years ago

vbfox commented 5 years ago

React use null to signal that nothing should be rendered, it could be done before with ofOption None but this add clearer alternative to generate this.

let render props =
    let details =
        match props.Details with
        | Some d -> strong [] [str (sprintf " (%s)")] d
        | None -> nothing
    span [] [ str props.Name; details ]
alfonsogarciacaro commented 5 years ago

Nice one, thank you!

forki commented 5 years ago

awesome this reduces couple of yields for me. How can we make it mor popular? @theimowski something for the template?

theimowski commented 5 years ago

Yeah, maybe - created an issue

MangelMaxime commented 5 years ago

Just for reference you can also use Option.map to not use yield.

let private showFirstConnectionUrl (url : string) =
    Message.message [ Message.Color IsInfo ]
        [ Message.header [ ]
            [ str "Ce compte n'a pas encore été activé." ]
          Message.body [ ]
            [ CopyButton.copyButtton [ CopyButton.Value url ] ] ]

let private viewUserInfo (info : LoadedModel) dispatch =
    div [ ]
        [ info.OriginalData.FirstConnectionUrl
          |> Option.map showFirstConnectionUrl
          |> ofOption
          // More code... 
        ]
forki commented 5 years ago

Yes, but really like the explicit nothing function.

Am Di., 6. Nov. 2018, 11:05 hat Maxime Mangel notifications@github.com geschrieben:

Just for reference you can also use Option.map to not use yield.

let private showFirstConnectionUrl (url : string) = Message.message [ Message.Color IsInfo ] [ Message.header [ ] [ str "Ce compte n'a pas encore été activé." ] Message.body [ ] [ CopyButton.copyButtton [ CopyButton.Value url ] ] ] let private viewUserInfo (info : LoadedModel) dispatch = div [ ] [ info.OriginalData.FirstConnectionUrl |> Option.map showFirstConnectionUrl |> ofOption // More code... ]

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/fable-compiler/fable-react/pull/113#issuecomment-436198121, or mute the thread https://github.com/notifications/unsubscribe-auth/AADgNDz1JH9eBFaCzR7UcmbO7xbhLl8Yks5usV7dgaJpZM4YPTYX .

vbfox commented 5 years ago

Yes using Option.Map + ofOption is the other choice but for complex case I find nothing to be clearer and producing less noise instead having to add Some/None + ofOption, when starting with options in the model then it depends

theimowski commented 5 years ago

Currently in SAFE template and its "counter" example there's a little potential of usage for nothing. I found a single place where yield was used for --layout fulma-hero: https://github.com/SAFE-Stack/SAFE-template/pull/175/files#diff-0ff6c3d1f6fb2e5a81ccfc138c7e76a1R593

Surely in future e.g. when we move away from "counter" example, nothing may turn out more handy