skip to Main Content

I have thid component



import { useState, useEffect } from "react"

export default function profile() {

  const [text, setT] = useState("")

  const f = () => console.log(hiop)

  const t = "hi boy how r u "

  function type(s = 100, i = 0, d = 0) {

    setInterval(() => (

      d == 0 ? i++ : i--,

      i >= t.length && d == 0 ? d = 1 : i == 0 ? d = 0 : 0,

      setT(t.slice(0, i))), s)

  }

  useEffect(() => {

    type()

    console.log(hiop)

  }, [])

  return (

    <h1 id="hiop" onClick={f}>{text}</h1>

  )

}

Can anybody tell me why its not working outside of useEffect and callback functions when I console the hiop element and why does it work when I put this console in useEffect and callback functions like onclick onsubmit

2

Answers


  1. It should work when you click on the text. I just run this code and its working.

    Login or Signup to reply.
  2. It really works when you invoke the function f in line 6 and it’s called when you click your h1 and it logs

    Try to make variables names more clear for yourself in the future

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search