React:这个番茄钟如何通过`freecodecamp tests 12 and 13`?

在这个 React Pomodoro 时钟中,每次setInterval调用都减少了。然而,当试图通过 freecodecamp 测试 12 和 13 时,会出现一条消息:。然而,当时钟运行时,代码似乎运行良好。任何帮助将不胜感激。测试套件也在代码沙箱中:https ://codesandbox.io/s/xenodochial-fog-981wldownstate.timeLeftSecondsBreak time didn't start with the correct value.: expected 60 to be at most 5

索引.html


<!DOCTYPE html>

<html dir="ltr">

<head>

  <meta charset="utf-8">

  <title>25-5 Clock</title>

  <style>

  </style>

</head>

<body>

  <main>

    <div id="app"></app>

    </main>

  </body>

  </html>




烙印99
浏览 108回答 1
1回答

慕慕森

这些测试失败的原因是因为timeLeftSeconds一旦时钟进入“中断”周期,状态就没有正确设置,反之亦然。我在下面做了必要的调整。decreaseCurrentSecond注意和方法的变化down。此外,永远不要像您在此处所做的那样尝试直接更新状态中的值 -&nbsp;decreaseCurrentSecond = () => this.state.timeLeftSeconds--;。这不是好的做法——为什么我不能直接修改组件的状态,真的吗?import React from "react";import ReactDOM from "react-dom";import "./style.css";/*&nbsp;* A simple React component&nbsp;*/const initState = {&nbsp; breakLength: 0.5,&nbsp; sessionLength: 0.5,&nbsp; init: "session",&nbsp; // stateIndex: 0,&nbsp; timeLeft: undefined,&nbsp; timeLeftSeconds: undefined,&nbsp; started: false,&nbsp; intervalFunc: undefined};const secondsToMins = (time) => {&nbsp; let converted =&nbsp; &nbsp; ("0" + Math.floor(time / 60)).slice(-2) +&nbsp; &nbsp; ":" +&nbsp; &nbsp; ("0" + Math.floor(time % 60)).slice(-2);&nbsp; return converted;};class Clock extends React.Component {&nbsp; constructor(props) {&nbsp; &nbsp; super(props);&nbsp; &nbsp; this.state = initState;&nbsp; &nbsp; this.breakDecrement = this.breakDecrement.bind(this);&nbsp; &nbsp; this.breakIncrement = this.breakIncrement.bind(this);&nbsp; &nbsp; this.sessionDecrement = this.sessionDecrement.bind(this);&nbsp; &nbsp; this.sessionIncrement = this.sessionIncrement.bind(this);&nbsp; &nbsp; this.startStop = this.startStop.bind(this);&nbsp; &nbsp; this.reset = this.reset.bind(this);&nbsp; }&nbsp; componentDidMount() {&nbsp; &nbsp; let sessionSeconds = this.state.sessionLength * 60;&nbsp; &nbsp; this.setState({ timeLeftSeconds: sessionSeconds });&nbsp; &nbsp; this.setState({ timeLeft: secondsToMins(sessionSeconds) });&nbsp; }&nbsp; breakDecrement() {&nbsp; &nbsp; console.log("break decrement");&nbsp; &nbsp; // decrements the breakLength and the breakSeconds&nbsp; &nbsp; // breakLength is only a number ie. 5 (does not show seconds)&nbsp; &nbsp; // breakSeconds is that nunber converted into seconds&nbsp; &nbsp; let breakLength = this.state.breakLength - 1;&nbsp; &nbsp; if (breakLength > 0 && breakLength < 61) {&nbsp; &nbsp; &nbsp; this.setState({ breakLength: breakLength });&nbsp; &nbsp; &nbsp; // let breakSeconds = breakLength * 60;&nbsp; &nbsp; &nbsp; // states[1]["duration"] = breakSeconds;&nbsp; &nbsp; }&nbsp; }&nbsp; breakIncrement() {&nbsp; &nbsp; // same as decrement except does increment&nbsp; &nbsp; let breakLength = this.state.breakLength + 1;&nbsp; &nbsp; if (breakLength > 0 && breakLength < 61) {&nbsp; &nbsp; &nbsp; this.setState({ breakLength: breakLength });&nbsp; &nbsp; &nbsp; // let breakSeconds = breakLength * 60;&nbsp; &nbsp; &nbsp; // states[1]["duration"] = breakSeconds;&nbsp; &nbsp; }&nbsp; }&nbsp; sessionDecrement() {&nbsp; &nbsp; // decrements the sessionLength and the sessionSeconds&nbsp; &nbsp; // sessionLength is only a number ie. 25 (does not show seconds)&nbsp; &nbsp; // sessionSeconds is that nunber converted into seconds&nbsp; &nbsp; let sessionLength = this.state.sessionLength - 1;&nbsp; &nbsp; if (sessionLength > 0 && sessionLength < 61) {&nbsp; &nbsp; &nbsp; // states[0]["duration"] = sessionLength * 60;&nbsp; &nbsp; &nbsp; this.setState((prevState) => ({&nbsp; &nbsp; &nbsp; &nbsp; sessionLength: prevState.sessionLength - 1,&nbsp; &nbsp; &nbsp; &nbsp; timeLeftSeconds: (prevState.sessionLength - 1) * 60,&nbsp; &nbsp; &nbsp; &nbsp; timeLeft: secondsToMins((prevState.sessionLength - 1) * 60)&nbsp; &nbsp; &nbsp; }));&nbsp; &nbsp; }&nbsp; }&nbsp; sessionIncrement() {&nbsp; &nbsp; // same as decrement except does increment&nbsp; &nbsp; let sessionLength = this.state.sessionLength + 1;&nbsp; &nbsp; if (sessionLength > 0 && sessionLength < 61) {&nbsp; &nbsp; &nbsp; // states[0]["duration"] = sessionLength * 60;&nbsp; &nbsp; &nbsp; this.setState((prevState) => ({&nbsp; &nbsp; &nbsp; &nbsp; sessionLength: prevState.sessionLength + 1,&nbsp; &nbsp; &nbsp; &nbsp; timeLeftSeconds: (prevState.sessionLength + 1) * 60,&nbsp; &nbsp; &nbsp; &nbsp; timeLeft: secondsToMins((prevState.sessionLength + 1) * 60)&nbsp; &nbsp; &nbsp; }));&nbsp; &nbsp; }&nbsp; }&nbsp; startStop(id) {&nbsp; &nbsp; // starts the countDown, which runs continuously until the start/stop button&nbsp; &nbsp; // is pressed again, which pauses the countdown.&nbsp; &nbsp; // the id parameter is used by countDown to play the audio beep&nbsp; &nbsp; if (!this.state.started) {&nbsp; &nbsp; &nbsp; this.setState({ started: true });&nbsp; &nbsp; &nbsp; this.countDown(id);&nbsp; &nbsp; }&nbsp; &nbsp; // pauses the countDown&nbsp; &nbsp; if (this.state.started) {&nbsp; &nbsp; &nbsp; this.setState({ started: false });&nbsp; &nbsp; &nbsp; let intervalFunc = this.state.intervalFunc;&nbsp; &nbsp; &nbsp; clearInterval(intervalFunc);&nbsp; &nbsp; }&nbsp; }&nbsp; reset() {&nbsp; &nbsp; let intervalFunc = this.state.intervalFunc;&nbsp; &nbsp; clearInterval(intervalFunc);&nbsp; &nbsp; // reset state to default values&nbsp; &nbsp; this.setState({ breakLength: 5 });&nbsp; &nbsp; this.setState({ sessionLength: 25 });&nbsp; &nbsp; this.setState({ init: "session" });&nbsp; &nbsp; this.setState({ timeLeftSeconds: 1500 });&nbsp; &nbsp; this.setState({ timeLeft: "25:00" });&nbsp; &nbsp; // this.setState({ stateIndex: 0 });&nbsp; &nbsp; this.setState({ started: false });&nbsp; &nbsp; this.setState({ intervalFunc: undefined });&nbsp; }&nbsp; decreaseCurrentSecond = () =>&nbsp; &nbsp; //this.state.timeLeftSeconds--;&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; this.setState({&nbsp; &nbsp; &nbsp; &nbsp; timeLeftSeconds: this.state.timeLeftSeconds - 1&nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; return this.state.timeLeftSeconds;&nbsp; &nbsp; };&nbsp; countDown(id) {&nbsp; &nbsp; // set the function to a variable and set state to it, so the function&nbsp; &nbsp; // can be paused with clearInterval()&nbsp; &nbsp; var intervalFunc = setInterval(&nbsp; &nbsp; &nbsp; () => down(this.decreaseCurrentSecond()),&nbsp; &nbsp; &nbsp; 1000&nbsp; &nbsp; );&nbsp; &nbsp; this.setState({ intervalFunc: intervalFunc });&nbsp; &nbsp; const down = (time) => {&nbsp; &nbsp; &nbsp; if (time > 0) {&nbsp; &nbsp; &nbsp; &nbsp; // converts seconds to MM:SS at every t-minus&nbsp; &nbsp; &nbsp; &nbsp; this.setState({ timeLeft: secondsToMins(time) });&nbsp; &nbsp; &nbsp; &nbsp; /*&nbsp; &nbsp; &nbsp; &nbsp; console.log(time);&nbsp; &nbsp; &nbsp; &nbsp; console.log(this.state.timeLeft);*/&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; let sound = document.getElementById(id).childNodes[0];&nbsp; &nbsp; &nbsp; if (time <= 0) {&nbsp; &nbsp; &nbsp; &nbsp; sound.play();&nbsp; &nbsp; &nbsp; &nbsp; this.setState({ timeLeft: secondsToMins(time) });&nbsp; &nbsp; &nbsp; &nbsp; console.log(`##########`);&nbsp; &nbsp; &nbsp; &nbsp; console.log(time);&nbsp; &nbsp; &nbsp; &nbsp; // console.log(this.stateIndex);&nbsp; &nbsp; &nbsp; &nbsp; console.log(this.state.init);&nbsp; &nbsp; &nbsp; &nbsp; console.log(this.state.timeLeftSeconds);&nbsp; &nbsp; &nbsp; &nbsp; console.log(`##########`);&nbsp; &nbsp; &nbsp; &nbsp; // let stateIndex = (this.state.stateIndex + 1) % states.length;&nbsp; &nbsp; &nbsp; &nbsp; // this.setState({ stateIndex: stateIndex });&nbsp; &nbsp; &nbsp; &nbsp; this.setState({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; init: this.state.init === "session" ? "break" : "session"&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; this.setState({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; timeLeftSeconds:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.state.init === "session"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ? this.state.sessionLength * 60 + 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : this.state.breakLength * 60 + 1&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; console.log(`##########`);&nbsp; &nbsp; &nbsp; &nbsp; console.log(time);&nbsp; &nbsp; &nbsp; &nbsp; // console.log(this.stateIndex);&nbsp; &nbsp; &nbsp; &nbsp; console.log(this.state.init);&nbsp; &nbsp; &nbsp; &nbsp; console.log(this.state.timeLeftSeconds);&nbsp; &nbsp; &nbsp; &nbsp; console.log(`##########`);&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; };&nbsp; &nbsp; // down(this.decreaseCurrentSecond());&nbsp; }&nbsp; render() {&nbsp; &nbsp; return (&nbsp; &nbsp; &nbsp; <div id="clock">&nbsp; &nbsp; &nbsp; &nbsp; <h1 id="title">25-5 Clock</h1>&nbsp; &nbsp; &nbsp; &nbsp; <div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p id="break-label">Break Length</p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p id="break-length">{this.state.breakLength}</p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <button id="break-decrement" onClick={(e) => this.breakDecrement()}>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {" "}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Decrease{" "}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </button>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <button id="break-increment" onClick={(e) => this.breakIncrement()}>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {" "}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Increase{" "}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </button>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; <div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p id="session-label">Session Length</p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p id="session-length">{this.state.sessionLength}</p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <button&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; id="session-decrement"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; onClick={(e) => this.sessionDecrement()}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; >&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {" "}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Decrease{" "}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </button>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <button&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; id="session-increment"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; onClick={(e) => this.sessionIncrement()}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; >&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {" "}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Increase{" "}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </button>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; <hr />&nbsp; &nbsp; &nbsp; &nbsp; <div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p id="timer-label">{this.state.init}</p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p id="time-left">{this.state.timeLeft}</p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <button id="start_stop" onClick={(e) => this.startStop(e.target.id)}>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <audio id="beep" src="./beep.mp3"></audio> start/stop{" "}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </button>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <button id="reset" onClick={(e) => this.reset()}>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {" "}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; reset{" "}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </button>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; );&nbsp; }}/*&nbsp;* Render the above component into the div#app&nbsp;*/ReactDOM.render(<Clock />, document.getElementById("app"));https://codesandbox.io/s/heuristic-star-i3t8m?file=/src/index.js
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript