简体   繁体   English

React.js:重用组件会导致UI更改冲突

[英]Reactjs: Reusing the components leads to UI changes conflicts

I am trying to build sample chat room application using reactjs and redux for learning purpose. 我正在尝试使用reactjs和redux构建示例聊天室应用程序以用于学习目的。 In which there will be 3 user in which Message_01 component is reused for 3 times. 其中将有3个用户,其中Message_01组件被重复使用3次。 below you can find the code.. 在下面您可以找到代码。

const Main = React.createClass({
  render() {
    return (
        <div className="parentContainer">
            <Message_01 className="messageComponent" message={this.props.message} user="Bob" onMessageRemove={this.props.removeMessage} />
            <Message_01 className="messageComponent" message={this.props.message} user="Kevin" onMessageRemove={this.props.removeMessage}  />
            <Message_01 className="messageComponent" message={this.props.message} user="Stuart" onMessageRemove={this.props.removeMessage} />
        </div>
    );
  }
});

Message_01.js: Message_01.js:

const Message_01 = React.createClass({
    sendMessage() {
        var msg = this.refs.msg.value;

        this.props.onMessageSend(this.props.user, msg);
        this.refs.msg.value = '';
    },

    keypress(event) {
        if (event.keyCode == 13) this.sendMessage();
    },

    onDoubleClickEvent(index, msgID) {
        var removeIcon = $('#' + msgID).find('.glyphicon-remove');

        if (this.props.message[index].User == this.props.user) {
            if (removeIcon.css('display') == 'none')
                removeIcon.css('display', 'block');
            else
                removeIcon.css('display', 'none');
        } else {
            console.log('you cannot get remove icon');
        }
    },

    removeMessage(index) {
        this.props.onMessageRemove(index);
    },

    render() {
        return (
            <fieldset>
                <legend>{this.props.user}</legend>
                {this.props.message.map((msg, index) => {
                    return (
                        <div key={index}>
                            <div className="chatContainer">
                                <div className="msgArea" id={msg.ID} onDoubleClick={() => this.onDoubleClickEvent(index, msg.ID)}>
                                <b>{msg.User}: </b> {msg.Message} <span className="timeSpan">{msg.Time}</span> <span onClick={() => this.removeMessage(index)} style={{ display: 'none' }} className="glyphicon glyphicon-remove" />
                                </div>
                            </div>
                        </div>
                    )
                })}
                <div className="MessageTyper">
                    <input type="text" ref="msg" className="message_typer" onKeyDown={(event) => { this.keypress(event) }} />
                    <button className="send" onClick={this.sendMessage}> Send </button>
                </div>
            </fieldset>
        );
    }
});

The code is such way that if any there will be three message container as shown in below image 代码是这样的,如果有的话,将有三个消息容器,如下图所示

chat room app 聊天室应用

As per the code If user double click the message instance then remove icon will get display near message as given in the code in respective message component. 根据代码如果用户双击消息实例,则删除图标将显示在消息附近,如相应消息组件中的代码所给。 but, now if the message is doubled click in the 2nd or 3rd component the remove icon is display in the first component (as like in below image). 但是,现在,如果消息已被双击,请在第二个或第三个组件中单击,删除图标将显示在第一个组件中(如下图所示)。 Which is wrong. 哪有错

Result of the app 应用程序的结果

it would be more helpful if i get any help for this. 如果我对此有任何帮助,它将会更有帮助。 Thanks in advance. 提前致谢。

This issue is because of using the wrong jquery selector. 此问题是由于使用了错误的jquery选择器。 I should use, var removeIcon =$($('#'+user).find('#' + msgID)).find('.glyphicon-remove'); 我应该使用var removeIcon =$($('#'+user).find('#' + msgID)).find('.glyphicon-remove'); instend var removeIcon = $('#' + msgID).find('.glyphicon-remove'); 实例var removeIcon = $('#' + msgID).find('.glyphicon-remove'); .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM