Reaction Hooks

Reaction hooks are the set of hooks which would enable you to use Reactions for any entity that could be reacted upon.

πŸ“˜

What are Reactions?

Refer our core reactions documentation

useReactionSpace

useReactionSpace hook gives you reaction space state based on given targetGroupId.

Hook Type definition: useReactionSpace

Example Usage:
import { useReactionSpace } from "@livelike/react-native";

const { reactionSpace } = useReactionSpace({
  targetGroupId: "<Target group ID>",
});

useReactionPacks

useReactionPacks gives you list of reaction pack that are mapped with a given a reaction space. You need a reaction space Id for this hook that could be retrieved using useReactionSpace.

For loading reaction pack state and keeping the state update, you need to use useLoadReactionPacksEffect hook.

Hook Type definition: useReactionPacks

Example Usage:
import { useReactionSpace, useReactionPacks } from "@livelike/react-native";

const { reactionSpace } = useReactionSpace({
  targetGroupId: "<Target group ID>",
});
const { reactionPacks } = useReactionPacks({
  reactionSpaceId: reactionSpace?.id,
});

useLoadReactionPacksEffect

useLoadReactionPacksEffect hook loads the reaction packs state and also updates the reaction packs state for any change made in the reaction packs through our producer suite.

Hook Type definition: useLoadReactionPacksEffect

Example Usage:
import { useReactionSpace, useLoadReactionPacksEffect } from "@livelike/react-native";

const { reactionSpace } = useReactionSpace({ targetGroupId: "<Room ID>" });
useLoadReactionPacksEffect({ reactionSpaceId: reactionSpace?.id });

useUserReactions

useUserReactions hook returns user reactions state for multiple targets. You need a reaction space Id for this hook that could be retrieved using useReactionSpace
For loading user reactions state, use useLoadUserReactions hook.

For auto updating user reactions state whenever any user adds or removes reaction, use useUserReactionEffect

Hook Type definition: useUserReactions

Example Usage:
import { useMemo } from "react";
import { 
  useReactionSpace,
  useLoadUserReactions,
  useReactions,
  useChatMessages
} from "@livelike/react-native";

const { reactionPacks, reactionSpace } = useReactions({
  targetGroupId: "<Room ID>",
});

const { messages } = useChatMessages({ roomId: "<Room ID>" });
const targetIds = useMemo(() => messages.map(({ id }) => id), [messages]);

const { userReactions } = useUserReactions({
  reactionSpace,
  targetIds,
});

useLoadUserReactions

useLoadUserReactions hook is to load user reactions state. You need a reaction space Id for this hook that could be retrieved using useReactionSpace
This hook implements a lazy load technique. To fetch the resources you need to invoke the loadUserReactions function that is returned by this hook.

Hook Type definition: useLoadUserReactions

Example Usage:
import { useReactionSpace, useLoadUserReactions } from "@livelike/react-native";

const { reactionSpace } = useReactionSpace({ targetGroupId: "<your entity Id>" });
const { loadUserReactions } = useLoadUserReactions({
  reactionSpaceId: reactionSpace?.id,
});

const targetIds = useMemo(() => messages.map(({ id }) => id), [messages]);
useEffect(() => {
  if (!targetIds.length) {
    return;
  }
  loadUserReactions({ targetIds });
}, [targetIds]);

useUserReactionEffect

useUserReactionEffect is a side effect based hook that updates user reactions state whenever any user adds or removes reactions. This hook makes user reaction state reactive keeping it up to date with latest user reactions. Internally it uses reaction space event listeners to listen to ADD_REACTION and REMOVE_REACTION event.
Make sure to use this hook only once in your react component tree.

You need a reaction space Id for this hook that could be retrieved using useReactionSpace

Hook Type definition: useUserReactionEffect

Example Usage:
import { useReactionSpace, useUserReactionEffect } from "@livelike/react-native";

const { reactionSpace } = useReactionSpace({ targetGroupId: "<Room ID>" });
useUserReactionEffect({ reactionSpaceId: reactionSpace?.id });