简体   繁体   English

React无法识别功能

[英]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? 我哪里出问题了?

Just as the error mentions, you forgot to actually add the method to the props: 正如错误所提到的,您忘记了将方法实际添加到道具中:

 <Widget
    handleNewUserMessage={this.handleNewUserMessage}
 />

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

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