Created
July 5, 2016 13:37
-
-
Save mk2/3eae78b3c9871b090efff1a741cb5c22 to your computer and use it in GitHub Desktop.
Reduxのアクションを綺麗に書く方法の提案 ref: http://qiita.com/mk2/items/5f9d65dbb149512fc468
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default class ActionSchema { | |
constructor(...props) { | |
Object.assign(this, { | |
...props, | |
}); | |
} | |
static get actionType() { | |
return this.name; | |
} | |
static action(props) { | |
return { | |
type: this.name, | |
...props, | |
}; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export class CreateMessage extends ActionSchema {} | |
export class DeleteMessage extends ActionSchema {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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