简体   繁体   English

React Native Redux防止在2分钟内重复执行两次操作

[英]React Native Redux prevent action from being repeated twice in 2 minutes

I'm developing a React Native Redux application and I've run into an issue where I'm struggling to figure out the cleanest most effective solution. 我正在开发React Native Redux应用程序,却遇到了一个问题,我在努力寻找最干净,最有效的解决方案。

I have a reducer which manages a modal like this: 我有一个减速器,它管理这样的模态:

import {
  OPEN_CHORDS_MODAL,
  CLOSE_CHORDS_MODAL
} from '../actions/types';

const INITIAL_STATE = {
  chordsModalIsOpen: false
};

export default (state = INITIAL_STATE, action) => {
  switch (action.type) {
    case OPEN_CHORDS_MODAL:
      return { chordsModalIsOpen: action.payload };
    case CLOSE_CHORDS_MODAL:
      return { chordsModalIsOpen: action.payload };
    default:
      return state;
  }
};

Then I have my actions for the modal here: 然后,我对模态执行以下操作:

import { FacebookAds } from 'expo';

import {
  OPEN_CHORDS_MODAL,
  CLOSE_CHORDS_MODAL
} from './types';

export const openChordsModal = () => ({
  type: OPEN_CHORDS_MODAL,
  payload: true
});

export const closeChordsModal = () => {
  FacebookAds.InterstitialAdManager.showAd('328225604270934_328677554225739')
    .then(console.log('Intestitial Ad shown'))
    .catch(err => console.log('Interstitial Ad Error', err));
  return { type: CLOSE_CHORDS_MODAL, payload: false };
};

Basically I'm looking at the closeChordsModal action. 基本上我正在看closeChordsModal动作。

I currently have an interstitial ad which displays every time the modal closes. 我目前有一个插页式广告,每次关闭模式时都会显示。 This works great but I want to improve the user experience by limiting the amount of times the interstitial ad pops. 效果很好,但我想通过限制插页式广告的弹出次数来改善用户体验。

I want to make sure that once the ad shows for the first time, it doesn't show again for at least another 2 minutes. 我想确保一旦广告首次展示,至少再过2分钟就不会再次展示。

ie The user opens and closes the modal, the ad appears (currently what's happening). 即,用户打开和关闭模式,广告就会显示(当前正在发生什么)。 Then the user opens and closes the modal again, if it has been within 2 minutes since the last close modal action, just close the modal, Otherwise close the modal and show the interstitial ad. 然后,如果距离上一次关闭模态操作已经不到2分钟,则用户再次打开并关闭模态,只需关闭模态,否则关闭模态并显示插页式广告。

How can I accomplish that? 我该怎么做? Thanks a lot! 非常感谢!

Since the solution involves Date to wait for the 2 minutes, we can't do that in the reducer, because reducers have to be pure, no side effect or external dependency (yes, time is an external dependency). 由于解决方案涉及Date来等待2分钟,因此我们无法在化简器中这样做,因为化简器必须是纯正的,没有副作用或外部依赖性(是的,时间是外部依赖性)。

In your action, every time you call closeChordsModal you save the current time + 2 minutes. 在执行操作时,每次调用closeChordsModal都会节省当前时间+ 2分钟。

const doNotShowAnythingUntil = new Date(new Date().getTime() + 2*60*1000);

then every time you call that action again you check that 然后每次您再次调用该操作时,您都会检查

if( new Date() < doNotShowAnythingUntil ) { do nothing }

In this way, your reducer is still pure and the action is taking care of the behaviour. 这样,您的减速器仍然是纯净的,并且动作在照料行为。

Code example: 代码示例:

import ...
import ...

export ...

let expirationDate;

export const closeChordsModal = () => {
    if(!expirationDate 
       || new Date() > expirationDate
    ) {
        expirationDate = new Date(
            new Date().getTime() + 2*60*1000
        );
        FacebookAds.InterstitialAdManager.showAd....
    }
}

React Native implements the browser timers. React Native 实现了浏览器计时器。 Maybe you can try something like this: 也许您可以尝试这样的事情:

export class ModalContainer extends Component {
  state = {
    blockAds: false,
  };

  enableAds() {
    setTimeout(
      () =>
        this.setState({
          blockAds: false,
        }),
      , 120000
    );
  }

  displayFacebookAds = () => {
    if(!this.state.blockAds) {
      *Your code to dispatch FacebookAd action*
      this.setState({ blockAds: true });
      this.enableAds();
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM