React Basic Hooks - useEffects sample

by pizza_hamburger_chicken

HTML

<div id="root"></div>
<div id="other-div"></div>
<div id="ref-div"></div>

CSS

body {
  height: 100vh;
  font-family: 'Open Sans', sans-serif;
  font-weight: bold;
  font-size: 24px;
  display: flex;
  justify-content: center;
  align-content: center;
  flex-wrap: wrap;
  text-align: center
}

#root, #other-div, #ref-div {
  width: 100%;
}

React

const {useState, useEffect, useRef} = React;

function Example() {
  const [count, setCount] = useState(0);

  // Similar to componentDidMount and componentDidUpdate:  
/*   useEffect(() => {   
    // Update the document title using the browser API    
    document.getElementById('other-div').innerHTML = `You clicked ${count} times`;  
  }); */
  return (
    <div>
      <p>You clicked {count} times</p>
      <button onClick={() => setCount(count + 1)}>
        Click me
      </button>
    </div>
  );
}

function RefExample() {
const letter = useRef('test');

  const onClick = () => {
    letter.current = 'Humanscape!';
    console.log(letter.current);
  };
    // Similar to componentDidMount and componentDidUpdate:  
  useEffect(() => {   
  	// Update the document title using the browser API    
  	document.getElementById('ref-div').innerHTML = letter;
  });
  return (
		<div>
      {letter}
    </div>
  );

}

function App() {
return (
  <div>
    <Example />
    <RefExample />
  </div>
);

}
ReactDOM.render( <App />, document.getElementById('root') );