The task is to send analytics data only if the input is updated and blurred.
Natively, this can be achieved by using the onchange event which only fires the event if the input is updated and loses focus.
React works differently such that onChange fires on every change to the input. Does React provide any out of the box solution for this?
The code is pretty simple:
const App = () => {
const [ name, setName ] = useState( '' );
useEffect( () => {
/**
* Run this function only on Update + Blur.
* This will contain the logic to send analytics.
*/
} );
return (
<input type="text" value={ name } onChange={ ( e ) => setName( e.target.value ) } />
);
};