0

i am building an Management app where one is assignee and other is user ( complainer, who makes complaint ). Both assignee and complainer have their own dashboards. i want to implement that when complainer makes new complaint, it will be displayed on assignee dashboard without reloading the page. i have implemented this but this gives me that error.

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 a useEffect cleanup function.

i have tried many times but still this error.

import React, { createContext, useState, useEffect } from "react";
import http from "../services/httpService";
import { getAssigneeComplaints } from "../services/complaintService";
import config from "../config.json";

export const ComplaintContext = createContext();

const ComplaintContextProvider = props => {
   const [assigneeComplaints, setAssigneeComplaints] = useState([]);

  // getting all assignee complaints
  const getAssigneeComplaint = async () => {
    const { data } = await getAssigneeComplaints();
    setAssigneeComplaints(data);
    console.log(data);
   };

  // saving a complaint
  const saveComplaint = async complaint => {
    console.log("new complaint is being saved");
    await http.post(config.apiUrl + "/complainer-complaints", complaint);
    getAssigneeComplaint();
 };

  useEffect(() => {
    getAssigneeComplaint();
  }, []);

  return (
    <>
      <ComplaintContext.Provider
        value={{
          getAssigneeComplaint,
          assigneeComplaints,
          saveComplaint
        }}
      >
        {props.children}
      </ComplaintContext.Provider>
    </>
  );
};

export default ComplaintContextProvider;

i expect to show the complaint on assignee's dashbaord without reloading the page.

HoldOffHunger
  • 18,769
  • 10
  • 104
  • 133
Makki Anjum
  • 138
  • 3
  • 10

2 Answers2

0

For real time updation you should use socket.io-react.Following link is useful for you : https://www.npmjs.com/package/socket.io-react

Bhawana
  • 372
  • 1
  • 6
  • can it be achieved with Redux instead of socketio? – Makki Anjum Jul 25 '19 at 11:09
  • If you use redux than after updating state it will again render component , so if you want data without reloading the page than you should go with socket. – Bhawana Jul 25 '19 at 11:18
  • then why it is not working using CONTEXT ? contextAPI can do the same as Redux. why this error?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 a useEffect cleanup function. – Makki Anjum Jul 25 '19 at 11:22
0

As the answer above you need to use web-socket for real time updates. Have a look on socket.io

Akram Badah
  • 417
  • 3
  • 12