首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >react-toolbox snackbar的任何隐藏事件?

react-toolbox snackbar的任何隐藏事件?
EN

Stack Overflow用户
提问于 2017-03-31 11:25:46
回答 1查看 314关注 0票数 0

我正在使用Redux的react-toolkit Snackbar组件。每个snackbar都被建模为Redux商店中的一个对象。但我想在每次通知超时或被清除/隐藏时删除此对象。我该怎么做呢?

每次添加通知时,是否需要手动将onTimeout设置为dispatch一个事件?

如果是这样,有没有一种方法可以将此派单添加到中心位置,而不是将其添加到我派此操作的所有位置?

EN

回答 1

Stack Overflow用户

发布于 2017-04-21 02:10:56

也许你可以在这里使用更高阶的组件。

SnackbarWrapper.js

代码语言:javascript
复制
import React from 'react';
import PropTypes from 'prop-types';

const SnackbarWrapper = (Component) => {
  class Decorated extends React.Component {
    constructor(props, context) {
      super(props, context);
      this.state = {
        active: props.active
      };
      this.handleSnackbarClick = this.handleSnackbarClick.bind(this);
      this.handleSnackbarTimeout = this.handleSnackbarTimeout.bind(this);
    }
    componentWillReceiveProps(nextProps) {
      // snackbar activated from outside
      if (this.props.active !== nextProps.active) {
        this.setState({ active: true });
      }
    }
    handleSnackbarClick(event, instance) {
      // snackbar dismissed from inside
      this.setState({ active: false });
      // handle the dispatch in callback function
      this.props.cb();
    }
    handleSnackbarTimeout(event, instance) {
      // snackbar dismissed from inside
      this.setState({ active: false });
      // handle the dispatch in callback function
      this.props.cb();
    }
    render() {
      const { action, label, type, timeout } = this.props;
      return (
        <Component
          action={action}
          label={label}
          type={type}
          timeout={timeout}
          active={this.state.active}
          onClick={this.handleSnackbarClick}
          onTimeout={this.handleSnackbarTimeout}
        />
      );
    }
  }

  Decorated.propTypes = {
    action: PropTypes.string.isRequired,
    label: PropTypes.string.isRequired,
    type: PropTypes.string.isRequired,
    timeout: PropTypes.number.isRequired,
    cb: PropTypes.func.isRequired,
    active: PropTypes.bool.isRequired
  };

  return Decorated;
};

export default SnackbarWrapper;

在组件中

代码语言:javascript
复制
import SnackbarWrapper from './SnackBarWrapper';
const RTSnackbar = SnackbarWrapper(Snackbar);
...
<RTSnackbar
  action="Dismiss"
  label="Snackbar action cancel"
  type="cancel"
  timeout={2000}
  cb={this.actionCb}
  active={this.state.active}
 />
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43131892

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档