Skip to content

Path imported by `[<ReactComponent(import="...", from="...")>] should not be relative to the caller side #624

@MangelMaxime

Description

@MangelMaxime

I have a Code.tsx file with a Components.fs file where I am binding the component for usage in F#:

[<ReactComponent(import="default", from="./${outDir}/Code.tsx")>]
let Code (code: string) (language : string) = React.imported()

Then in another file from another directory, I am calling the F# Code function to use the component. The generated code is:

import Code from "./Code.tsx";

The issue is that ./Code.tsx is invalid because the caller file is not inside of the same directory as Components.fs:

.
├── Components
│   ├── Code.tsx
│   ├── Components.fs
│   └── Components.fs.js
├── Utils
│   ├── Html.fs

The import path should be relative to where the API is defined.

IHMO this should be the default behaviour otherwise, it means that if you need to use a JSX component then you need to re-declare it several times 😅

A workaround, is to create a standard Feliz binding using Interop.reactApi.createElement to we can use Fable special macro${outDir}:

type Components =

    static member Code (code: string) (language: string) =
        Interop.reactApi.createElement (
            import "default" "${outDir}/Code.tsx",
            {|
                code = code
                language = language
            |}
        )

It is important to not mark this member as inline otherwise it will not work because the call site is not stable.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions