score:1
you need to make sure the component is still mounted before trying to update the state:
import { usestate, useeffect } from "react";
import axios from "axios";
const usefetch = (url) => {
const [data, setdata] = usestate(null);
const [error, seterror] = usestate(null);
const ismounted=useref(null)
useeffect(()=>{ ismounted.current=true; return()=>{ismounted.current=false}},[])
useeffect(() => {
axios
.get(url)
.then((response) => {
if (ismounted.current)
{setdata(response.data);}
})
.catch((err) => {
if (ismounted.current)
{seterror(err);}
});
}, [url]);
return { data, error };
};
export default usefetch;
set a mutable value to true right after the component mounts, and set it to false when it is going to unmount.
before every setstate check if it is still mounted.
Source: stackoverflow.com
Related Query
- Can't perform a React state update on an unmounted component when using custom hook for fetching data
- Can't perform a React state update on an unmounted component when using useEffect hook
- Can't perform a React state update on an unmounted component using context provider
- When I add Nextjs Image component in react hook, will show error "Can't perform a React state update on an unmounted component."
- Can't perform a react state update error when using useEffect in a custom express route
- Can't perform a React state update on an unmounted component implemented using HOOK
- Can't perform a React state update on an unmounted component error when fetching data
- Can't perform a React state update on an unmounted component - even when clearing interval
- Getting this error when replying to comment: Can't perform a React state update on an unmounted component
- How to resolve Can't perform a React state update on an unmounted component. in ReactJS when using a string variable instead of a string literal
- Can't perform a React state update on an unmounted component, using class component and component did mount
- Can't perform a React state update on an unmounted component when closing a material-ui modal by setting state
- Can't Perform a React State update on a unmounted component when passing a state as an prop to the child component
- Can't perform a React state update on an unmounted component when adding componentDidUpdate
- Can't perform a React state update on an unmounted component
- React useEffect causing: Can't perform a React state update on an unmounted component
- React-hooks. Can't perform a React state update on an unmounted component
- Warning: Can't perform a React state update on an unmounted component. In a functional component
- useEffect - Can't perform a React state update on an unmounted component
- Error : Can't perform a React state update on an unmounted component
- Can't perform a React state update on an unmounted component with fetch POST method
- react-top-loading-bar Warning: Can't perform a React state update on an unmounted component
- Update React component when state changes, using a reference to itself
- How to fix React WArning : Can't perform a React state update on an unmounted component
- React - Can't perform a React state update on an unmounted component
- React testing library: An update inside a test was not wrapped in act(...) & Can't perform a React state update on an unmounted component
- Ionic React Overmind Can't perform a React state update on an unmounted component
- Router events cause an error while being used on the constructor Warning: Can't perform a React state update on an unmounted component
- Trying to fix: Can't perform a React state update on an unmounted component
- react router v6 won't switch to Home Page after Login Page: Warning: Can't perform a React state update on an unmounted component
More Query from same tag
- React onClick pass object ID from a JSON array
- Avoid SPAN tag in react-translate-component
- Infinite API calls on changing state in callback
- How to get rid of React Select Import Error?
- React: object is not defined no-undef
- How to save the name of object in array object?
- Hosting Apple verification file with NextJS
- How to fetch data from API using id in reactJS?
- Why does the onClick function not fire directly on this component?
- Attempted import error: 'makeStyles' is not exported from '@material-ui/core/styles'
- I am trying to create a Download Button but file is not downloading?
- Auto-scroll to bottom of the messages
- redux toolkit state is not changing
- How to call a json data in component
- Getting image as an object
- How to make every pool of an Array to be only once Assignable
- Filter multiple values in React
- How to render a nested array of React components
- React: state-based styling
- Is there a way of using the useState hook to toggle className in react?
- Password States are reacting one character late
- How to access the DOM element of the child component in Preact with hooks?
- How to render array data one by one inside div tag in react js?
- How do I setup React?
- How to dispatch event in handleSubmit in withFormik
- Unable to import base64 image into react component with webpack
- React Router is leaving my main page loaded when trying to load other pages
- Refreshing chart after submitting a new task
- Type of inputRef - TypeScript
- How return a const or function in primary function