Javascript 未捕获的TypeError:对象(…)不是函数
我对Preact非常陌生,当我想在Preact中使用钩子时,我得到一个错误:Javascript 未捕获的TypeError:对象(…)不是函数,javascript,preact,Javascript,Preact,我对Preact非常陌生,当我想在Preact中使用钩子时,我得到一个错误: Uncaught TypeError: Object(...) is not a function 我也不知道该怎么办,网上有一些关于预演的文章 这是我的密码 import './style'; import { useState } from 'preact'; function App() { const [value, setValue] = useState(0); const increment
Uncaught TypeError: Object(...) is not a function
我也不知道该怎么办,网上有一些关于预演的文章
这是我的密码
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
import./style';
从'preact'导入{useState};
函数App(){
const[value,setValue]=useState(0);
const increment=useCallback(()=>setValue(value+1),[value]);
返回(
计数器:{value}
增量
);
}
导出默认应用程序
导入错误。应该是:
import { useState } from 'preact/hooks';
请参见此处的文档:import./style';
从'preact'导入{useState};
const App=props=>{
const[value,setValue]=useState(0);
const increment=useCallback(()=>setValue(value+1),[value]);
返回(
计数器:{value}
增量
);
};
导出默认应用程序;
我想你忘了导入useCallback钩子。它应该是从'preact/hooks'导入{useState,useCallback};这些答案解决了你的问题吗?没有,是它自己解决的
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;