Skip to content
The Chat Archive
gpt-3.5-turbo-0301· OpenAI WildChat-4.8M (AI2)

fill the code with these react Counter Increment Button Reset Button Log History Here's…

  1. User

    fill the code with these react Counter

    Increment Button Reset Button Log History Here's the starter code.

    function App(){
        return (
            <div className ="container">
                <Header/>
                <Copyright start={1979}/>
            </div>
        );
    }
    
    
    
    function Copyright(props){
        return (
            <h4>Copyright Your Name Here {props.startyear} - {(new Date).getFullYear()}</h4>
        )
    }
    
    
    function Header(props){
        return (
            <h2>Counter example!</h2>
        )
    }
    </script>
    
  2. Assistant
        const incrementCount = () => {
          setCount(prevCount => prevCount + 1);
        };
    
        const resetCount = () => {
          setCount(0);
          setHistory([]);
        };
    
        const logHistory = () => {
          setHistory(prevHistory => [
            ...prevHistory,
            {count, timestamp: Date.now()}
          ]);
        };
    
        return (
          <div className="container">
            <Header />
            <div className="d-flex justify-content-center my-3">
              <h3 className="me-3">{count}</h3>
              <button className="btn btn-primary me-3" onClick={incrementCount}>Increment</button>
              <button className="btn btn-secondary me-3" onClick={resetCount}>Reset</button>
              <button className="btn btn-outline-secondary" onClick={logHistory}>Log History</button>
            </div>
            <div>
              <h5>History:</h5>
              <ul>
                {history.map((entry, index) => (
                  <li key={index}>Count: {entry.count}, Time: {new Date(entry.timestamp).toLocaleString()}</li>
                ))}
              </ul>
            </div>
          </div>
        )
      }
      
      function Header(props){
          return (
              <h2>Counter example!</h2>
          )
      }
      
      ReactDOM.render(
        <Counter />,
        document.getElementById('root')
      );
    </script>