Last active
April 18, 2018 15:44
-
-
Save DesignByOnyx/e45696b579c762756c3bdf1093e8eb92 to your computer and use it in GitHub Desktop.
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 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