自定义反应钩子不是使用useState的函数吗?

时间:2020-05-19 03:00:43

标签: javascript reactjs

我有这个自定义钩子

const useShowBg = () => {
  const [showBg, useShowBg] = useState(false);

  return [showBg, useShowBg];
};

export default useShowBg;

我将其导入组件并像这样使用


import myHook from './myHook';

const App = () => {
    const [showBg, useShowBg] = myHook

    return (
      <div>
      <button onClick={() => useShowBg(true)}>show</button>
        {showBg && <p>
          Start editing to see some magic happen :)
        </p>}
      </div>
    );

}

单击以触发函数时出现useShowBg is not a function错误?有什么问题吗?

演示https://stackblitz.com/edit/react-qomaiu?file=index.js

1 个答案:

答案 0 :(得分:0)

您忘记了调用钩子

const [showBg, useShowBg] = myHook()