Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

data binding in react + signalR

I have a strategy question.

I want to change data in my website using signalR and display changed data using react. My question would be: How to perform data binding between signalR and react?

My first clue is the following:

signalR:

chat.client.addMessage = function (name, message) {
    chatHistory.push({ Author: name, Text: message }); //here I change global variable chatHistory
};

react:

var CommentList = React.createClass({some class here});

var CommentBox = React.createClass({
    componentRefresh: function () {
        this.setState({ data: chatHistory });
    },
    getInitialState: function () {
        return { data: chatHistory };
    },
    componentDidMount: function () {
        this.componentRefresh();
        setInterval(this.componentRefresh, this.props.interval);
    },
    render: function () {
        return (
          React.DOM.div(null,
            CommentList({ data: this.state.data })
          )
      );
    }
});

React.renderComponent(
  CommentBox({ interval: 2000 }),
  document.getElementById('content')
);

in react commentBox component I feed global chatHistory and ask for a new value every 2 seconds.

Is there more elegant way of doing it? and how to avoid redrawing of CommentBox if chatHistory variable wasn't changed?

like image 750
31415926 Avatar asked Jul 30 '14 11:07

31415926


People also ask

What is two way data binding in react apps?

In this tutorial, we are going to learn about two-way data binding in react apps with the help of examples. What is two way data binding? The data we changed in the view has updated the state. The data in the state has updated the view. Let’s implement the two way binding in react.

How do you bind data to a variable in react?

active oldest votes. 55. Data binding in React can be achieved by using a controlled input. A controlled input is achieved by binding the value to a state variable and a onChange event to change the state as the input value changes.

How to configure SignalR hub in react?

Inside the ConfigureServices method, add the following: Now, we have to create a new SignalR Hub which is very straight forward. Create a new class called MessageHub. We need to ensure that this class inherits the Hub class. Finally, we need to map our hub to a route. The React app will use this route to connect to the right hub.

How to integrate SignalR with MVC and react?

Firstly, we built a ASP.NET Core MVC API application. Next, we built a React application in the same directory as the ASP.NET Core application and integrated the two together. Thereafter, we integrated SignalR into both applications to enable us to communicate between ASP.NET Core and React.


1 Answers

Your approach of maintaining state in CommentBox is fine. As your component base grows, it might become complicated to maintain self-updating components though. I recommend investigating the Flux architecture the React team designed and their Todo MVC Flux example in particular.

You could implement shouldComponentUpdate to prevent React from re-rendering the CommentBox if you know state hasn't changed. Also, you should keep a reference to the interval so you can clear it when the CommentBox is unmounted otherwise it will go on polling after the component is removed.

var CommentBox = React.createClass({
  ...

  componentDidMount: function() {
    this.componentRefresh();
    this._interval = setInterval(this.componentRefresh, this.props.interval);
  },

  componentWillUnmount: function() {
    clearInterval(this._interval);
    this._interval = null;
  },

  shouldComponentUpdate: function(nextProps, nextState) {
    // Do a deep comparison of `chatHistory`. For example, use
    // Underscore's `isEqual` function.
    return !_.isEqual(this.state.chatHistory, nextState.chatHistory);
  },

  ...
});
like image 105
Ross Allen Avatar answered Oct 22 '22 08:10

Ross Allen