admin管理员组文章数量:1336346
I'm trying to dive deeper into rxjs and found an issue where the input field I'm trying to debounce dispatches an event on every keypress, the debounce only holds the output but results in a tree like:
a
as(delay - waits 200ms, then fires the rest synchronously)
asd
asdf
asdfg
....
The same code works as expected in a class ponent() but cannot understand why it doesn't work with stateless ponents. Here's an example: - you can see the useState update fires for every keystroke.
Thanks a lot.
I'm trying to dive deeper into rxjs and found an issue where the input field I'm trying to debounce dispatches an event on every keypress, the debounce only holds the output but results in a tree like:
a
as(delay - waits 200ms, then fires the rest synchronously)
asd
asdf
asdfg
....
The same code works as expected in a class ponent(https://stackoverflow./a/44300853/1356046) but cannot understand why it doesn't work with stateless ponents. Here's an example: https://stackblitz./edit/react-hzhrmf - you can see the useState update fires for every keystroke.
Thanks a lot.
Share Improve this question asked Oct 23, 2019 at 10:32 SinSyncSinSync 4881 gold badge6 silver badges17 bronze badges2 Answers
Reset to default 7React continuously calls your function to render the ponent. Therefore the Subject is continuously recreated.
Using a factory with useState to keep the subject and working with useEffect to make sure the subscription is only made once should fix your issue.
Something like this :
import React, { Component, useState, useEffect, useRef } from 'react';
import { render } from 'react-dom';
import { debounceTime, map, tap, distinctUntilChanged } from 'rxjs/operators';
import { fromEvent, Subject } from 'rxjs';
import './style.css';
const App = props => {
const [queryName, setQueryName] = useState("");
const [debouncedName, setDebouncedName] = useState("");
const [onSearch$] = useState(()=>new Subject());
useEffect(() => {
const subscription = onSearch$.pipe(
debounceTime(400),
distinctUntilChanged(),
tap(a => console.log(a))
).subscribe(setDebouncedName);
}, [])
const handleSearch = e => {
setQueryName(e.target.value);
onSearch$.next(e.target.value);
};
return (
<div>
<input
placeholder="Search Tags"
value={queryName}
onChange={handleSearch}
/>
<p>Debounced: {debouncedName}</p>
</div>
);
}
render(<App />, document.getElementById('root'));
Here is a version as custom Hook as this might be used multiple times in a form and can otherwise clutter your code.
function useDebounce<T = any>(time: number, defaultValue: T): [T, (v: T) => void] {
let [value, setValue] = React.useState<T>(defaultValue);
let [value$] = React.useState(() => new Subject<T>());
React.useEffect(() => {
let sub = value$.pipe(debounceTime(time)).subscribe(setValue);
return () => sub.unsubscribe();
}, [time, value$]);
return [value, (v) => value$.next(v)];
}
//useage:
let [value,setValue] = useDebounce(200,"");
本文标签:
版权声明:本文标题:javascript - Rxjs debounce on react text input component using Subjects does not batch input text on statelessfunctional compone 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742236968a2438255.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论