score:1
Accepted answer
the second parameter that you pass to useeffect tells react to skip the effect unless one of the items in the array has changed. you passed in an empty array, so other than the initial render, it will never update. thus, you set up the event listeners with functions that have the original state in their closure, and nothing else.
to get it to update as state changes, either remove the array, or fil it with variables that your code depends on:
useeffect(() => {
document.addeventlistener('copy', handlecopy);
document.addeventlistener('paste', handlepaste);
return () => {
document.removeeventlistener('copy', handlecopy);
document.removeeventlistener('paste', handlepaste);
}
}, [state])
Source: stackoverflow.com
Related Query
- why is a React class component converted to functional component with hooks not picking up state in event handler on document
- Not able to add, update, delete rows in material table react using class based component. Previously it was working with functional component
- How to convert React class component to functional component with hooks
- Can anyone explain why this works with a class component but not a functional component?
- Refactor React class component into functional with hooks
- Converting React class component to functional component with hooks : Problem with event listeners
- converting onCancel to functional component with react Hooks from class component
- Trying to convert a class based component to a functional with React Hooks
- Testing React Functional Component with Hooks using Jest
- Why do I have to .bind(this) for methods defined in React component class, but not in regular ES6 class
- Accessing the State of a Functional Component with React Hooks when Testing with Enzyme
- Why is my react component not updating with state updates?
- Multiple times render in react functional component with hooks
- Converting React class with "static get defaultProps()" to functional component
- Converting class component to functional component using react hooks (useRef)—how to avoid "undefined" error
- In react class component why is the state not being changed in the first instance of button click? But in the second instance it gets updated?
- Reactjs - translating a class component to a functional one with hooks
- How can I convert a class with a constructor to a functional component using React Hooks?
- Elements not displaying with .map() in react functional component with useEffect hook
- How to test a handle function call with a react functional component with hooks
- React with Typescript | Functional component | Type 'boolean' is not assignable to type 'ReactElement<any, any>'
- Why is setState not working in React functional component
- Why callbacks in react functional component is not reading the updated state value
- React functional component useEffect hook with dependency equal in class component lifecycles
- React JS Context API with functional component is not working
- Refactor a functional component with React hooks
- Component local state not updating with react custom hooks
- React Functional component with stateless class inside
- React call from class to functional component. Error: Invalid hook call. Hooks can only be called inside of the body of a function component
- Why is my React component not re-rendering upon state change with Date?
More Query from same tag
- Avoid passing around request object
- Create a React component for each element on JSON
- Dynamic variable name using this in ReactJS
- Conditional link in JSX
- Parent page doesn't scroll when mouse over iframe
- How to animate a React component on render?
- Getting prop from child everytime the prop changes
- InteliJ: Typescript auto check {} object against an interface?
- TypeError: Cannot read property 'byteLength' of undefined - AWS S3 Upload
- React-TypeScript: Expected 0 arguments, but got 1 on a useReducer
- Gatsby build getting window error while using onLoad
- TypeError: Cannot read property 'forEach' of undefined React
- Adding a unicode escape to a dynamic string value
- React component props not changing when they should?
- Open Modal displaying info about clicked target in React
- 2nd parameter of useCallback hook
- makeStyles() throwing error using Typescript
- How to send data which arrived on post endpoint through io.emit? NodeJS + React
- i want to change the width of the video player when it comes to tablet size
- Solve conflict between Electron and Reactjs
- render link tag in react object
- Redirect Hash URL to standard URL
- Cannot route to required React Component using React-Router-DOM
- Tailwindcss v3 not working with next.js v12
- how to store a function in React
- React Router 5. Component not rendered after redirect
- Way to add checkbox checked event to cross out in list
- 'useEffect', which lacks return-type annotation, implicitly has an 'any' return type.ts(7010)
- Is there a way to pass an imported component through a function?
- typeerror:null is not an object(evaluating cities.push)