Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@DesignByOnyx
Last active April 18, 2018 15:44
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 DesignByOnyx/e45696b579c762756c3bdf1093e8eb92 to your computer and use it in GitHub Desktop.
Save DesignByOnyx/e45696b579c762756c3bdf1093e8eb92 to your computer and use it in GitHub Desktop.
import React from 'react';
import DefineMap from 'can-define/map/map';
import ObservationRecorder from 'can-observation-recorder';
import Component from 'react-view-model/component';
class Parent extends Component {
static ViewModel = DefineMap.extend({});
render() {
console.log('Parent render');
return <Child time={new Date()} />;
}
}
class Child extends Component {
static ViewModel = DefineMap.extend({
time: 'date',
tick() {
this.time = new Date();
},
init() {
if(!this.time) this.tick();
},
// init: ObservationRecorder.ignore(function() {
// if(!this.time) this.tick();
// })
});
componentDidMount() {
super.componentDidMount();
this.interval = setInterval(this.viewModel.tick, 1000);
}
componentWillUnmount() {
super.componentWillUnmount();
clearInterval(this.interval);
}
render() {
console.log('Child render');
const { time } = this.viewModel;
return <div>The time: {time.toLocaleString()}</div>;
}
}
export default () => {
console.log('Container render');
return <Parent />;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment