Repository navigation
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 |
Replies: 2 comments 2 replies
|
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. |
|
Preact team, Is this relationship between hooks and class components, and your general attitude about it, still the same in Preact version 11? Just to be clear, a big factor I find appealing about Preact is pragmatism. So much in the software field is obsessed with utopianism that never quite comes true. I prefer a less glamorous approach of imperfect yet useful tools. So I like the attitude of flexibility here. I just wanted to confirm your thinking is still generally the same. |
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.