# \[React\] Is there a way to use React Portals?

**URL:** <https://discuss.kotlinlang.org/t/react-is-there-a-way-to-use-react-portals/25282>\
**Category:** JavaScript\
**Created:** [July 26, 2022, 3:55am UTC](https://discuss.kotlinlang.org/t/react-is-there-a-way-to-use-react-portals/25282 "2022-07-26T03:55:20Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![PietroLinguini](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/pietrolinguini/32/9439_2.png) [@PietroLinguini](https://discuss.kotlinlang.org/u/PietroLinguini)\
**Post date:** [July 26, 2022, 3:55am UTC](https://discuss.kotlinlang.org/t/react-is-there-a-way-to-use-react-portals/25282/1 "2022-07-26T03:55:20Z")

</div>

The function I suppose I should use is:

```auto
createPortal(
    children: ReactNode?,
    container: Element,
    key: Key? = definedExternally,
)

```

What I tried is doing something like this:

A component (Modal.kt)

```auto
val Modal = FC<Props> {
    p {
        +"A model modal"
    }
}

```

Then in the main App component:

```auto
val App = FC<Props> {
    val modalContainer = document.getElementById("menu-root") ?: error("Couldn't find menu root container!")
   createPortal(Modal.create(), modalContainer)
}

```

And in the main method:

```auto
fun main() {
    val container = document.getElementById("root") ?: error("Couldn't find root container!")
    createRoot(container).render(App.create())
}

```

This doesn’t work however. I’ve tried writing something akin do what you’d write in vanilla React:

In Modal.kt

```auto
val modalContainer = document.getElementById("modal-root") ?: error("Couldn't find modal root!")
val modalComponent = FC<Props> {
    p {
        +"A model modal"
    }
}

val Modal = createPortal(
    modalComponent.create(),
    modalContainer
)

```

And in the main App component:

```auto
val App = FC<Props> {
    +Modal // Is of type ReactPortal, so you can't invoke it like a function
}

```

This doesn’t work, however, so I feel pretty stuck. Would anybody be able to provide insight into this? It would be greatly appreciated!
