Fabian Schneider
Fabian Schneider

Reputation: 809

React doesn't recognize function

I am trying this tutorial for the react chat and keep getting the error

TypeError: n.props.handleNewUserMessage is not a function

I tried to resolve it using the following resources:

This is my code:

import React, { Component } from 'react';
import { Widget, addResponseMessage } from 'react-chat-widget';
import 'react-chat-widget/lib/styles.css';

class App extends Component {
  componentDidMount() {
    addResponseMessage("How can I help you?");
  }

  handleNewUserMessage = (newMessage) => {
    console.log(`New message incomig! ${newMessage}`);
    // Now send the message throught the backend API
    addResponseMessage('response');
  }

  render() {
    return (
      <div className="App">
        <Widget />
      </div>
    );
  }
}


export default App;

Where have I gone wrong?

Upvotes: 0

Views: 475

Answers (1)

Daniel Krom
Daniel Krom

Reputation: 10060

Just as the error mentions, you forgot to actually add the method to the props:

 <Widget
    handleNewUserMessage={this.handleNewUserMessage}
 />

Upvotes: 3

Related Questions