[英]Uncaught TypeError: Object (...) is not a function
I'm very new to Preact and when I want to use hooks in Preact, I get an error:我对 Preact 很陌生,当我想在 Preact 中使用钩子时,出现错误:
Uncaught TypeError: Object(...) is not a function
And I don't know what to do and there is a little articles in web for Preact我不知道该怎么做,网上有一些关于 Preact 的文章
This is my code这是我的代码
import './style';
import { useState } from 'preact';
function App() {
const [value, setValue] = useState(0);
const increment = useCallback(() => setValue(value + 1), [value]);
return (
<div>
Counter: {value}
<button onClick={increment}>Increment</button>
</div>
);
}
export default App
You're importing it wrong.你导入错了。 Should be:应该:
import { useState } from 'preact/hooks';
See docs here: https://preactjs.com/guide/v10/hooks/#usestate请参阅此处的文档: https : //preactjs.com/guide/v10/hooks/#usestate
import './style';
import { useState } from 'preact';
const App = props => {
const [value, setValue] = useState(0);
const increment = useCallback(() => setValue(value + 1), [value]);
return (
<div>
Counter: {value}
<button onClick={increment}>Increment</button>
</div>
);
};
export default App;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.