Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
214 views
in Technique[技术] by (71.8m points)

reactjs - how can i stop this counter and put the final value in databases using react js axios

i need a help please i am new in reactjs how can i stop this counter and put the final value in databases using react js axios

the code https://codesandbox.io/s/react-hook-counter-c2cei?file=/src/index.js:24-43

import React, { useState, useRef, useEffect } from "react";
import ReactDOM from "react-dom";

import "./styles.css";

function Counter() {
  const [counter, setCounter] = useState(0);

  useInterval(() => setCounter(counter + 1), 1000);

  return (
    <div className="App">
      <h2>Counter: {counter}</h2>
      <button type="button" onClick={() => setCounter(0)}>
        Reset
      </button>
    </div>
  );
}

function useInterval(callback, delay) {
  const savedCallback = useRef();

  useEffect(() => {
    savedCallback.current = callback;
  }, [callback]);

  useEffect(() => {
    function runCallback() {
      savedCallback.current();
    }
    if (delay !== null) {
      let id = setInterval(runCallback, delay);
      return () => clearInterval(id);
    }
  }, [delay]);
}

const rootElement = document.getElementById("root");
ReactDOM.render(<Counter />, rootElement);

question from:https://stackoverflow.com/questions/65932369/how-can-i-stop-this-counter-and-put-the-final-value-in-databases-using-react-js

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you want to "stop" the timer and do something with the current counter value than I have some suggested tweaks to allow your timer to be paused/stopped and issue a side-effect with the current state.

  1. Modify the useInterval hook to always run the cleanup function when the dependency updates. You can also simplify the interval callback, i.e. pass the callback ref's current value.

    function useInterval(callback, delay) {
      const savedCallback = useRef();
    
      useEffect(() => {
        savedCallback.current = callback;
      });
    
      useEffect(() => {
        let id;
        if (delay) {
          id = setInterval(savedCallback.current, delay);
        }
        return () => clearInterval(id);
      }, [delay]);
    }
    
  2. Add a delay state to your component that you can toggle between null and 1000.

    const [delay, setDelay] = useState(1000);
    
  3. Use a functional state update in your setCounter state updater to avoid stale state enclosures.

    useInterval(() => setCounter((c) => c + 1), delay);
    
  4. Add a button to toggle the interval running.

    <button
      type="button"
      onClick={() => setDelay((delay) => (delay ? null : 1000))}
    >
      {delay ? "Pause" : "Start"}
    </button>
    
  5. Use an useEffect hook to run a side-effect to do what you need with the counter state when the interval is paused/stopped

    useEffect(() => {
      if (delay === null) { /* do stuff */ }
    }, [counter, delay]);
    

Demo

Edit how-can-i-stop-this-counter-and-put-the-final-value-in-databases-using-react-js

Full code:

function Counter() {
  const [counter, setCounter] = useState(0);
  const [delay, setDelay] = useState(1000);

  useInterval(() => setCounter((c) => c + 1), delay);

  useEffect(() => {
    if (delay === null) { /* do stuff */ }
  }, [counter, delay]);

  return (
    <div className="App">
      <h2>Counter: {counter}</h2>
      <button type="button" onClick={() => setCounter(0)}>
        Reset
      </button>
      <button
        type="button"
        onClick={() => setDelay((delay) => (delay ? null : 1000))}
      >
        {delay ? "Pause" : "Start"}
      </button>
    </div>
  );
}

function useInterval(callback, delay) {
  const savedCallback = useRef();

  useEffect(() => {
    savedCallback.current = callback;
  });

  useEffect(() => {
    let id;
    if (delay) {
      id = setInterval(savedCallback.current, delay);
    }
    return () => clearInterval(id);
  }, [delay]);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...