1. import React, { useState } from 'react';
    2. import ReactDOM from 'react-dom';
    3. // import useUpdate from './useUpdate.js';
    4. const rootElement = document.querySelector('#root');
    5. let _state = [];
    6. let _index = 0;
    7. const myUseState = (initialValue) => {
    8. const currentIndex = _index;
    9. _index += 1;
    10. _state[currentIndex] = _state[currentIndex] || initialValue;
    11. const setState = (newState) => {
    12. _state[currentIndex] = newState;
    13. render();
    14. }
    15. return [_state[currentIndex], setState];
    16. }
    17. const render = () => {
    18. _index = 0;
    19. ReactDOM.render(<App />, rootElement);
    20. }
    21. const App = () => {
    22. const [n, setN] = useState(0);
    23. const [m, setM] = useState(0);
    24. return (
    25. <div>
    26. n: {n} <button onClick={() => setN(n + 1)}>+1</button>
    27. <hr />
    28. m: {m} <button onClick={() => setM(m + 1)}>+1</button>
    29. </div>
    30. )
    31. }
    32. ReactDOM.render(<App />, rootElement);

    React Hooks