iam getting 3 errors in my console 1.Uncaught TypeError: Cannot read properties of null (reading 'useState') 2.The above error occurred in the component: 3.Get or create chat error TypeError: Cannot read properties of null (reading 'useState')
Below is the code where i used ChatEngineWrapper:
import React, { useEffect, useState } from "react";
import { ChatEngineWrapper, Socket, ChatFeed } from 'react-chat-engine'
const ChatEngine = props => {
const [showChat, setShowChat] = useState(false)
useEffect(() => {
if (props.visible) {
setTimeout(() => {
setShowChat(true)
}, 500)
}
})
return (
<div
className='transition-5'
style={{
...styles.chatEngineWindow,
...{
height: props.visible ? '100%' : '0px',
zIndex: props.visible ? '100' : '0',
width: '100%',
backgroundColor:'white'
}
}}
>
{
props.visible &&
<ChatEngineWrapper>
<Socket
projectID={process.env.REACT_APP_CE_PROJECT_ID}
userName={props.user.email}
userSecret={props.user.email}
/>
<ChatFeed activeChat={props.chat.id} />
</ChatEngineWrapper>
}
</div>
)
}
export default ChatEngine;
below is the code where iam creating user and chat
import React, { useState } from "react"
import axios from 'axios'
import { styles } from "../styles"
import Avatar from '../Avatar'
const EmailForm = props => {
const [email, setEmail] = useState('')
const [loading, setLoading] = useState(false)
function getOrCreateUser(callback) {
axios.put(
'https://api.chatengine.io/users/',
{username: email, email: email, secret: email},
{headers: {"Private-Key": process.env.REACT_APP_CE_PRIVATE_KEY}}
)
.then(r => callback(r.data))
.catch(e => console.log('Get or create user error', e))
}
function getOrCreateChat(callback) {
axios.put(
'https://api.chatengine.io/chats/',
{usernames: [email, 'HAMSHEENA'], is_direct_chat: true},
{headers: {"Private-Key": process.env.REACT_APP_CE_PRIVATE_KEY}}
)
.then(r => callback(r.data))
.catch(e => console.log('Get or create chat error', e))
}
function handleSubmit(event) {
event.preventDefault();
console.log('Sending Email', email)
getOrCreateUser(
user => {
props.setUser(user)
getOrCreateChat(chat => {
props.setChat(chat)
})
}
)
}
return (
<div
style={{
...styles.emailFormWindow,
...{
height: props.visible ? '100%' : '0px',
opacity: props.visible ? '1' : '0'
}
}}
>
<div style={{ height: '0px' }}>
<div style={styles.stripe} />
</div>
<div
className='transition-5'
style={{
...styles.loadingDiv,
...{
zIndex: loading ? '10' : '-1',
opacity: loading ? '0.33' : '0',
}
}}
/>
<div style={{ position: 'absolute', height: '1%', width: '100%', textAlign: 'center' }}>
<Avatar
style={{
position: 'relative',
left: 'calc(50% - 44px)',
top: '10%',
}}
/>
<div style={styles.topText}>
Welcome to <br /> programming support
</div>
<form
onSubmit={e => handleSubmit(e)}
style={{ position: 'relative', width: '100%', top: '19.75%' }}
>
<input
placeholder='Your Email'
onChange={e => setEmail(e.target.value)}
style={styles.emailInput}
/>
</form>
<div style={styles.bottomText}>
Enter your email <br /> to get started.
</div>
</div>
</div>
)
}
export default EmailForm;
User is getting created inside chatEngine but chat window is not displaying in UI