Last active
August 29, 2015 14:20
-
-
Save suisho/5a69b34b1cb159e775db to your computer and use it in GitHub Desktop.
Reactで抽象クラスみたいなことをしたいときの解法 ref: http://qiita.com/suisho/items/0be6b17e04922418f482
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
class Icon extends React.Component{ | |
activeClass(){ | |
throw new Error("Not implemented error") | |
} | |
inactiveClass(){ | |
throw new Error("Not implemented error") | |
} | |
render(){ | |
var {key, active} = this.props; | |
const classes = active ? this.activeClass() : this.inactiveClass() | |
const cls = classNames("fa", classes); | |
return (<i | |
key={key} | |
className={cls} | |
/>); | |
} | |
} | |
class StarIcon extends Icon{ | |
activeClass(){ | |
return "fa-star" | |
} | |
inactiveClass(){ | |
return "fa-star-o" | |
} | |
} |
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
var props = {}; | |
props.foo = x; | |
props.bar = y; | |
var component = <Component {...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
Icon.PropTypes = { | |
clssName: React.PropTypes.string, | |
key: React.PropTypes.string, | |
} | |
StarIcon.PropTypes = { | |
active: React.PropTypes.boolean | |
} | |
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
class Icon extends React.Component{ | |
render(){ | |
var {key, classNames} = this.props; | |
const classes = cx("fa", classNames); | |
return (<i | |
key={key} | |
className={classes} | |
/>); | |
} | |
} | |
class StarIcon extends React.Component{ | |
render(){ | |
var classNames = this.props.active ? "fa-star" : "fa-star-o" | |
return (<LevelIcon { ...this.props } | |
classNames={classNames} />) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment