在项目中我使用的ui组件在下面的代码中是用伪时钟的形式表示的。
如何在不更改实际上是外部库的代码时钟的情况下使此代码工作。
最重要的是,该决定本着 mobx 的精神。
class ClockStore {
@observable data = [
{id: "hour", value: "00"},
{id: "min", value: "00"},
{id: "second", value: "00"},
];
@action
update(id, value){
this.data.find(item => item.id === id).value = value;
}
}
let clockStore = new ClockStore();
@inject('clockStore')
@observer
class App extends Component<any, any> {
render(){
return <Clock data={this.props.clockStore.data}></Clock>
}
}
// it's not my clock this component is taken from github
class Clock extends Component<any, any> {
render(){
return (
<div>
<ClockFace data={this.props.data} />
<div className="clock-control"></div>
</div>
)
}
}
class ClockFace extends Component<any, any> {
render(){
return (
<div className="clock-face">
{this.props.data.map( ( item, index ) => <ClockSection key={ index }>{ item.value }</ClockSection> )}
</div>
)
}
}
class ClockSection extends Component<any, any> {
render(){
return (
<span className="clock-section">{ this.props.children }</span>
)
}
}
let stores = { clockStore };
ReactDOM.render(
<Provider {...stores}>
<App />
</Provider>,
document.querySelector( 'main' )
);
// the code that sets the clock
let count = 0;
document.addEventListener( 'click', () => clockStore.update( 'hour', count ++ ) );
// --------------------------