Skip to main content

Getting Started

What is Bettermode React SDK?

Bettermode React SDK allows developers to easily embed Bettermode directly into their React apps. The SDK provides React hooks for accessing the data on the Bettermode 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 Bettermode 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 Bettermode React SDK using the following npm command:

npm install @tribeplatform/react-sdk

3. Inject Bettermode React provider

Wrap your App by BettermodeProvider. Here is how it will look like in a basic app created by create-react-app.

index.tsx
import { Provider as BettermodeProvider } from '@tribeplatform/react-sdk'

ReactDOM.render(
<React.StrictMode>
<BettermodeProvider
config={{
accessToken: '{yourAccessToken}',
baseUrl: 'https://api.bettermode.com',
}}
>
<App />
</BettermodeProvider>
</React.StrictMode>,
document.getElementById('root'),
)
info

If your community is hosted on a region other than the US region (us-east-1), you should use a different GraphQL URL (baseUrl) as stated here under the GraphQL Endpoint section.

You should replace {yourAccessToken} with the access token generated in step 1.

4. Use hooks to query data

Bettermode 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 Bettermode react hooks:

SpaceList.jsx
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:

Feed.jsx
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 Bettermode React hooks to perform actions.

Here is an example of how performing Like action on a post can look like:

LikeButton.jsx
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.