Skip to content

Instantly share code, notes, and snippets.

@mfeineis
Last active March 6, 2018 01:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mfeineis/ee4c1ccca154d17f7bb6ca34774ad9ae to your computer and use it in GitHub Desktop.
Save mfeineis/ee4c1ccca154d17f7bb6ca34774ad9ae to your computer and use it in GitHub Desktop.
A minimal example for integrating Elm into a React 16 App (https://ellie-app.com/bCchkr7cYa1/2)
port module Main exposing (main)
import Html exposing (Html)
port fromElm : String -> Cmd msg
port toElm : (String -> msg) -> Sub msg
type alias Flags =
{ who : String
}
type alias Model =
{ greeting : String
}
type Msg
= Incoming String
main : Program Flags Model Msg
main =
Html.programWithFlags
{ init = init
, subscriptions = subscriptions
, update = update
, view = view
}
subscriptions : Model -> Sub Msg
subscriptions model =
Sub.batch
[ toElm Incoming
]
init : Flags -> ( Model, Cmd Msg )
init { who } =
( { greeting = "Godspeed you " ++ who ++ "!" }
, Cmd.none
)
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
Incoming text ->
Debug.log ("Elm: Incoming " ++ text)
( model, fromElm "Hello from Elm" )
view : Model -> Html msg
view { greeting } =
Html.text greeting
<html>
<head>
<style>
/* you can style your program here */
</style>
</head>
<body>
<div id="react-root"></div>
<script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<script>
// Minimal Elm in React setup adapted from https://github.com/evancz/react-elm-components
const h = React.createElement;
class ElmWrapper extends React.Component {
setupElm(domNode) {
// Embedding the Elm app into the given DOM node
if (domNode === null) {
return;
}
const app = this.props.src.embed(domNode, this.props.flags);
if (typeof this.props.ports !== 'undefined') {
this.props.ports(app.ports);
}
}
shouldComponentUpdate() {
// Elm handles this sub-tree so we don't want React
// to ever re-render this
return false;
}
render() {
// Rendering the containing `div` once and getting
// a reference to the DOM node for the Elm setup
return h("div", { ref: node => this.setupElm(node) });
}
};
const App = ({ appName }) => (
h("div", { className: "react-app" },
h("h1", null, appName),
h(ElmWrapper, {
flags: { who: "Black Emperor" },
ports: ports => {
console.log("Setting up ports");
ports.fromElm.subscribe(msg => {
console.log("JS: fromElm:", msg);
});
ports.toElm.send("Hello from React");
},
src: Elm.ElmWithReact,
})
)
);
ReactDOM.render(
h(App, { appName: "React Playground" }),
document.getElementById("react-root")
);
</script>
</body>
</html>

MIT License

Copyright (c) 2018 Martin Feineis

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment