Created
July 17, 2020 21:50
-
-
Save isaidspaghetti/f88359e13d0a226a4f69191125cff2e5 to your computer and use it in GitHub Desktop.
Render react Stream Chat
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//frontend/App.js:7 | |
function App() { | |
//... | |
const [chatClient, setChatClient] = useState(null); | |
const [channel, setChannel] = useState(null); | |
const register = async (e) => { | |
try { | |
e.preventDefault(); | |
var response = await fetch('http://localhost:8080/registrations', { | |
method: 'POST', | |
headers: { | |
'Accept': 'application/json', | |
'Content-Type': 'application/json', | |
}, | |
body: JSON.stringify({ | |
firstName, | |
lastName, | |
email, | |
}), | |
}); | |
const { customerId, customerToken, channelId, apiKey } = await response.json(); | |
const chatClient = new StreamChat(apiKey); | |
await chatClient.setUser( | |
{ | |
id: customerId, | |
name: firstName, | |
}, | |
customerToken, | |
) | |
const channel = chatClient.channel('messaging', channelId); | |
setChatClient(chatClient); | |
setChannel(channel) | |
} catch (e) { | |
console.error(e) | |
} | |
}; | |
if (chatClient && channel) { | |
return ( | |
<div className="App"> | |
<Chat client={chatClient} theme={'messaging light'}> | |
<Channel channel={channel}> | |
<Window> | |
<ChannelHeader /> | |
<MessageList /> | |
<MessageInput /> | |
</Window> | |
<Thread /> | |
</Channel> | |
</Chat> | |
</div> | |
); | |
} else { | |
return ( | |
//... | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment