[英]How to fix “ Cannot read property 'sendMessage' of undefined” react chat system
I am building a chat system with react and chatkit. 我正在建立一个带有react和chatkit的聊天系统。 I have a function called sendMessage in App.js that sends whatever the user type in the sendMessage Form to display it in the MessageList Component.
我在App.js中有一个名为sendMessage的函数,该函数可以发送sendMessage表单中的任何用户类型以将其显示在MessageList组件中。
this is App.js 这是App.js
import Chatkit from '@pusher/chatkit'
import MessageList from './components/MessageList'
import SendMessageForm from './components/SendMessageForm'
import RoomList from './components/RoomList'
import NewRoomForm from './components/NewRoomForm'
import { tokenUrl, instanceLocator } from './config'
class App extends React.Component {
constructor() {
super()
this.state = {
messages: []
}
this.sendMessage = this.sendMessage.bind(this)
}
componentDidMount() {
const chatManager = new Chatkit.ChatManager({
instanceLocator,
userId: 'Ismail',
tokenProvider: new Chatkit.TokenProvider({
url: tokenUrl
})
})
chatManager.connect()
.then(currentUser => {
this.currentUser = currentUser
this.currentUser.subscribeToRoom({
roomId: 24622056,
hooks: {
onNewMessage: message => {
this.setState({
messages: [...this.state.messages, message]
})
}
}
})
})
}
sendMessage(text) {
this.currentUser.sendMessage({
text,
roomId: 24622056
})
}
render() {
return (
<div className="app">
<RoomList />
<MessageList messages={this.state.messages} />
<SendMessageForm sendMessage={this.sendMessage} />
<NewRoomForm />
</div>
);
}
}
export default App
And this is sendMessageForm.js component. 这是sendMessageForm.js组件。
import React from 'react'
class SendMessageForm extends React.Component {
constructor() {
super()
this.state = {
message: ''
}
this.handleChange = this.handleChange.bind(this)
this.handleSubmit = this.handleSubmit.bind(this)
}
handleChange(e) {
this.setState({
message: e.target.value
})
}
handleSubmit(e) {
e.preventDefault()
this.props.sendMessage(this.state.message)
}
render() {
return (
<form
onSubmit={this.handleSubmit}
className="send-message-form">
<input
onChange={this.handleChange}
value={this.state.message}
placeholder="Type your message and hit ENTER"
type="text" />
</form>
)
}
}
export default SendMessageForm
the expected output is the message showing up in the messages box after i hit enter but I get this: 预期的输出是在我按Enter键后显示在消息框中的消息,但是我得到了:
TypeError: Cannot read property 'sendMessage' of undefined
App.sendMessage
src/App.js:46
43 | }
44 |
45 | sendMessage(text) {
> 46 | this.currentUser.sendMessage({
| ^ 47 | text,
48 | roomId: 24622056
49 | })
View compiled
SendMessageForm.handleSubmit
src/components/SendMessageForm.js:22
19 |
20 | handleSubmit(e) {
21 | e.preventDefault()
> 22 | this.props.sendMessage(this.state.message)
| ^ 23 | }
24 |
25 | render() {
As others have answered, your currentUser
class property is undefined, and as such can't have a sendMessage
method. 正如其他人回答的那样,您的
currentUser
类属性是未定义的,因此不能具有sendMessage
方法。 Your code essentially looks like this: 您的代码基本上如下所示:
this.undefined.sendMessage(...)
which obviously can't work. 这显然是行不通的。
You probably forgot to set currentUser
's value (I guess it has a sendMessage
method). 您可能忘记了设置
currentUser
的值(我猜它具有sendMessage
方法)。
In your sendMessageForm.js File Change the first two lines of constructor to this 在您的sendMessageForm.js文件中,将构造函数的前两行更改为
constructor(props) {
super(props);
.
.
.
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.