React不识别功能

时间:2018-05-23 08:40:40

标签: javascript reactjs

我正在尝试this tutorial for the react chat并继续收到错误

TypeError: n.props.handleNewUserMessage is not a function

我尝试使用以下资源解决它:

这是我的代码:

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;

我哪里出错?

1 个答案:

答案 0 :(得分:3)

正如错误提到的那样,你忘了将方法实际添加到道具中:

 <Widget
    handleNewUserMessage={this.handleNewUserMessage}
 />