score:4
Accepted answer
just store the original value in a ref and compare it to the value provided during render:
const {useref, usestate} = react;
// you can implement the value comparison using your preferred method
function arevaluesequal (value1, value2) {
return object.is(value1, value2);
}
/**
* ts:
* function usevaluechanged <t>(value: t): boolean
*/
function usevaluechanged (value) {
const originalref = useref(value);
return !arevaluesequal(originalref.current, value);
}
function example () {
const rendercountref = useref(0);
rendercountref.current += 1;
const [count, setcount] = usestate(0);
const increment = () => setcount(n => n + 1);
const didchange = usevaluechanged(count);
return (
<div>
<div>render count: {rendercountref.current}</div>
<div>changed: {didchange ? 'yes' : 'no'}</div>
<button onclick={increment}>clicks: {count}</button>
</div>
);
}
reactdom.render(<example />, document.getelementbyid('root'));
<script src="https://unpkg.com/react@17.0.2/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@17.0.2/umd/react-dom.development.js"></script>
<div id="root"></div>
Source: stackoverflow.com
Related Query
- How to return mutated values from a custom React hook?
- How can I mock return data from a custom hook in React/Jest?
- How do I return values from a React component?
- How to return values from UseEffect Hook
- how to return values from a Laravel Sever to React with inertiajs?
- How to create custom validation from react hook form?
- How to fetch data from a custom React hook (API) with onClick and display it in a Div using TypeScript?
- How to mock react custom hook return value as a module with Jest
- React hook form how to pass value from custom component to controller with already assign own on change?
- How do I return data from a custom react hook?
- How to return values from a react component to the calling module?
- How to properly export a component from a React custom hook and a function to control it?
- How to return custom component from another function in react JS?
- How to use the return value from React hook updater function?
- Early return from react useEffect custom hook is not getting destructure
- How to inject parameters into return function with custom hook react
- How to enrich data from custom react hook
- How to convert values from input radio in boolean in React Hook Form?
- how to return a function from react hook which internally uses a promise to get the value
- How get multiple values from checkbox list into state using React useState hook
- How to return boolean value from custom function in react
- How to mock react custom hook returned value?
- How to get values from child components in React
- How to mock a custom hook inside of a React component you want to test?
- react / redux-form: how to return promise from onSubmit?
- How to memoize custom React hook
- How to use React Context with useState hook to share state from different components?
- How to set multiple values at once in react hook form using Typescript
- How to compare values from react redux state in hooks
- How to return a React memoized callback from factory function
More Query from same tag
- React dropzone, how to upload image?
- Transparency to Opacity conversion
- How to manage a selector that accepts arguments with yield select?
- How to pass an array with multiple values in the body of jsPDF?
- Can I access the props or state of a Pagination component being generated by an Ant Design Table?
- Render single icon on hover using map - ReactJs
- With React, clicking on a label is not changing the input type checkbox checked value. Why?
- Is it possible to use an object type definition to construct a new array/tuple type?
- How to mock "new Date()" which is inside functional component?
- How to access event.target.value from React component
- Async throws error in React Functional Component
- What is the difference between HOC and lifting state up in ReactJS?
- Is it possible to disable specific React warnings from Jest (using Create React App)
- How to maintain the hover effect of the sibling in Material-UI
- React. Private router with async fetch request
- Render Component every time it's get clicked
- TypeError: cannot read property 'onMarksSelection' of undefined. When trying to attach a event listener to a tableau viz using React js
- Create three.js components with map method
- React, onChange and onClick events fires simultaneously
- How do I trace React Emotion Styled Components back to their location in the source code
- How can I code the result of this api request in visual studio
- How Apollo Query component use it's self value as callback
- Add to Cart Item functionality is not working in my react-redux
- react-i18next: How can I get text in a language, that is not my current selected one?
- How to create external download link in React?
- Proxyquire with webpack don't compile
- <select> does not change its selected <option> when setting a default value
- CORS Error: LinkedIn Authentication; .NET Core 5 REST Api
- How to check if scroll is near the bottom
- Cancel previous fetch request with redux-thunk