Advertisement
vasil_k_k

React without hooks

Apr 26th, 2024 (edited)
747
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ----------  Production code version 1.03  ----------
  2.  
  3. import React, { useState } from 'react';
  4. import { renderTopSection } from './TopSection';
  5.  
  6. export default function MainSection() {
  7.  
  8.     const [_, update] = useState(0);
  9.  
  10.     const render = () => update( _ => ++_);
  11.  
  12.     let testDataArray;
  13.  
  14.     try {
  15.         testDataArray = JSON.parse(localStorage.getItem('testData'));
  16.     } catch (error) {
  17.         console.log('code: 1003');
  18.     } finally {
  19.         testDataArray = testDataArray ?? ['one', 'two', 'three'];
  20.         localStorage.setItem('testData', JSON.stringify(testDataArray));
  21.     }
  22.  
  23.     function createLiElement() {
  24.         const inputElement = document.querySelector(".create-li");
  25.         const newLiItem = inputElement.value;
  26.         testDataArray.push(newLiItem);
  27.         localStorage.setItem('testData', JSON.stringify(testDataArray));
  28.         inputElement.value = "";
  29.         renderTopSection();
  30.         render();
  31.     }
  32.  
  33.     const ereseData = () => {
  34.         localStorage.setItem('testData', "");
  35.         render();
  36.     }
  37.  
  38.         return (
  39.             <>
  40.                 <ul>
  41.                     { testDataArray.map((item, index) => <li key={index}>{item}</li>) }
  42.                 </ul>
  43.                 <input type="text" className="create-li" onKeyDown={ (e) => {e.key === "Enter" && createLiElement()} }/>
  44.                 <button style={{ marginTop: "10px" }} type='button' onClick={ createLiElement }>Create</button>
  45.                 <button style={{ marginTop: "10px" }}  type='button' onClick={ ereseData }>Erase this section</button>
  46.                 <button style={{ marginTop: "10px" }}  type='button' onClick={ () => { ereseData(); renderTopSection(); }}>Erase both section</button>
  47.             </>
  48.         );
  49. }
  50.  
  51.  
  52.  
  53. // ---------------  Development code  ---------------
  54.  
  55. import React, { useState } from 'react';
  56.  
  57. export default function TestComponent() {
  58.  
  59. const [state, update] = useState(0)
  60.  
  61. // Universal update function
  62. function triggerUpdate() {
  63.     update((state) => state +1);
  64. }
  65.    
  66. let testDataArray;
  67.  
  68. try {
  69.     testDataArray = JSON.parse(localStorage.getItem('testData'));
  70. } catch (error) {
  71.     console.log('No data in the Local Storage');
  72. }
  73.  
  74. if(!testDataArray) {
  75.     // Simulate fetching data from the server
  76.     testDataArray = ['one', 'two', 'tree'];
  77.  
  78.     //Pushing data to the local storage
  79.     localStorage.setItem('testData', JSON.stringify(testDataArray));
  80. }
  81.  
  82. function createLiElement() {
  83.     const inputElement = document.querySelector(".create-li");
  84.     const newLiItem = inputElement.value;
  85.     testDataArray.push(newLiItem);
  86.  
  87.     //POST request to the server with the New Data
  88.  
  89.     //Pushing data to the Local Storage as well
  90.     localStorage.setItem('testData', JSON.stringify(testDataArray));
  91.  
  92.     //Clear the input field
  93.     inputElement.value = "";
  94.  
  95.     triggerUpdate();
  96. }
  97.  
  98. function ereseData() {
  99.     localStorage.setItem('testData', "");
  100.     triggerUpdate();
  101. }
  102.  
  103.     return (
  104.         <>
  105.             <ul>
  106.                 {testDataArray.map((item, index) => <li key={index}>{item}</li>)}
  107.             </ul>
  108.             <input type="text" className="create-li" onKeyDown={(e) => {e.key === "Enter" && createLiElement()}}/>
  109.             <button type='button' onClick={ createLiElement }>Create</button>
  110.             <button type='button' onClick={ ereseData }>Erase Data</button>
  111.         </>
  112.     );
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement