score:2
You can wrap the setError
function with useCallback with an empty dependency
in your custom hook before returning so that it is created only once
export const useError = (): ((error: any, title?: string) => void) => {
const dispatch = useDispatch();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const setError = useCallback((error: any, title = 'Error'): void => {
Sentry.captureException(error);
const bodyText = error.message || error;
const errorTitle = error.name || title;
dispatch(
setNotification({
type: notificationTypes.prompt,
title: errorTitle,
bodyText,
className: 'error',
show: true,
})
);
}, []);
return setError;
};
With the above you might get a ESLint warning to add dispatch and Sentry as dependency to useCallback
dependency array, you can add them to the dependency array since neither of disptach
or Sentry
would change
Source: stackoverflow.com
Related Query
- How to use useCallback on a custom hook?
- How to use useCallback hook with onChange function REACT
- How to use custom hook (fetch) with typescript
- How to use a custom React hook to make a POST or DELETE request with Axios
- How to use custom react query hook twice in the same component?
- How to properly use the UseCallback Hook
- How to use a custom hook with event handler?
- How to use Apollo useMutation in custom React hook
- How do I use my custom useFetch hook when a button is clicked?
- How to use TypeScript with a custom hook for reactjs useContext?
- How to use the custom hook on click event which contains useEffect and useReducer?
- How do I use a custom hook inside of useEffect?
- How to use react-testing-library and jest with mocked custom react hook updating?
- How do I get a custom hook in react to load first and then use setInterval to run every 4 sec?
- How to use callback with useState hook in react
- When to use native React.useReducer Hook and how it differentiate from Redux
- React Formik : how to use custom onChange and onBlur
- How to mock react custom hook returned value?
- How to use React useRef hook with typescript?
- How to use custom Input with Formik in React?
- Is it possible to use a custom hook inside useEffect in React?
- How to use absolute path to import custom scss, when using react + webpack?
- React hook form: How to can I use onChange on React Hook Form Version 7.0
- How to mock a custom hook inside of a React component you want to test?
- How to use TypeScript with withRouter, connect, React.Component and custom properties?
- How to test custom async/await hook with react-hooks-testing-library
- How to create a custom hook that recives dependencies?
- How to import and use a custom font in a material-ui theme?
- How can I use the useQuery hook to populate state in other hooks?
- How to use typescript with the definition of custom styles for a select using react-select
More Query from same tag
- How to get a react component's size (height/width) before render?
- Is it possible to restrict to multiple roles in react-admin?
- React state won't update from within a useCallback
- How to pass a function as props to a component of Material-UI for React?
- How can I delete a row using the reduce method from my redux store
- I am trying to set data in state everytime on first index for react in array of object
- .map returning first value in array
- How to update the value present inside array of array in reactjs
- How to give border styles to Material UI TableRow Component?
- How to use webrtc-adapter npm package in Next.js
- Why does React conditional rendering render NaN?
- How to use Redux with Reactjs - this basic d3.js chart as an example
- React - Error : TypeError: Cannot read properties of undefined (reading 'then')
- CORS issue during AWS Cognito Hosted UI sign in using Google
- React find is not a function
- React Konva, blueimp-load-image uploaded Image rescaling
- How to validate a React select value using Yup validation?
- Using react.useEffect and mobx.autorun together
- Why is Context not being passed to my HOC
- TypeError: Object(...) is not a function - Can't connect redux with wrapper function in react
- Reverse animation on click using Framer Motion
- How to optimize the quiz buttons in React.js
- How to transfer props to children's children in React?
- Passing props to child using Route
- React Typescript tag details onclick not showing on the sun editor
- How to set default value for drop-down in react?
- How to get next and previous five element of from an array?
- REACT Multi steps form : Next button doesn't work and first circle already in green
- Check if child component rendered - Jest, Enzyme
- Converting stateful React component to stateless functional component: How to implement "componentDidMount" kind of functionality?