我在reactjs中有一个秒表,如何将每个数字添加到某种数组中以显示每个数字?

奥斯丁的故事

我使用react创建了一个秒表。我的秒表从0开始,并在按下带有componenDidMount和componentWillMount的空格按钮时停止。我的问题是,我似乎无法弄清楚如何用秒表返回的数字创建某种列表。我创建了:

  times = () => {
    this.setState(previousState => ({
      myArray: [...previousState.myArray, this.state.milliSecondsElapsed]
    }));

  };

然后在render()中进行打印。

<h1>{this.times}</h1>

我想要做的是创建某种数组,该数组将在我的handleStart和handleStop方法中跟踪milliSecondsElapsed。

这就是我所拥有的。

import React, {Component} from "react";
import Layout from '../components/MyLayout.js';

export default class Timer extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      milliSecondsElapsed: 0,
      timerInProgress: false // state to detect whether timer has started
    };
    this.updateState = this.updateState.bind(this);
    this.textInput = React.createRef();
  }

  componentDidMount() {
    window.addEventListener("keypress", this.keyPress);
  }

  componentWillUnmount() {
    window.removeEventListener("keypress", this.keyPress);
  }

  textInput = () => {
    clearInterval(this.timer);
  };
  updateState(e) {
    this.setState({})
    this.setState({ milliSecondsElapsed: e.target.milliSecondsElapsed });
  }

  keyPress = (e) => {
    if (e.keyCode === 32) {
      // some logic to assess stop/start of timer
      if (this.state.milliSecondsElapsed === 0) {
        this.startBtn.click();
      } else if (this.state.timerInProgress === false) {
        this.startBtn.click();
      } else {
        this.stopBtn.click();
      }
    }
  };

  handleStart = () => {
    if (this.state.timerInProgress === true) return;

    this.setState({
      milliSecondsElapsed: 0
    });
    this.timer = setInterval(() => {
      this.setState(
        {
          milliSecondsElapsed: this.state.milliSecondsElapsed + 1,
          timerInProgress: true
        },
        () => {
          this.stopBtn.focus();
        }
      );
    }, 10);
  };
  handleStop = () => {
    this.setState(
      {
        timerInProgress: false
      },
      () => {
        clearInterval(this.timer);
        this.startBtn.focus();
      }
    );
  };

  times = () => {
    this.setState(previousState => ({
      myArray: [...previousState.myArray, this.state.milliSecondsElapsed]
    }));

  };

  render() {
    return (
        <Layout>
          <div className="index" align='center'>
            <input 
              value={this.state.milliSecondsElapsed/100}
              onChange={this.updateState}
              ref={this.textInput}
              readOnly={true}
            />
            <button onClick={this.handleStart} ref={(ref) => (this.startBtn = ref)}>
              START
            </button>
            <button onClick={this.handleStop} ref={(ref) => (this.stopBtn = ref)}>
              STOP
            </button>
            <h1>{this.state.milliSecondsElapsed/100}</h1>
          </div>

        
      </Layout>
    


    );
  }
}
德鲁·里斯(Drew Reese)

问题

this.times 是仅更新状态的函数,它不返回任何可渲染的JSX。

times = () => {
  this.setState((previousState) => ({
    myArray: [...previousState.myArray, this.state.milliSecondsElapsed]
  }));
};

  1. 创建myArray状态。

    this.state = {
      myArray: [], // <-- add initial empty array
      milliSecondsElapsed: 0,
      timerInProgress: false // state to detect whether timer has started
    };
    
  2. 将状态更新逻辑从this.times移到this.handleStop

    handleStop = () => {
      this.setState(
        (previousState) => ({
          timerInProgress: false,
          myArray: [
            ...previousState.myArray, // <-- shallow copy existing data
            this.state.milliSecondsElapsed / 100 // <-- add new time
          ]
        }),
        () => {
          clearInterval(this.timer);
          this.startBtn.focus();
        }
      );
    };
    
  3. 将经过时间数组呈现为逗号分隔的列表。

    <div>{this.state.myArray.join(", ")}</div>
    

完整代码

class Timer extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      myArray: [],
      milliSecondsElapsed: 0,
      timerInProgress: false // state to detect whether timer has started
    };
    this.updateState = this.updateState.bind(this);
    this.textInput = React.createRef();
  }

  componentDidMount() {
    window.addEventListener("keypress", this.keyPress);
  }

  componentWillUnmount() {
    window.removeEventListener("keypress", this.keyPress);
  }

  textInput = () => {
    clearInterval(this.timer);
  };

  updateState(e) {
    this.setState({ milliSecondsElapsed: e.target.milliSecondsElapsed });
  }

  keyPress = (e) => {
    if (e.keyCode === 32) {
      // some logic to assess stop/start of timer
      if (this.state.milliSecondsElapsed === 0) {
        this.startBtn.click();
      } else if (this.state.timerInProgress === false) {
        this.startBtn.click();
      } else {
        this.stopBtn.click();
      }
    }
  };

  handleStart = () => {
    if (this.state.timerInProgress === true) return;

    this.setState({
      milliSecondsElapsed: 0
    });
    this.timer = setInterval(() => {
      this.setState(
        {
          milliSecondsElapsed: this.state.milliSecondsElapsed + 1,
          timerInProgress: true
        },
        () => {
          this.stopBtn.focus();
        }
      );
    }, 10);
  };

  handleStop = () => {
    this.setState(
      (previousState) => ({
        timerInProgress: false,
        myArray: [
          ...previousState.myArray,
          this.state.milliSecondsElapsed / 100
        ]
      }),
      () => {
        clearInterval(this.timer);
        this.startBtn.focus();
      }
    );
  };

  render() {
    return (
      <div>
        <div className="index" align="center">
          <input
            value={this.state.milliSecondsElapsed / 100}
            onChange={this.updateState}
            ref={this.textInput}
            readOnly={true}
          />
          <button
            onClick={this.handleStart}
            ref={(ref) => (this.startBtn = ref)}
          >
            START
          </button>
          <button onClick={this.handleStop} ref={(ref) => (this.stopBtn = ref)}>
            STOP
          </button>
          <h1>{this.state.milliSecondsElapsed / 100}</h1>
        </div>
        <div>{this.state.myArray.join(", ")}</div>
      </div>
    );
  }
}

