Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/jedirandy/elm-react

:leaves: Use Elm modules in you React app!
https://github.com/jedirandy/elm-react

elm javascript javascript-library react

Last synced: 18 days ago
JSON representation

:leaves: Use Elm modules in you React app!

Awesome Lists containing this project

README

        

# Elm React

[![Build Status](https://travis-ci.org/jedirandy/elm-react.svg?branch=master)](https://travis-ci.org/jedirandy/elm-react)
[![npm module](https://badge.fury.io/js/elm-react.svg)](https://www.npmjs.org/package/elm-react)

A library for using Elm modules in your React components

## Install
```sh
npm install elm-react
```

## Usage
```javascript
import React from 'react'
import { inject } from 'elm-react'
import { ElmModule } from './ElmModule.elm'

class App extends React.Component {
render() {
const { renderElm, handleClick } = this.props;
return (


handleClick()}>Click me
{ renderElm({ flags: {} }) }

)
}

onScroll(args) {
// ...
}
}

inject(
// Elm module to be used
ElmModule,
// options
{
send: {
'click': 'handleClick' // Pass 'handleClick' to App's props, which is bound with the 'click' port of ElmModule for sending messages
},
subscribe: {
'scroll': 'onScroll' // subscribe to the 'scroll' port, use 'onScroll' as callback
},
as: 'renderElm' // renderElm will be available in App's props, to render ElmModule
}
)(App)
```

Check out [this project](/example) for a working example!

## API

`inject(module, options)(component)`

* module: the Elm module to be injected into the React component

* options *(object)*
* send *(object)* Each key-value pair is a mapping from the Elm module's port, to the function name that will be passed to the React component's props, that function can be used to send a message to that port

* subscribe *(object)* Subscribe to an Elm port, each key-value pair is a mapping from the Elm module's port name to the callback name of the React Component

* as *(string)* the name of the render method to be injected into the React component's props

* component *(React Component)*: the React component into which the Elm module is injected