Advertisement
fakesamgregory

Master React in Webflow - useState

Nov 6th, 2024 (edited)
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // index.js
  2. import React from 'react';
  3. import ReactDOM from 'react-dom';
  4.  
  5. import App from './App';
  6.  
  7. const element = document.getElementsByClassName('react')[0];
  8. const root = ReactDOM.createRoot(element);
  9. root.render(<App />);
  10.  
  11. /* ------------ */
  12. // App.js
  13. import React from 'react';
  14. import NumberTracker from './NumberTracker';
  15.  
  16. export default function App() {
  17.     return <NumberTracker />;
  18. }
  19.  
  20. /* ------------ */
  21. // NumberTracker.js
  22. import React, { useState } from 'react';
  23. import Button from './Button';
  24.  
  25. export default function NumberTracker() {
  26.     const [number, setNumber] = useState(0)
  27.    
  28.     const increaseNumber = () => {
  29.         setNumber(prev => prev + 1)
  30.     };
  31.  
  32.     const decreaseNumber = () => {
  33.         setNumber(prev => prev - 1)
  34.     };
  35.  
  36.     return (
  37.         <>
  38.             <p>{number}</p>
  39.             <Button onClick={increaseNumber}>increase</Button>
  40.             <Button onClick={decreaseNumber}>decrease</Button>
  41.         </>
  42.     );
  43. }
  44.  
  45. /* ------------ */
  46. // Button.js
  47. import React from 'react'
  48.  
  49. export default function Button({ children, onClick }) {
  50.     return <button onClick={onClick}>{children}</button>
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement