@tribeplatform/react-sdk
v2.2.0
Published
> TODO: description
Downloads
167
Keywords
Readme
Getting Started
What is Tribe React SDK?
Tribe React SDK allows developers to easily embed Tribe directly into their React apps. The SDK provides React hooks for accessing the data on the Tribe Platform and enables full customizability of the UI.
The state management, caching, optimistic response, etc. is all handled on the SDK level. This removes all the complexities in building an online community and lets the developer to focus on the look and feel so it fits their product or brand perfectly.
How to use Tribe React SDK?
1. Generate an access token
First you need to generate an access token for the logged in member or guest. You can generate it directly using our GraphQL API or JavaScript SDK.
2. Install the React SDK
You can simply install Tribe React SDK using the following npm
command:
With Yarn:
$ yarn add @tribeplatform/react-sdk
With NPM:
$ npm install @tribeplatform/react-sdk
3. Inject Tribe React provider
Wrap your App by TribeProvider
. Here is how it will look like in a basic app created by create-react-app
.
import { Provider as TribeProvider } from '@tribeplatform/react-sdk'
ReactDOM.render(
<React.StrictMode>
<TribeProvider
config={{
accessToken: '{yourAccessToken}',
baseUrl: 'https://app.tribe.so/graphql',
}}
>
<App />
</TribeProvider>
</React.StrictMode>,
document.getElementById('root'),
)
You should replace {yourAccessToken}
with the access token generated in step 1.
4. Use hooks to query data
Tribe React SDK uses react-query
under the hood for making the queries and performing GraphQL mutations.
Here is an example for fetching and displaying your community's spaces using Tribe react hooks:
import { useSpaces } from '@tribeplatform/react-sdk/hooks'
export function SpaceList() {
const { data: spaces, isLoading } = useSpaces({ fields: { image: 'basic' } })
return (
<ul>
{isLoading && <div>Loading...</div>}
{spaces?.pages[0]?.nodes?.map(space => (
<li className="mb-3">{space.name}</li>
))}
</ul>
)
Here is a more complicated example on fetching feed posts with load more button:
import { useFeed } from '@tribeplatform/react-sdk/hooks'
import { simplifyPaginatedResult } from '@tribeplatform/react-sdk/utils'
export function Feed() {
const {
data: posts,
isLoading,
fetchNextPage,
hasNextPage,
isFetchingNextPage,
} = useFeed({
fields: {
owner: { member: 'all' },
reactions: { variables: { limit: 5 }, fields: 'basic' },
},
variables: { limit: 10 },
})
// Convert pages of notes into a flat list of nodes
const { nodes: latestPosts } = simplifyPaginatedResult(posts)
return (
<main>
{isLoading && <div>Loading...</div>}
<ul>
{latestPosts?.map(post => {
return (
<li>{post.title}</li>
)
})}
</ul>
{hasNextPage && (
<button
onClick={() => fetchNextPage()}
>
{isFetchingNextPage ? `Loading more...` : `Load more`}
</button>
)}
</main>
)
}
The above example, displays a list of posts in the feed and a "Load more" button. Clicking on the "Load more" button automatically appends the new data to the list and the new posts are shown right away.
5. Use hooks to perform an action
Similar to queries, you can use Tribe React hooks to perform actions.
Here is an example of how performing Like action on a post can look like:
import {
useAddReaction,
useRemoveReaction,
} from '@tribeplatform/react-sdk/hooks'
export function LikeButton({ post }) {
const { mutate: likePost } = useAddReaction()
const { mutate: unlikePost } = useRemoveReaction()
const reacted = !!post?.reactions?.find(reaction => {
return reaction.reaction === '+1' && reaction.reacted
})
return (
<button
onClick={e => {
if (reacted) unlikePost({ postId: post?.id, reaction: '+1' })
else
likePost({
postId: post?.id,
input: { reaction: '+1' },
})
}}
>
{reacted ? "Unlike" : "Like"}
</button>
)
}
Clicking on the "Like" button will change the value of the post right away, and the button will be switched to "Unlike" right away.