javascript - 通过单击调用停止时钟的“停止”功能
问题描述
预期效果:
- 单击名称 A。名称 A 处于活动状态
- 当我单击“Name B”元素时,我想调用“stopTimer ()”函数。在元素“Name A”中停止时间并运行函数“stopTimer()”
当我单击诸如“名称 A”之类的元素时,名称 A 处于活动状态,在单击“名称 B”之前,我无法调用“stopTimer ()”函数
当项目“名称 B”处于活动状态时,反之亦然。点击项目'Name A'调用函数'stopTimer()'
这个解决方案有可能吗?我正在征求意见。
更新:
所有代码:https ://stackblitz.com/edit/react-kxuvgn
我明白我的错误。我应该把函数stopTimer ()
放在 parent 中App
。当我单击时,stopTimer
都需要该功能App
Stopwatch
组件中插入stop
按钮。我应该在哪里设置:{timerOn: false}); clearInterval (this.timer);
这两个组件都很常见?
stopTimer = () => {
this.setState ({timerOn: false});
clearInterval (this.timer);
};
应用程序
class App extends React.Component {
constructor() {
super();
this.state = {
selectedTodoId: '',
selectedTabId: null,
items: [
{
id: 1,
name: 'A',
description: 'Hello'
},
{
id: 2,
name: 'B',
description: 'World'
}
],
selectIndex: null
};
}
stopTimer = (timer, timerOn) => {
this.setState({ timerOn: timerOn });
clearInterval(timer);
};
select = (id) => {
if(id !== this.state.selectedTabId){
this.setState({
selectedTodoId: id,
selectedTabId: id
})
this.stopTimer();
}
}
isActive = (id) => {
return this.state.selectedTabId === id;
}
render() {
return (
<div>
<ul>
{
this.state.items
.map((item, index) =>
<Item
key={index}
index={index}
item={item}
select={this.select}
items = {this.state.items}
selectIndex = {this.state.selectIndex}
isActive= {this.isActive(item.id)}
/>
)
}
</ul>
<ItemDetails
items = {this.state.items}
selectIndex = {this.state.selectIndex}
resul={this.state.resul}
/>
<Stopwatch
stopTimer = {this.stopTimer}
/>
</div>
);
}
}
手表
class Stopwatch extends Component {
constructor() {
super();
this.state = {
timerOn: false,
timerStart: 0,
timerTime: 0
};
}
startTimer = () => {
this.setState({
timerOn: true,
timerTime: this.state.timerTime,
timerStart: Date.now() - this.state.timerTime
});
this.timer = setInterval(() => {
this.setState({
timerTime: Date.now() - this.state.timerStart
});
}, 10);
};
stopTimer = () => {
this.setState({ timerOn: false });
clearInterval(this.timer);
};
resetTimer = () => {
this.setState({
timerStart: 0,
timerTime: 0
});
};
render() {
const { timerTime } = this.state;
let centiseconds = ("0" + (Math.floor(timerTime / 10) % 100)).slice(-2);
let seconds = ("0" + (Math.floor(timerTime / 1000) % 60)).slice(-2);
let minutes = ("0" + (Math.floor(timerTime / 60000) % 60)).slice(-2);
let hours = ("0" + Math.floor(timerTime / 3600000)).slice(-2);
return (
<div>
<div className="Stopwatch-display">
{hours} : {minutes} : {seconds}
</div>
{ (
<button onClick={this.startTimer}>Start</button>
)}
{(
<button onClick={this.stopTimer}>Stop</button>
)}
{this.state.timerOn === false && this.state.timerTime > 0 && (
<button onClick={this.resetTimer}>Reset</button>
)}
</div>
);
}
}
解决方案
为此,您必须提升您的状态,在子卸载之前,将值传递给父组件进行存储。
componentWillUnmount() {
this.stopTimer();
this.props.updateTimerTime(this.props.index, this.state.timerTime);
}
当子组件挂载时,从父组件传递的道具中设置状态。
componentDidMount() {
this.setState({
timerTime: this.props.timerTime,
});
}
推荐阅读
- c++ - 如何让可执行文件加载系统库,例如从本地路径加载 libpng
- javascript - jQuery 不会在 IE 上运行,普通的 JS 也不会
- visual-studio-2017 - 如何将代码分析错误 CA1506 类耦合更改为“中等可维护性”
- firebase - 无法从 3.19.1 更新 firebase 工具
- c# - c# asp.net网站调用一个webApi
- html - 总表/容器中的第 n 个孩子
- python-3.x - 通过 GeckoDriver Firefox 和 Selenium 执行测试时出现“线程在处理恐慌时出现恐慌。中止”错误
- powerbi - 如何使用 Power BI 在所有列中查找和替换
- java - 如何检查数字是否存在 JSON 字符串?
- vuejs2 - 将元素ui导入nuxt时出现样式错误