Skip to content

Instantly share code, notes, and snippets.

@xcodebuild
Last active October 26, 2017 03:06
Show Gist options
  • Save xcodebuild/22f0f3e0176844e47d5b6c80f43d91f3 to your computer and use it in GitHub Desktop.
Save xcodebuild/22f0f3e0176844e47d5b6c80f43d91f3 to your computer and use it in GitHub Desktop.
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'));
let initalState = {
count: 1,
};
export default {
onInit() {
this.state = initalState;
},
// life cycle
componentWillReceiveProps(nextProps) {
// ...
},
onCountAdd() {
this.setState({
...this.state,
count: this.state.count + 1,
});
},
};
import PropTypes from 'prop-types';
export default {
title: PropTypes.string.isRequired,
};
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;
}
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