Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document creation of stateful react components #87

Open
pkese opened this issue May 13, 2018 · 6 comments
Open

Document creation of stateful react components #87

pkese opened this issue May 13, 2018 · 6 comments

Comments

@pkese
Copy link

pkese commented May 13, 2018

I've spent some effort on creating a custom react component using props and state (including componentDidMount, componentWillReceiveProps, setState and such).

There's a few lines in source code suggesting to do something like:

type MyComponent(initialProps) =
    inherit React.Component<MyProps, MyState>(initialProps)
    base.setInitState({ value = 5 })
    ...

... that I tried to follow, but then I got issues with React reporting errors about component's Children being an object rather than react component and such.
I did fiddle something creating some Pojo objects for MyState and MyProps types, but I'm not really sure what I did and if I did it correctly (I was more like a monkey that somehow managed to write a Shakespeare novel).

It would be really nice to have the concept of strongly typed state and props described somewhere in the documentation.

@pkese
Copy link
Author

pkese commented May 13, 2018

...I also had to

let inline myComponent model = ofType<MyComponent, MyProps, MyState> { model = model } []

...to make it usable (I found that pattern in https://blog.vbfox.net/2018/02/06/fable-react-1-react-in-fable-land.html and it works), although I'm not sure what it does and why this is necessary.

Please understand... this is not a rant... I just listed a couple of things that I'm struggling with. Although I'm experienced with React , I got a bit overwhelmed with the F#-type details when trying to use React with Fable. I'm not sure how to overcome this knowledge gap. Thanks.

@alfonsogarciacaro
Copy link
Member

Hi @pkese! Thanks a lot for your comments. No worries, you're totally right, we're focusing on Elmish apps and still miss documentation about how to write React components directly. This document about how to use 3rd-party React components was recently added and covers some parts, like using the Pojo attribute to compile a record as a plain object instead of a class so Rresteact doesn't complain (we want to remove the need of the attribute in Fable 2).

For the rest and for now, you can check the react-todomvc sample that implements React components directly without using any framework. It's a bit old but the only thing that has changed is the need to use override when implementing methods from the React.Component parent (like render, shouldComponentUpdate, etc).

It's necessary to use ofType because React doesn't instantiate directly component classes (this is transparent to JS devs because of the JSX syntax). You can find other helpers to render React components/elements here (most of them compile to React.createElement).

BTW, you can use the reactiveCom helper to quickly write an Elmish-like stateful React component just by passing functions and without implementing the class yourself.

@SirUppyPancakes
Copy link
Contributor

Just thought I'd drop these here, which helped me get started creating stateful React components in Fable:
https://blog.vbfox.net/2018/02/06/fable-react-1-react-in-fable-land.html
https://blog.vbfox.net/2018/02/08/fable-react-2-optimizing-react.html

They even go over some of the important optimizations related to lambdas and lists/keys.

@njlr
Copy link

njlr commented Feb 26, 2020

Another thing to watch out for is that the Props type must be an object (e.g. an F# record) if you use a string or similar it will fail at run-time. Perhaps Fable could generate a compiler error here?

@MangelMaxime
Copy link
Member

Fable can't do that you no because it doesn't know about React.

However, it's possible to create an Fsharp Analyzer which can analyze the code for you and generate warning/errors.

It's can be integrated in Ionide or invoked by CLI

@njlr
Copy link

njlr commented Feb 27, 2020

Fable can't do that you no because it doesn't know about React.

However, it's possible to create an Fsharp Analyzer which can analyze the code for you and generate warning/errors.

It's can be integrated in Ionide or invoked by CLI

I'm not familiar with the Fable internals, but could the Fable.React bindings be annotated with something that tells the Fable compiler to throw an error?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants