我有一个组件(执行文本输入)在另一个组件中渲染了两次。
当我在其中一个文本输入组件中按下一个键时,每个都会收到通知......
该组件需要一个时间字符串,例如"14:30"
,并且应该允许输入小时和分钟部分。
回流作用:
let TimeActions = Reflux.createActions([
'setHour',
'setMinute'
]);
回流混合:
let TimeMixin = {
init: function() {
this.listenToMany(TimeActions);
},
onSetHour(h) {
this.time.hour = h;
this.trigger(this.time);
},
onSetMinute(m) {
this.time.minute = m;
this.trigger(this.time);
}
};
反应组件:
export default class TimePicker extends React.Component {
constructor(props) {
super(props);
let parts = this.props.time.split(':'),
time = {
hour: parts[0],
minute: parts[1]
};
this.store = Reflux.createStore({
mixins: [ TimeMixin ],
time: time
});
this.state = time;
}
componentDidMount() {
this.unsubscribe = this.store.listen(this.onTimeChanged.bind(this));
}
componentWillUnmount() {
this.unsubscribe();
}
onTimeChanged(time) {
console.log('TIME SET TO', time);
this.setState(time);
}
render() {
let classes = classNames('time-picker');
return (
<div className={classes}>
<table>
<tbody>
<tr>
<td>
<input type="text" className="hour" maxLength="2"
value={this.state.hour}
onChange={this.onHourChanged} />
</td>
<td class="separator">:</td>
<td>
<input type="text" className="minute" maxLength="2"
value={this.state.minute}
onChange={this.onMinuteChanged} />
</td>
</tr>
</tbody>
</table>
</div>
);
}
onHourChanged(event) {
TimeActions.setHour($(this.getDOMNode()).val());
}
onMinuteChanged(event) {
TimeActions.setMinute($(this.getDOMNode()).val());
}
}
我从另一个 React 组件渲染了TimePicker
两次......
...
<TimePicker time={this.props.from} />
<TimePicker time={this.props.to} />
...
...并且在"01"
任何实例的分钟输入中键入时,我得到以下日志:
TIME SET TO Object {hour: "08", minute: "01"}
TIME SET TO Object {hour: "12", minute: "01"}
知道我做错了什么吗?