React - 整个组件可访问的对象

时间:2018-04-05 09:44:49

标签: reactjs pusher chatkit

所以我正在尝试使用chatkit,它们具有我们使用的连接功能。

但是我正在尝试获取currentuser对象,所以我可以在其他函数中使用它。但是,对象始终未定义,函数也未定义。

我正在使用此sdk https://docs.pusher.com/chatkit/reference/javascript

我尝试了 const用户 this.user

import React, { Component } from 'react';
import ToggleButton from './ToggleButton';
import ChatBox from './ChatBox';
import { ChatManager, TokenProvider } from '@pusher/chatkit'



class App extends Component {

    constructor(props) {
        super(props);
        this.state = {
            chatbox: true,
            user: ''
        }
        const chatManager = new ChatManager({
            instanceLocator: 'somestring',
            userId: 'JaneLowTeu',
            tokenProvider: new TokenProvider({ url: 'https://us1.pusherplatform.io/services/chatkit_token_provider/v1/somestring/token' })
        })

        this.user = chatManager.connect() // <-- I want to save the object. Tried both this.user and const user =
            .then(currentUser => {

             console.log('Successful connection', currentUser)
                return currentUser;
            })
            .catch(err => {
             console.log('Error on connection', err)
    })
}




    joinRoom = () => {
        this.user.createRoom({ //<- When I click the button i want to createRoom but now it says user is undefined, function is undefined.
              name: 'general',
              private: true,
              addUserIds: ['craig', 'kate']
            }).then(room => {
              console.log(`Created room called ${room.name}`)
            })
            .catch(err => {
              console.log(`Error creating room ${err}`)
            })
    }



    showChatBox = () => {
        console.log(this.state.chatbox);
        if (this.state.chatbox) {
            return (<ChatBox />);
        }
    }

    toggleChatBox = () => {
        this.setState(prevState => ({ chatbox: !prevState.chatbox }))
    }
    render() {
        return (
            <div>
            <div 
            onClick={() => this.joinRoom()}
            >Join Room</div>

            <ToggleButton onClick={this.toggleChatBox}/>
            {this.showChatBox()}
            </div>
            )
    }
}

export default App;

1 个答案:

答案 0 :(得分:3)

connect返回Promise而不是currentUser

// Connect does not return the `currentUser`
this.user = chatManager.connect()
  .then(currentUser => {
    // You need to access the `currentUser` here
    console.log('Successful connection', currentUser)
    return currentUser;
  })
  .catch(err => {
    console.log('Error on connection', err)
  })

几乎那里。您只需要从currentUser函数访问then并更新您的组件状态:

constructor(props) {
  super(props);
  this.state = {
    chatbox: true,
    user: { }
  }
}

chatManager.connect()
  .then(currentUser => {
    this.setState({
      user: currentUser
    });
    console.log('Successful connection', currentUser);
    return currentUser;
  })
  .catch(err => {
    console.log('Error on connection', err)
  })

然后您可以访问this.state.user

joinRoom = () => {
  this.state.user.createRoom({
     name: 'general',
     private: true,
     addUserIds: ['craig', 'kate']
   }).then(room => {
     console.log(`Created room called ${room.name}`)
   })
   .catch(err => {
     console.log(`Error creating room ${err}`)
   })
 }

请务必将user从字符串('')更改为空对象({})。