Skip to main content

再看一眼 useState Hook

· One min read
VisualDust
Ordinary Magician | Half stack developer

之前说React Hooks可以没有class直接work。这是useState的写法和对应的class写法:

import React, { useState } from 'react';

function Example() {
// Declare a new state variable, which we'll call "count"
const [count, setCount] = useState(0);

return (
<div>
<p>You clicked {count} times</p>
<button onClick={() => setCount(count + 1)}>
Click me
</button>
</div>
);
}