0

Here is my function:

functon ABCComponent({ navigation }){

    const testPostID = "1234567891011";
    
    const [post, setPost] = useState("");

     const getPostById = async(postID)=>{
        await axios.get(`http://localhost:5000/posts/post/${postID}`)
            .then((response) =>{
                dispatch(setLoading(true));
                dispatch(setSuccess(true));
                dispatch(setLoading(false));
                response.data.message.map((post, index) =>{
            
                    setPost(post); // I think it's complaining about this line
                });
            }).catch((error) =>{
                console.log("err: ", error);
                dispatch(setLoading(true));
                dispatch(setSuccess(false));
                dispatch(setLoading(false));
                dispatch(setMessage(`${error}`));
            });
    };

    useEffect(() =>{
        getPostById(testPostID);
    }, []);
    
}

I am getting the following error:

Warning: Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in %s.%s, a useEffect cleanup function, in ABCComponent.

what is the best way to go about solving this?

Abc 123
  • 39
  • 1
  • 7
  • `functon`... `getPostById` vs `fetchPostById`... please include your **actual code** in your question – Phil Jul 03 '22 at 23:41
  • The error is saying that by the time the async request resolves, your component has been unmounted. There's no way to tell why that is without seeing how you're using the component in its parent – Phil Jul 03 '22 at 23:43
  • @Phil I have updated the question. I’m using the function in useEffect hook – Abc 123 Jul 04 '22 at 00:17

0 Answers0