# React We provide a higher-order component to connect to enhance presentational components to react to any observable (not limited to those generated by **Subspace**). ### Usage ```js import { observe } from '@embarklabs/subspace/react'; const ObserverComponent = observe(WrappedComponent); ``` This enhanced component will subscribe to any observable property it receives when the component is mounted and automatically unsubscribe when the component is unmounted. ### Example ::: tip This example is available in [Github](https://github.com/embark-framework/subspace/tree/master/examples/react-example1) ::: #### MyComponentObserver.js ```js import React from "react"; import ReactDOM from 'react-dom'; import {observe} from "@embarklabs/subspace/react"; const MyComponent = ({eventData}) => { // Handle initial state when no data is available if (!eventData) { return
No data
; } return{eventData.someReturnedValue}
}; // MyComponent will now observe any observable prop it receives // and update its state whenever the observable emits an event export default observe(MyComponent); ``` #### App.js ```js import React, {Component} from 'react'; import ReactDOM from 'react-dom'; import Subspace from '@embarklabs/subspace'; import MyComponentObserver from './MyComponentObserver'; class App extends Component { state = { myEventObservable$: null } async componentDidMount() { const MyContractInstance = ...; // TODO: obtain a web3.eth.contract instance const subspace = new Subspace("wss://localhost:8545"); // Use a valid provider (geth, parity, infura...) await subspace.init() const myEventObservable$ = subspace.trackEvent(MyContractInstance, "MyEvent", {filter: {}, fromBlock: 1 }); this.setState({ myEventObservable$ }); } render() { return