Skip to content

Instantly share code, notes, and snippets.

@x7c1
Created December 10, 2017 16:50
Show Gist options
  • Select an option

  • Save x7c1/2f84d3bfc11c67ab609be94c26084a7a to your computer and use it in GitHub Desktop.

Select an option

Save x7c1/2f84d3bfc11c67ab609be94c26084a7a to your computer and use it in GitHub Desktop.
redux-seal middleware
import { Sealer } from 'redux-seal/Sealer';
import { increase } from './actions';
export const counterButton = () => class CounterButton extends Sealer {
onClick = () => {
this.dispatch(increase(1));
};
get label() {
return `increment counter : ${this._currentCount}`;
}
get _currentCount() {
return this.state.sampleCounter.count;
}
};
import { Sealer } from './Sealer';
export const seal = ({ dispatch, getState }) => next => action => {
if (action.prototype instanceof Sealer) {
return new action({ dispatch, getState });
}
return next(action);
};
import React from 'react';
import { connect } from 'react-redux';
import { counterButton } from './counterButton';
const SamplePageView = props => {
const button = props.counterButton();
return (
<div className='react-sample-area'>
<button type='button' onClick={button.onClick}>
{button.label}
</button>
</div>
);
};
const mapStateToProps = state => state;
const mapDispatchToProps = {
counterButton,
};
export const SamplePage = connect(mapStateToProps, mapDispatchToProps)(SamplePageView);
export class Sealer {
constructor({ dispatch, getState }) {
this.dispatch = dispatch;
this.getState = getState;
}
get state() {
return this.getState();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment