Skip to content

Instantly share code, notes, and snippets.

@mk2
Created July 5, 2016 13:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mk2/3eae78b3c9871b090efff1a741cb5c22 to your computer and use it in GitHub Desktop.
Save mk2/3eae78b3c9871b090efff1a741cb5c22 to your computer and use it in GitHub Desktop.
Reduxのアクションを綺麗に書く方法の提案 ref: http://qiita.com/mk2/items/5f9d65dbb149512fc468
export default class ActionSchema {
constructor(...props) {
Object.assign(this, {
...props,
});
}
static get actionType() {
return this.name;
}
static action(props) {
return {
type: this.name,
...props,
};
}
}
export class CreateMessage extends ActionSchema {}
export class DeleteMessage extends ActionSchema {}
import { CreateMessage, DeleteMessage } from './schema';
export function createMessage({messageType, content}) {
const message = {
type: messageType,
content,
}
return dispatch => {
dispatch(CreateMessage.action({message}));
setTimeout(function () {
dispatch(DeleteMessage.action({message}));
}, 10000);
};
}
import { CreateMessage, DeleteMessage } from '../actions/schema';
export default function messages(state = [], { type, ...rest }) {
switch (type) {
case CreateMessage.actionType:
...
case DeleteMessage.actionType:
...
default:
return state;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment