javascript - 使用 React/Socket.IO 无法读取未定义错误的属性 'emit'

标签 javascript node.js reactjs socket.io

我正在尝试基于 React 教程 https://facebook.github.io/react/docs/tutorial.html 使用 React 和 Socket.Io 构建一个基本的聊天应用程序但不断收到错误“无法读取未定义的属性‘emit’”。这可能是我忽略的一些微不足道的事情,但我无法弄清楚。

触发错误的函数是:

    handleSubmit: function (e) {
    e.preventDefault();
    var author = this.state.author.trim();
    var text = this.state.text.trim();
    if (!text || !author) {
        return;
    }
    this.props.onCommentSubmit({ author: author, text: text });
    this.setState({ author: '', text: '' });
    this.socket.emit('message', comment);
},

完整代码为

import React, { Component, PropTypes } from 'react';
import ReactDom from 'react-dom';
import io from 'socket.io-client'

/********************************************************************************************************/

var CommentBox = React.createClass({

    getInitialState: function () {
        return { data: [] };
    },

    handleCommentSubmit: function (comment) {
        this.setState({ data: [comment, ...this.state.data] });
    },

    componentDidMount: function (data) {
        this.socket = io.connect();
        this.socket.on('message', function (comment) {
            this.setState({ data: [comment, ...this.state.data] });
        });
    },

    render: function () {
        return (
            <div className="commentBox">
                <h1>Comments</h1>
                <CommentList data={this.state.data} />
                <CommentForm onCommentSubmit={this.handleCommentSubmit} />
            </div>
        );
    }
});

/********************************************************************************************************/

var CommentList = React.createClass({
    render: function () {
        var commentNodes = this.props.data.map(function (comment) {
            return (
                <Comment author={comment.author} key={comment.id}>{comment.text}</Comment>
            );
        });
        return (
            <div className="commentList">
                {commentNodes}
            </div>
        );
    }
});

/********************************************************************************************************/

var CommentForm = React.createClass({

    getInitialState: function () {
        return { author: '', text: '' };
    },

    handleAuthorChange: function (e) {
        this.setState({ author: e.target.value });
    },

    handleTextChange: function (e) {
        this.setState({ text: e.target.value });
    },

    handleSubmit: function (e) {
        e.preventDefault();
        var author = this.state.author.trim();
        var text = this.state.text.trim();
        if (!text || !author) {
            return;
        }
        this.props.onCommentSubmit({ author: author, text: text });
        this.setState({ author: '', text: '' });
        this.socket.emit('message', comment);
    },

    render: function () {
        return (
            <div>
                <form className='commentForm' onSubmit={this.handleSubmit}>
                    <input type='text' placeholder='Name' value={this.state.author} onChange={this.handleAuthorChange} />
                    <input type='text' placeholder='Enter Message' value={this.state.text} onChange={this.handleTextChange} />
                    <input type='submit' value='Post' />
                </form>
            </div>
        );
    }
});

/********************************************************************************************************/

var Comment = React.createClass({
    render: function () {
        return (
            <div className="comment">
                <h2 className="commentAuthor">
                    {this.props.author}
                </h2>
                {this.props.children}
            </div>
        );
    }
});

/********************************************************************************************************/
ReactDom.render(
    <CommentBox />,
    document.getElementById('container')
);

最佳答案

this.socket.emit('message', comment) 的调用位于错误的位置,您的 CommentForm 组件中没有定义 this.socket 和 comment。

您必须在 CommentBox 组件的 handleCommentSubmit 方法中调用 this.socket.emit('message', comment)。 (第二个代码示例中的第 14 行)

关于javascript - 使用 React/Socket.IO 无法读取未定义错误的属性 'emit',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40099760/

相关文章:

javascript - D3 缩放和平移不适用于整个 map ,仅适用于投影

javascript - 在 ember 中记录单选按钮的值

javascript - 如何循环遍历 javascript 对象并将值复制到新对象

javascript - 按标签和时间范围查询 Instagram 帖子

node.js - 扩展 NodeJS 聊天室 - 在 Redis 中存储连接状态

ios - 移动 Safari 中的 React PWA 图像上传会破坏应用程序吗?

reactjs - 来自 react-native-searchable-dropdown 的 React-native 组件 SearchableDropdown 不会触发 onItemSelect={(item) => {console.log(item)}}

javascript - 如何在 Python 中使用 Javascript 对象文字

node.js - expressja 使用 xhr 或 fetch 发出 http 请求

mysql - 循环查询在 Node js 中 Sequelize