--- layout: page title: React bindings --- # React bindings _elfi_ ships with some React bindings so you can quickly start working on an application using both of them. They are available in the `elfi/react` module. ## `storeShape` The `storeShape` object allows you to specify that a component's prop should be a store, which means it has the `dispatch`, `getState` and `subscribe` methods available. ```js import { storeShape } from "elfi/react" function Counter({ store }) { return
{store.getState()}
} Counter.propTypes = { store: storeShape.isRequired, } ``` ## `ElfiContext` and `connect` _elfi_ ships with an integration for the official React Context API that's been available since React 16.3.0. To use it, you must wrap your app in the context provider `ElfiContext.Provider` and you can use `ElfiContext.Consumer` in your child components to consume store data. You'll also probably want to automatically trigger updates using the `subscribe` method from the store. Here is a typical app startup code using _elfi/react_: ```js // app.js import React from "react" import ReactDOM from "react-dom" import { createStore } from "elfi" import { ElfiContext } from "elfi/react" import Counter from "./Counter" const store = createStore(0) // Our state is a simple integer const root = document.getElementById("app-root") document.addEventListener("DOMContentLoaded", renderApp) store.subscribe(renderApp) function renderApp() { ReactDOM.render(Current value is {value}
} export default connect( Counter, value => ({ value }), ) ``` ## Dispatching actions from components Dispatching actions from your React components is no different than using _elfi_ outside of the React environment. To continue on our counter example, here's how you would increment the counter's value when clicking on it: ```js // Counter.js import React from "react" import PropTypes from "prop-types" import { storeShape, connect } from "elfi/react" // Our increment action function increment(state) { return state + 1 } class Counter extends React.Component { static propTypes = { store: storeShape.isRequired, value: PropTypes.number.isRequired, } handleClick = () => { const { store } = this.props store.dispatch(increment) } render() { const { value } = this.props returnCurrent value is {value}
} } export default connect( Counter, value => ({ value }), ) ``` You can [try this demo online](https://codepen.io/madx/pen/MBLvPg)