Last active
October 26, 2017 03:06
-
-
Save xcodebuild/22f0f3e0176844e47d5b6c80f43d91f3 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 view from './view'; | |
| import model from './model'; | |
| import proptypes from './proptypes'; | |
| import { merge } from './utils'; | |
| import { render } from 'react-dom'; | |
| const Comp = merge(view, model, proptypes); | |
| render(<Comp title="this is title" />, document.getElementById('root')); |
This file contains hidden or 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
| let initalState = { | |
| count: 1, | |
| }; | |
| export default { | |
| onInit() { | |
| this.state = initalState; | |
| }, | |
| // life cycle | |
| componentWillReceiveProps(nextProps) { | |
| // ... | |
| }, | |
| onCountAdd() { | |
| this.setState({ | |
| ...this.state, | |
| count: this.state.count + 1, | |
| }); | |
| }, | |
| }; | |
This file contains hidden or 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 PropTypes from 'prop-types'; | |
| export default { | |
| title: PropTypes.string.isRequired, | |
| }; | |
This file contains hidden or 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 const merge = (view, model, proptypes) => { | |
| class MergedComponent extends React.Component { | |
| constructor() { | |
| super(...arguments); | |
| Object.keys(model).forEach(key => { | |
| this[key] = model[key].bind(this); | |
| }); | |
| this.onInit(); | |
| } | |
| render() { | |
| return view.call(this); | |
| } | |
| } | |
| if (proptypes) { | |
| MergedComponent.propTypes = proptypes; | |
| } | |
| return MergedComponent; | |
| } |
This file contains hidden or 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 React from 'react'; | |
| // do not use arrow function here | |
| export default function(){ | |
| const { | |
| count, | |
| } = this.state; | |
| const { | |
| title, | |
| } = this.props; | |
| const { | |
| onCountAdd, | |
| } = this; | |
| return ( | |
| <div> | |
| {title} | |
| {count} | |
| <button onClick={onCountAdd} /> | |
| </div> | |
| ); | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment