Skip to main content

React hooks

Chúng tôi cũng cấp một số hooks để giúp bạn tương tác với dữ liệu của Communi Chat.

HookMô tảKiểu dữ liệu
useIsInitSuccessLấy trạng thái khởi tạo thành công của PSChatfunction()
usePSCurrentUserLấy thông tin của người dùng hiện tạifunction()
usePSThreadUnreadCountLấy số lượng cuộc hội thoại có tin nhắn chưa đọcfunction()
useSelectThreadLấy thông tin của một cuộc hội thoạifunction(id: string)
useAllThreadsUnreadCountLấy tổng số lượng tin nhắn chưa đọc của toàn bộ threadfunction()
import { useState } from "react";
import { PSChatProvider, usePSCurrentUser } from "@communi/chat-react";
import axios from "axios";

const UserName = () => {
const user = usePSCurrentUser();
return <div>{user.display_name}</div>;
};

export const App = () => {
const [threadId, setThreadId] = useState(null);

// Demo
const fetchCommuniToken = async () => {
try {
const response = await axios.get("YOUR_FETCH_TOKEN_ENDPOINT");
const { token } = response.data;
return token;
} catch (e) {
console.warn(e);
}
};

return (
<PSChatProvider appId="YOUR_APP_ID" fetchToken={fetchCommuniToken}>
<div className="width-full flex">
<UserName />
</div>
</PSChatProvider>
);
};