question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Rendering Fable React components in a TypeScript React app

See original GitHub issue

I asked this on Gitter, but didn’t get any bites. Hopefully this is the correct place to ask.

I’ve got a question on interop between an NPM library of React components I’d like to port to Fable and using said library from Fable and TypeScript apps.

Say I’ve created a stateless functional component in TypeScript like this:

const MagicButton : SFC<{buttonType: string}> = (props) =>
  <button className={props.buttonType}>
    {props.children}
  </button>

I would like to be able to reimplement it in Fable and use it from both F# and TypeScript apps. I’d like to keep the children and props separate in Fable apps as it reads a lot better, but not lose the ability to use it in JSX.

I’ve tried something like

let Button props children =
  button [ Class props.buttonType] children

which works well in Fable, but does not render children in TypeScript. This makes sense because in React children are passed on props and not as a second parameter. I’ve hacked together something that works:

let UnboxButton props children =
  let pChildren = 
    match (unbox props?children) with
      | Some c -> c
      | _ -> []
  button [ Class props.buttonType ] (unbox pChildren @ children)

which does what I want (renders children in both scenarios), but the code feels hacky and I would rather not use something like this.

Is there a way I can create React components in a Fable project that keep the clean props children signature but still consume them from TypeScript or JavaScript?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
alfonsogarciacarocommented, Mar 30, 2018

Interesting, so far I haven’t used a React Function component accepting children. It’s a bit more verbose but using the class syntax probably works better here:

type [<Pojo>] MyButtonProps = { buttonType: string }

type MyButton(props) =
    inherit React.Component<MyButtonProps, obj>(props)
    override this.render() =
        button [Class this.props.buttonType] (List.ofArray this.children)

You should be able to use this normally with JSX.

import { MyButton } from "./File.fs"

<MyButton buttonType="foo">...</MyButton>
1reaction
timothymclanecommented, Apr 2, 2018

The class syntax isn’t so bad and does what I need. I have a strong preference toward functions instead of classes for React components, especially for presentational components that don’t need lifecycle methods or access to internal state.

If there’s a feasible way to get it working with stateless functional components the way it works with class-based components, that’d be my ideal scenario. But this gets me moving for now. Thanks for the help!

Read more comments on GitHub >

github_iconTop Results From Across the Web

fable-react/docs/using-third-party-react-components.md at ...
1. Install the react component · 2. Define the props type · 3. Define the React component creation function. Member Import; Default Import;...
Read more >
Using third party React Components with Fable/Elmish
I'm trying to integrate Carbon Design System - React SDK in a Fable/Elmish application and it looks like I have to create the...
Read more >
Composing React Components with TypeScript
In this guide, you will learn how to compose React.js components using the compile-to-JavaScript language, TypeScript. Using TypeScript with ...
Read more >
Using JSX in your Fable React Apps
Although the new JSX.Element type from Fable.Core and ReactElement for Fable.React are different types, in React apps they are just the same ...
Read more >
Render a React Component with State and Props using ...
Writing components in TypeScript that contain both props as well as a state is one of the core tasks needed to build apps....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found