- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 React 和 mobx 开发一些东西。
我创建了一个 ImageCarousel 组件,在其中显示单击的图像。我有一个上一个和一个下一个按钮(它们本身就是一个组件),用于在图像之间移动。
我尝试用 lodash debounce
包装这些操作(上一个和下一个),但途中出现了一些问题。
我当前的商店有这些操作:
上一页轮播
nextCarousel
debounceAction
debounceAction
只是一个高阶函数,它获取 2 个参数(fn、wait),并使用这些参数调用 lodash debounce
。
我的 CarouselButton 组件通过其 props 实现我上面提到的那些操作。在组件内部,我使用 onClick
事件触发,以使用实际操作(prev 或 next)。
我不确定如何以正确的方式用去抖来包装我的 Action 。
我在第二个代码片段(在CarouselButton 组件中)中调用debounceAction
(包装去抖的HOF)。
你看到我的错误了吗?
galleryStore.jsx - 当前商店:
class GalleryStore {
// Observables
@observable images = [
// ....images
];
@observable carouselleButtons= {
next: "fa fa-chevron-right",
back: "fa fa-chevron-left"
}
@observable selectedImageIndex = null;
@observable hideCarousel = true;
@observable onTransition = false;
// Actions
selectImage = (index) =>{
this.selectedImageIndex = index;
}
toggleCarousel = () =>{
this.hideCarousel = !this.hideCarousel;
}
carouselNext = () => {
if(this.selectedImageIndex == this.images.length - 1) {
return;
}
this.onTransition = true;
setTimeout(() => {
this.selectedImageIndex = this.selectedImageIndex + 1;
this.onTransition = false;
},500)
}
carouselPrev = () => {
if(this.selectedImageIndex == 0) {
console.log('start of the collection');
return;
}
this.onTransition = true;
setTimeout(() => {
this.selectedImageIndex = this.selectedImageIndex - 1;
this.onTransition = false;
}, 500)
}
debounceAction = (fn, wait) => {
//lodash's debounce
return debounce(fn, wait);
}
carouselButton 组件 - 此处我调用去抖动:
// React's
import React from 'react';
// Styles
import CarouselButtonStyle from './carouselButtonStyle';
// CarouselButton Component
export default class CarouselButton extends React.Component {
debounce = (e) =>{
const { debounceAction } = this.props;
// -----> HERE I CALL DEBOUNCE ! <---------
e.stopPropagation();
debounceAction(this.buttonHandler, 400);
}
buttonHandler = (e) => {
const {limit, index, action, debounceAction} = this.props;
if(index == limit) return;
else action();
}
render(){
const {limit, index, icon, action, debounceAction} = this.props;
return(
<CarouselButtonStyle
onClick={(e) => {this.debounce(e)}}
className={ index == limit ? 'end-of-collection' : '' } >
<i className={icon} aria-hidden="true" />
</CarouselButtonStyle>
);
}
}
imageCarousel.jsx - carouselButton 父组件:
// React's
import React from 'react';
// Mobx-react's
import { observer, inject } from 'mobx-react';
// Styles
import ImageCarouselStyle from './imageCarouselStyle';
// Components
import ImgContainer from './imgContainer/imgContainer';
import CarouselButton from './carouselButton/carouselButton';
// ImageCarousel Component
@inject('galleryStore')
@observer
export default class ImageCarousel extends React.Component {
closeCarousel = () => {
this.props.galleryStore.toggleCarousel();
}
onKeyDown = (e) => {
const { keyCode } = e;
if(keyCode ===27) this.onEscHandler();
else if (keyCode == 37) this.onLeftArrow();
else if (keyCode == 39) this.onRightArrow();
else return;
}
onLeftArrow = () => { this.props.galleryStore.carouselPrev() }
onRightArrow = () => { this.props.galleryStore.carouselNext() }
onEscHandler = () => { this.closeCarousel() }
componentDidMount(){
document.addEventListener('keydown', this.onKeyDown, false);
}
render(){
return(
<ImageCarouselStyle
hidden={this.props.galleryStore.hideCarousel}
onClick={this.closeCarousel} >
<CarouselButton action={'prev'}
icon={this.props.galleryStore.carouselleButtons.back}
action={this.props.galleryStore.carouselPrev}
limit={0}
index={this.props.galleryStore.selectedImageIndex}
debounceAction={this.props.galleryStore.debounceAction} />
<ImgContainer
images={this.props.galleryStore.images}
imgIndex={this.props.galleryStore.selectedImageIndex}
onTransition={this.props.galleryStore.onTransition}/>
<CarouselButton action={'next'}
icon={this.props.galleryStore.carouselleButtons.next}
action={this.props.galleryStore.carouselNext}
limit={this.props.galleryStore.amountOfImages}
index={this.props.galleryStore.selectedImageIndex}
debounceAction={this.props.galleryStore.debounceAction} />
</ImageCarouselStyle>
);
}
}
最佳答案
问题是您必须从 CarouselButton
的 debounce
方法返回 debounceAction
。
debounce = (e) =>{
const { debounceAction } = this.props;
// -----> HERE I CALL DEBOUNCE ! <---------
e.stopPropagation();
debounceAction(this.buttonHandler, 400);
// -^^^ must return here
}
但是,我建议更进一步,以避免将来出现困惑。只需在需要时调用 lodash 的 debounce
,而不是在代码中多次重写它并导致有问题的方法名称。
以下是如何包装点击处理程序的最基本示例:
class Button extends React.Component {
handleClick = _.debounce((e) => {
alert('debounced click reaction')
}, 1000)
render() {
return <button onClick={this.handleClick}>CLICK ME</button>
}
}
ReactDOM.render(
<Button />,
document.getElementById('app')
);
<div id="app"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>
关于javascript - 为什么 debounce 不调用我的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47553486/
我有两个可观察对象: Observable O(open): 包含一些内容的文件在 textview 中打开 Observable E(edit):在textview中编辑的文件内容 我想去抖动 E
当我尝试使用 JavaScript 的“debounce” 时,如下所示, debounce(() => { this.getDataFn(true); }, 3000); 收到诸如debou
我有 2 个微服务在 Azure 上的 Kubernetes 集群中运行。当项目更新时,“项目”微服务会向 Kafka 发送一个事件。 “分析”微服务使用该事件,进行一些昂贵的计算,最后发送包含结果的
在我的应用程序中,我有一个返回这样的可观察对象的服务: public genericService(params) { //Do some stuff //... return
背景 假设您有一个应用程序,当您发出请求时,您会收到大量信息。在我关于餐馆的应用程序中,我想在每次收到包含几家餐馆的请求时计算所有菜单的平均价格: var avg = menus => { con
这个问题在这里已经有了答案: Write async function with Lodash in a Vuejs component (2 个答案) 关闭 4 年前。 如何在 async 函数上
有什么方法可以检查去抖功能是否处于挂起状态? 通过使用源码分析,我发现只有两种方法:flush和 cancel . 最佳答案 更新 1 debounce的pending方法只能在 master 上访问
我正在尝试通过 React 中的输入更改来消除发送 Redux Action 的抖动。 const debouncedSubmit = debounce(() => dispatch(new Task
我试图在调整窗口大小时触发一个事件,它似乎不起作用。 $(window).resize(function(){ _.debounce(function(){ console.log
我想我发现了与 Swift Combine 的去抖操作符相关的内存泄漏。这要么是泄漏,要么我做错了什么。这是一个重现问题的简单示例: @IBOutlet weak var currentVal
我正在使用此代码: function resizeJquerySteps() { $('.wizard .content').animate({ height: $('.bod
我正在使用 React 和 mobx 开发一些东西。 我创建了一个 ImageCarousel 组件,在其中显示单击的图像。我有一个上一个和一个下一个按钮(它们本身就是一个组件),用于在图像之间移动。
我正在尝试debounce a function using Lodash ,当它调用该函数时,它似乎根本没有消除它。我的问题似乎与我在其他地方看到的错误不同on SO或 Google(通常,他们不会
我正在使用 Ben Alman 的 Throttle-debounce 插件。 当我这样调用 .throttle 时: $(window).scroll($.throttle(250, functio
我已经创建了一个自定义异步验证器,它使用一项服务来根据服务器验证电子邮件。然而,这意味着每次输入一个字符时服务器都会被命中,这是不好的。我在这里遵循了几个我无法开始工作的答案。 我的验证器: impo
我正在处理用于过滤一组数据的多个复选框。但是,我不希望复选框在每次单击复选框后触发过滤器,因此我想对其进行去抖处理。在选择最后一个复选框后,可能要等待 500 毫秒到一秒。 查看我的plnkr
这是我的代码( Angular 2): GO! debouncedFunc = _.debounce(()=>{ console.log('bam') }, 1000, {"leading
我已经创建了一个自定义异步验证器,它使用一项服务来根据服务器验证电子邮件。然而,这意味着每次输入一个字符时服务器都会被命中,这是不好的。我在这里遵循了几个我无法开始工作的答案。 我的验证器: impo
我正在尝试使用函数 _.debounce()的underscore.js但我不能正确地做到这一点。 我正在尝试消除窗口滚动的抖动,如下所示,但我很困惑。 $(document).ready(funct
当我运行此代码时,我在控制台中看不到任何控制台日志。 debounce 方法(取自 here )根本不执行该方法吗? function debounce(func, wait, immediate)
我是一名优秀的程序员,十分优秀!