编辑我在reactjs中有一个秒表,我如何将每个数字添加到某种数组中

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何将每个数字添加到python中的列表中?

来自分类Dev

如何将数组添加到列表并迭代以获取每个数组的第一个值?

来自分类Dev

将javascript数组中的n个数字添加到数组中的前一个数字

来自分类Dev

如何使用ParallelStream将一个数组的每个元素添加到另一个数组的相应元素中?

来自分类Dev

如何将一个数组中的值添加到另一个数组?

来自分类Dev

C#将数组中的上一个数字添加到下一个

来自分类Dev

PHP将数组元素添加到另一个数组中的每个元素

来自分类Dev

如何创建一个数字序列,在 MS Word 中显示每个数字中的两个?

来自分类Dev

我如何将一个数组中的数字求和

来自分类Dev

如何将一个数字乘以我的列表中用科学记数法表示的每个元素?

来自分类Dev

向数组中的每个元素/整数添加一个数字

来自分类Dev

为数组中的每个元素添加一个数字[Double]

来自分类Dev

如何将一个数组的元素添加到另一个数组

来自分类Dev

我想给我的每个数组元素一个数字

来自分类Dev

将每个图像包装在div中并添加一个数字

来自分类Dev

如何将一个数组添加到另一个数组以创建一个数组数组。(JS)

来自分类Dev

如何检查一个数字是否可以被列表中的每个数字整除

来自分类Dev

如何将多维数组的键值添加到另一个数组

来自分类Dev

如何在python中的任意数组中将每个元素除以一个数字?

来自分类Dev

如何将一个数组添加到存在于mongodb对象中的另一个数组中

来自分类Dev

Ruby-忽略“ AM” /“ PM”添加到数组中的每个数字

来自分类Dev

挑选每个数字并将其添加到数组中

来自分类Dev

如何将一个数字一分为二,每个数字都在一个范围内?

来自分类Dev

将A然后B附加到数组中的每个数字

来自分类Dev

如何将新数组添加到另一个数组的特定对象中

来自分类Dev

Python如何将多个不同长度的数组添加到一个数组中

来自分类Dev

将零添加到具有单个数字的每个变量

来自分类Dev

如何将随机数组的值添加到另一个数组中,然后在计算出现的值的同时显示它们?C ++

来自分类Dev

如何将随机数组的值添加到另一个数组中,然后在计算出现的值的同时显示它们?C ++

Related 相关文章

  1. 1

    如何将每个数字添加到python中的列表中?

  2. 2

    如何将数组添加到列表并迭代以获取每个数组的第一个值?

  3. 3

    将javascript数组中的n个数字添加到数组中的前一个数字

  4. 4

    如何使用ParallelStream将一个数组的每个元素添加到另一个数组的相应元素中?

  5. 5

    如何将一个数组中的值添加到另一个数组?

  6. 6

    C#将数组中的上一个数字添加到下一个

  7. 7

    PHP将数组元素添加到另一个数组中的每个元素

  8. 8

    如何创建一个数字序列,在 MS Word 中显示每个数字中的两个?

  9. 9

    我如何将一个数组中的数字求和

  10. 10

    如何将一个数字乘以我的列表中用科学记数法表示的每个元素?

  11. 11

    向数组中的每个元素/整数添加一个数字

  12. 12

    为数组中的每个元素添加一个数字[Double]

  13. 13

    如何将一个数组的元素添加到另一个数组

  14. 14

    我想给我的每个数组元素一个数字

  15. 15

    将每个图像包装在div中并添加一个数字

  16. 16

    如何将一个数组添加到另一个数组以创建一个数组数组。(JS)

  17. 17

    如何检查一个数字是否可以被列表中的每个数字整除

  18. 18

    如何将多维数组的键值添加到另一个数组

  19. 19

    如何在python中的任意数组中将每个元素除以一个数字?

  20. 20

    如何将一个数组添加到存在于mongodb对象中的另一个数组中

  21. 21

    Ruby-忽略“ AM” /“ PM”添加到数组中的每个数字

  22. 22

    挑选每个数字并将其添加到数组中

  23. 23

    如何将一个数字一分为二,每个数字都在一个范围内?

  24. 24

    将A然后B附加到数组中的每个数字

  25. 25

    如何将新数组添加到另一个数组的特定对象中

  26. 26

    Python如何将多个不同长度的数组添加到一个数组中

  27. 27

    将零添加到具有单个数字的每个变量

  28. 28

    如何将随机数组的值添加到另一个数组中,然后在计算出现的值的同时显示它们?C ++

  29. 29

    如何将随机数组的值添加到另一个数组中,然后在计算出现的值的同时显示它们?C ++

热门标签

归档