0

I have some hook, that takes a callback as a parameter

const [fetchPostById, isLoading, error] = useFetching(async (id) => {
    const response = await PostService.getById(id);
    setPost(response.data);
  });

And I'm using another hook to do that

useEffect(() = {
    fetchPostById(params.id);
  }, []);

Inside useEffect params.id is defined and is equal 5

But inside useFetching id is undefined

This is my custom hook useFetching

export const useFetching = (callback) => {
  const [isLoading, setIsLoading] = useState(false);
  const [error, setError] = useState("");

  const fetching = async () => {
    try {
      setIsLoading(true);
      await callback();
    } catch (e) {
      setError(e.message);
    } finally {
      setIsLoading(false);
    }
  };

  return [fetching, isLoading, error];
};

0 Answers0