在React JS mixin中,轮询不起作用。

3

因此,我创建了以下mixin:

var Polling = {

    startPolling: function() {
        var self = this;

        setTimeout(function() {
            self.poll();

            if (!self.isMounted()) {
                return;
            }

            self._timer = setInterval(self.poll(), 15000);
        }, 1000);
    },

    poll: function() {
        if (!this.isMounted()) {
            return;
        }

        var self = this;
        console.log('hello');
        $.get(this.props.source, function(result) {
            if (self.isMounted()) {
                self.setState({
                    error: false,
                    error_message: '',
                    users: result
                });
            }
        }).fail(function(response) {
            self.setState({
                error: true,
                error_message: response.statusText
            });
        });
    }
}

请注意 poll 函数中的 console.log('hello');。根据这个逻辑,我应该每 15 秒钟看到它。
现在让我们看一个 React 组件:
//= require ../../mixins/common/polling.js
//= require ../../mixins/common/state_handler.js
//= require ../../components/recent_signups/user_list.js

var RecentSignups = React.createClass({

    mixins: [Polling, StateHandler],

    getInitialState: function() {
        return {
            users: null,
            error_message: '',
            error: false
        }
    },

    componentDidMount: function() {
        this.startPolling();
    },

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

    shouldComponentUpdate: function(nextProps, nextState) {
        if (this.state.users         !== nextState.users ||
            this.state.error         !== nextState.error ||
            this.state.error_message !== nextState.error_message) {

            return true;
        }

        return false;
    },

    renderContents: function() {
        if (this.state.users === null) {
            return;
        }

        return (
            <div>
                <ul>
                    <UserList users={this.state.users} />
                </ul>
            </div>
        );
    },

    render: function() {
        return (
            <div>
            {this.loading()}
            {this.errorMessage()}
            {this.renderContents()}
            </div>
        )
    }
});

RecentSignupsElement = document.getElementById("recent-signups");

if (RecentSignupsElement !== null) {
    ReactDOM.render(
        <RecentSignups source={ "http://" + location.hostname + "/api/v1/recent-signups/" } />,
        RecentSignupsElement
    );
}

componetDidMount函数中,我正在调用this.startPolling。页面加载时,1秒后我看到:

hello
hello
  • A) 它的 (poll 函数) 似乎被调用了两次 oO。
  • B) 它的 (poll 函数) 没有再次被调用。

我将轮询分离出来,这样我就可以在同一页上的其他组件中使用它,而不会重复代码。

非常简单的问题:

为什么以及如何修复它?我需要每15秒轮询一次,并且只有在第一次调用poll时才会看到hello

2个回答

6
在这一行中,你调用了 self.poll() 函数,返回的结果是计时器:
self._timer = setInterval(self.poll(), 15000);

改为传递函数:

self._timer = setInterval(self.poll, 15000);

0
作为另一种选择,以“你的代码不起作用?那就用别人的!”的精神来看,react-async-poll 是一个方便的组件包装器,可用于轮询。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接