0

I'm creating a ReactJS chatting application. If a user start typing in a message then I want that another user should get a message saying username is typing. So for this I'm using onChange but whatever a user is typing in is not coming in the input textbox.

Message.js file

const socket = io('localhost:9000');

class Message extends Component {
  constructor(props) {
    super(props);
    this.state = {
      data: "",
      messages: "",
      typing: "",
    }
    this.sendMessage = this.sendMessage.bind(this);
    this.onTyping = this.onTyping.bind(this);
  }

  sendMessage(message) {
    console.log(this.state.data, "=============>state dataa")
    const data = {
      message,
      senderId: this.props.userId,
      roomId: this.state.data.roomId
    };
    console.log('Inside New message', data);
    socket.emit('new message', data );
  }

  onTyping(typing) {
    console.log(this.state.typing, "=====Typing Message====");
  }

  render() {
    const { messages, userId, chatDetails } = this.props;
    console.log('Chat in container : ', messages.toJS());

    return (
      <div className="message">
        { !chatDetails && <h1 style={{ textAlign: 'center' }}>Initiate a New Chat.</h1> }
        { chatDetails && <MessageList messages={this.state.message} user={userId} /> }
        { chatDetails && <MessageBar send={this.sendMessage} typingMessage={this.onTyping}/> }
      </div>
    );
  }
}

const mapStateToProps = state => ({
  messages: state.get('messages'),
  userId: state.getIn(['profile', 'id']),
  chatDetails: state.getIn(['videocall', 'callerDetails'])
});

const mapDispatchToProps = dispatch => ({
  saveMessage: payload => dispatch(saveMessage(payload)),
});

export default connect(mapStateToProps, mapDispatchToProps)(Message);

MessageBar.js

class MessageBar extends Component {
  constructor(props) {
    super(props);
    this.state = { message: '' };
    this.sendMessage = this.sendMessage.bind(this);
    this.onTyping = this.onTyping.bind(this);
  }

  onTyping(e) {
    this.setState({typing: e.target.value});
    this.props.typingMessage(this.state.typing);
  }

  sendMessage(e) {
    this.setState({message: ''});
    this.props.send(this.state.message);
  }


  render() {
    return (
      <div className="message-bar">
        <input
          value={this.state.message}
          type="text"
          onChange={this.onTyping}
          placeholder="Type your message ..."
        />
        <button onClick={this.sendMessage}>
          <i className="fa fa-paper-plane" />
        </button>
      </div>
    );
  }
}

export default MessageBar;

I'm using socket.io and reactjs for this chatting application Can anyone please help me out with this.

2 Answers 2

1

I think you have to update the state of message instead of typing during onTyping function.

onTyping(e) {
 this.setState({message: e.target.value});
 this.props.typingMessage(this.state.typing);
}
Sign up to request clarification or add additional context in comments.

Comments

0

setState is asynchronous so you need to wait state to set before sending it to the prop.

class MessageBar extends Component {
  constructor(props) {
    super(props);
    this.state = { message: '' };
    this.sendMessage = this.sendMessage.bind(this);
    this.onTyping = this.onTyping.bind(this);
  }

  onTyping(e) {
    // here wait for state to set
    this.setState({typing: e.target.value}, () => {
        this.props.typingMessage(this.state.typing);
    });
  }

  sendMessage(e) {
    this.setState({message: ''});
    this.props.send(this.state.message);
  }


  render() {
    return (
      <div className="message-bar">
        <input
          value={this.state.message}
          type="text"
          onChange={this.onTyping}
          placeholder="Type your message ..."
        />
        <button onClick={this.sendMessage}>
          <i className="fa fa-paper-plane" />
        </button>
      </div>
    );
  }
}

export default MessageBar;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.