0

I have module like this

export default {
    getRating(productId) {
        return axios
            .get(
                `<some-paramethers>`,
            )
            .then(response => response.data)
            .catch((error) => {
                console.log('Failed to get reviews :', error);
            });
    },

    getMultipleProductRatings(productIds) {
        return axios
            .get(
                `<some-paramethers>`,
            )
            .then(response => response.data)
            .catch((error) => {
                console.log('Failed to get reviews :', error);
            });
    },
};

and I have to import one or both of this methods into my React component.

I tried like this:

import getRating from '../../my-module/path';


const ProductCards = ({ slidesCollection }) => {
    const [ratingStats, setRatingStats] = useState({ averageRating: 0 });
    const productId = 'xxxxxxx';

    useEffect(() => {
        getRating(productId).then(res => {
            console.log(res);
        });
    }, []);

but I get error in the browser: TypeError: Object(...) is not a function

pointing at getRating function in useEffect hook

Pokreniseweb
  • 165
  • 3
  • 16

1 Answers1

1
const helpers = {
    getRating: (productId) => {
        return axios
            .get(
                `<some-paramethers>`,
            )
            .then(response => response.data)
            .catch((error) => {
                console.log('Failed to get reviews :', error);
            });
    },

    getMultipleProductRatings: (productIds) => {
        return axios
            .get(
                `<some-paramethers>`,
            )
            .then(response => response.data)
            .catch((error) => {
                console.log('Failed to get reviews :', error);
            });
    },
};

export default helpers;

Usage:

import helpers from '../../my-module/path';


const ProductCards = ({ slidesCollection }) => {
    const [ratingStats, setRatingStats] = useState({ averageRating: 0 });
    const productId = 'xxxxxxx';

    useEffect(() => {
        helpers.getRating(productId).then(res => {
            console.log(res);
        });
    }, []);
niconiahi
  • 545
  • 4
  • 5