Advertisement
Guest User

Untitled

a guest
Aug 17th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. [@react.component]
  2. let make = () => {
  3. let (a, setA) = React.useState(() => "Hello ");
  4. let (b, setB) = React.useState(() => "World !");
  5.  
  6. // This hook is called only once when the component is added to the DOM,
  7. // and functions like componentDidMount
  8. React.useEffect0(() => {
  9. // Prints Hello and World !
  10. Js.log2(a, b);
  11. // This is an option(function). It is called when the component is unmounted.
  12. Some(_ => Js.log2(a, b));
  13. });
  14.  
  15. // This hook is called everytime either a or b is set.
  16. // This includes their initialization !
  17. React.useEffect2(
  18. () => {
  19. Js.log2(a, b);
  20. None;
  21. },
  22. (a, b),
  23. );
  24.  
  25. <>
  26. <input
  27. value=a
  28. onChange={e => {
  29. let value = ReactEvent.Form.target(e)##value;
  30. // When the input value changes, we set a state value
  31. // The corresponding useEffect hook is triggered and the component is re-rendered
  32. setA(_ => value);
  33. }}
  34. />
  35. <input
  36. value=b
  37. onChange={e => {
  38. let value = ReactEvent.Form.target(e)##value;
  39. setB(_ => value);
  40. }}
  41. />
  42. </>;
  43. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement