-
Notifications
You must be signed in to change notification settings - Fork 37
React Client
Anders Malmgren edited this page Mar 6, 2019
·
1 revision
Install using unpkg signalr.eventaggregatorproxy.react as decribed here
Requires the vanilla client library too, so do not forget to install it. Also do not forget to register the event proxy.
<script src="/eventAggregation/events" asp-append-version="true"></script>
The plugin is implemented as a Higher-Order Component or HOC. You need to extend your Component. Its done like
const SignalrMyComponent = withSignalREventAggregator(MyComponent);
From your component
componentDidMount() {
this.props.subscribe(MyApp.MyEvent, this.onEvent);
}
onEvent(e) {
this.setState(state => ({
events: state.events.concat(e)
}));
}
You can use all of the vanilla client overloads, the context (this) is marshaled by React. This means you do not need to unsubscribe to messages even in a SPA scenario. The component will call unsubscribe on the componentWillUnmount event for you.
Just like the vanilla client you can publish events.
this.props.publish(new MyClientSideEvent());