News for hackers. Stuff that matters.
Re:React is becoming a black box by thursday0987 on Thursday September 03, 2020 @06:11PM (#24369466)
this is a pointless example. it doesn't render anything, and therefore doesn't have to deal with React's render cycle.

also, you shouldn't use useState unless you want it to rerender, which you don't because you're printing to console. use a ref instead for myNumber.

the problem you are trying to solve, updating a number if someone clicks on something, is already solved in React:

    const HelloComponent => React.forwardRef((props, forwardRef) => {
     const myNumber = useRef(0)
     const printToConsole = () => {
        myNumber.current = myNumber.current + 1
        console.log("My Number:", myNumber.current)
      }
      render (
        <div ref={forwardRef} onClick={printToConsole}>click me</div>
      )
    }
then you can simply trigger a click by calling .click() on the forwarded ref wherever you happen to mount it.

There are no comments on this comment yet.