Is using hooks in preact class components expected? #4641
-
class Test extends Component{
render(){
const [test,setTest]=useState(0)
return <div>
<div>{test}</div>
<div style={{background:"green"}} onClick={()=>{setTest(test+1)}}>clickMe</div>
</div>
}
} look this example, i use hook in render function, unexpected, it works well |
Beta Was this translation helpful? Give feedback.
Answered by
rschristian
Jan 24, 2025
Replies: 1 comment 1 reply
-
Yes, technically class components and function components aren't entirely separate things in Preact; instead, hooks build on top of class components. It'll look weird to a lot of people, and so isn't something we necessarily advertise too often, but it's completely stable and will work for the foreseeable future for sure. Go wild with it if it can be useful for your app. |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
JoviDeCroock
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yes, technically class components and function components aren't entirely separate things in Preact; instead, hooks build on top of class components.
It'll look weird to a lot of people, and so isn't something we necessarily advertise too often, but it's completely stable and will work for the foreseeable future for sure. Go wild with it if it can be useful for your app.