Uncaught TypeError: Object (...) is not a function

我对 Preact 很陌生,当我想在 Preact 中使用钩子时,我收到一个错误:


Uncaught TypeError: Object(...) is not a function

而且我不知道该怎么做,网上有一些关于 Preact 的文章


这是我的代码


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


30秒到达战场
浏览 1648回答 2
2回答

郎朗坤

你导入错了。应该:import&nbsp;{&nbsp;useState&nbsp;}&nbsp;from&nbsp;'preact/hooks';请参阅此处的文档:https ://preactjs.com/guide/v10/hooks/#usestate

慕尼黑5688855

import './style';import { useState } from 'preact';const App = props => {&nbsp; const [value, setValue] = useState(0);&nbsp; const increment = useCallback(() => setValue(value + 1), [value]);&nbsp; return (&nbsp; &nbsp; <div>&nbsp; &nbsp; &nbsp; Counter: {value}&nbsp; &nbsp; &nbsp; <button onClick={increment}>Increment</button>&nbsp; &nbsp; </div>&nbsp; );};export default App;
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript