Skip to content

Instantly share code, notes, and snippets.

@isaidspaghetti
Created July 17, 2020 21:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save isaidspaghetti/f88359e13d0a226a4f69191125cff2e5 to your computer and use it in GitHub Desktop.
Save isaidspaghetti/f88359e13d0a226a4f69191125cff2e5 to your computer and use it in GitHub Desktop.
Render react Stream Chat
//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