admin管理员组文章数量:1326630
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 ) } />
);
};
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 ) } />
);
};
Share
Improve this question
asked Sep 29, 2020 at 8:39
Siddharth ThevarilSiddharth Thevaril
3,7983 gold badges39 silver badges74 bronze badges
2
-
Why not just use the
onBlur
handler? It's a property you can give the input that fires when it's blurred. Don't needuseEffect
or anything – Jayce444 Commented Sep 29, 2020 at 8:46 - 2 @Jayce444, onBlur will fire even when there's no change to the input field. This is why I emphasised on change + blur. – Siddharth Thevaril Commented Sep 29, 2020 at 8:49
2 Answers
Reset to default 6Ok, I think this should satisfy your needs. What you want to do is keep a transient record of their input before they started typing, then update it when they blur if and only if that input value has changed. You can do this with a ref, so something like this:
const App = () => {
const transientName = useRef('');
const [ name, setName ] = useState( '' );
const handleBlur = () => {
// This will only trigger if they blur AND the name has changed
if (transientName.current !== name) {
// Update the transient name value
transientName.current = name;
// Do other on blur analytics stuff
}
};
return (
<input type="text" value={ name } onChange={ ( e ) => setName( e.target.value ) } onBlur={handleBlur} />
);
};
This way, if they change the input and blur, it will fire, but if they blur the input without having changed the input value, it shouldn't fire your custom on blur code inside the if condition
This is similar to ponentDidUpdate
function YourComponent({ someProp }) {
useEffect(() => {
// code to run when someProp changes
}, [someProp]);
}
Which means it would update anytime your specified prop or state is changed, in your case I would do something like this:
const App = () => {
const [ name, setName ] = useState( '' );
useEffect( () => {
/**
* Run this function only on Update + Blur.
* This will contain the logic to send analytics.
*/
}, [name] );
return (
<input type="text" value={ name } onChange={ ( e ) => setName( e.target.value ) } />
);
};
This would trigger your specified function whenever your name value is updated.
本文标签: javascriptReactJSrun useEffect on Blur only if the input field is updatedStack Overflow
版权声明:本文标题:javascript - ReactJS - run useEffect on Blur only if the input field is updated - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742209901a2433516.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论