英文:
Giving id to element and consoling not working outside of useEffect and functions
问题
我有这个组件
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>
)
}
英文:
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
答案1
得分: 2
当您在第6行调用函数f时,它确实起作用,并且当您单击h1时调用它,它会记录。
尝试在将来使变量名称更清晰。
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论