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

Lists passed through FunctionComponent.Of() upsets _createForOfIteratorHelper #203

Open
davedawkins opened this issue Aug 14, 2020 · 1 comment

Comments

@davedawkins
Copy link

davedawkins commented Aug 14, 2020

I'm trying to integrate with reactjs-autocomplete.

If I use the direct integration (autocomplete in module AutoComplete below), it kind of works. The things that don't seem to work so well may work better when it's mounted as a FunctionComponent (using autocompleteF in the same module).

However, the props list now throws a runtime TypeError at the call to keyValueList, and I can't see why. You can still take the head/tail, and the same call to keyValueList works just fine when called from the plainer autocomplete.

Uncaught TypeError: Invalid attempt to iterate non-iterable instance.
In order to be iterable, non-array objects must have a [Symbol.iterator]() method.
    _createForOfIteratorHelper Util.js:140
    createObj Util.ts:520
    autocompleteF AutoComplete.fs:20
module AutoComplete

open Fable.React
open Fable.Core.JsInterop
open Fable.Core
open Browser

type AutoCompleteProps<'T,'V> = 
  | Items of 'T array
  | RenderItem of ('T -> ReactElement)
  | Value of 'V
  | GetItemValue of ('T -> 'V)
  | OnChange of (Browser.Types.Event -> string -> unit)
 
let inline autocomplete (props : AutoCompleteProps<'T,'V> list) : ReactElement =
    let propsObj = (keyValueList CaseRules.LowerFirst props)
    ofImport "default" "react-autocomplete" propsObj []

let autocompleteF<'T,'V>  =
    FunctionComponent.Of(fun (props : AutoCompleteProps<'T,'V> list) -> 
        let propsObj = (keyValueList CaseRules.LowerFirst props)
        div [] [ 
            ofImport "default" "react-autocomplete" propsObj []
            str "autocomplete" 
        ]
    )

This is how the call to autocompleteF is defined

              div [] [
                AutoComplete.autocompleteF [
                  Items [| { Label = "HelloX" }; { Label = "WorldX" } |]
                  Value "HelloX"
                  RenderItem (fun item -> div [] [ str item.Label ])
                  GetItemValue (fun item -> item.Label)
                  //OnChange (fun e v -> Select v |> dispatch)
                ]
              ]
@davedawkins davedawkins changed the title Lists passed through FunctionComponent.Of() upset _createForOfIteratorHelper Lists passed through FunctionComponent.Of() upsets _createForOfIteratorHelper Aug 15, 2020
@davedawkins
Copy link
Author

davedawkins commented Aug 15, 2020

Here's a workaround, in the form of makeList. Weird that it fixes things, but I think some magic/metadata has been lost from props after being passed through ReactJS

// Duplicates list-like object to give it back iteration capability
// Workaround for issue logged against fable react
let rec makeList list = 
    match list with
    | [] -> []
    | x::xs -> x :: (makeList xs)

let autocompleteF2<'Item>  =
    FunctionComponent.Of( (fun (props : AutoCompleteProps<'Item> list) -> 
        let propsObj = (keyValueList CaseRules.LowerFirst (makeList props))

        propsObj?menuStyle <- keyValueList CaseRules.LowerFirst menuStyle

        div [] [ 
            ofImport "default" "react-autocomplete" propsObj []
        ]
    ), "autocompleteView", memoEqualsButFunctions)

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

1 participant