Skip to main content
The usePoints hook provides a way to get the current user’s points and to add points to the current user.

Usage

Get Points

    import { usePoints } from '@useawards/useaward-expo';
    import { useEffect, useState } from 'react';
    import { Text, View } from 'react-native';

    export default function Points() {
        const { isLoaded, getPoints } = usePoints();
        const [points, setPoints] = useState<number>([]);

        useEffect(() => {
            if (isLoaded) {
            const fetchPoints = async () => {
                const points = await getPoints();
                setPoints(points);
            }

            fetchPoints();
            }
        }, [isLoaded, getPoints]);

        return (
            <View>
                {points ?? 0}
            </View>
        )
    }

Add Points

    import { usePoints } from '@useawards/useaward-expo';
    import { useEffect, useState } from 'react';
    import { TouchableOpacity, Text, View } from 'react-native';

    export default function Points() {
        const { addPoints } = usePoints();

        const handleAddPoints = async () => {
            await addPoints(100, "Some reason"); // reason can be empty or null
        }

        return (
            <View>
                <TouchableOpacity onPress={handleAddPoints}>
                    <Text>Add Points</Text>
                </TouchableOpacity>
            </View>
        )
    }

Points history

    import { usePoints } from '@useawards/useaward-expo';
    import { useEffect, useState } from 'react';
    import { Text, View } from 'react-native';

    export default function PointsHistory() {
        const { isLoaded, getPointsHistory } = usePoints();

        useEffect(() => {
            if (isLoaded) {
                const fetchHistory = async () => {
                    const history = await getPointsHistory(); // you can pass page number as an argument
                }

                fetchHistory();
            }
        }, [isLoaded, getPointsHistory]);

        return (
            <View>
                ...
            </View>
        )
    }
History object:
{
    "data": [{
        "id": 1,
        "points": 100,
        "reason": "Some reason",
        "createdAt": "2021-01-01T00:00:00.000Z"
    }],
    "meta": {
        "current_page": 1,
        "from": 1,
        "last_page": 1,
        "per_page": 10,
        "to": 7,
        "total": 7
    }
}