reactjs – React 16.7 Hooks:`react.useState`不是函数

前端之家收集整理的这篇文章主要介绍了reactjs – React 16.7 Hooks:`react.useState`不是函数前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试带有反应16.7的钩子的功能组件,有一个错误

SRC /组件/页脚/ index.js

function Footer() {
  const [selectedTab,setSelectedTab] = useState('redTab');
  const [hidden,setHidden] = useState(false);
  const [fullScreen,setFullScreen] = useState(false);
 //...
}

的package.json

我该怎么办?

确保将react-dom升级到16.7.0-alpha.0.

的package.json

{
  "dependencies": {
    "react": "16.7.0-alpha.0","react-dom" "16.7.0-alpha.0",...
  },...
}

也许你只是在没有安装新版本的情况下碰撞了package.json中的版本. You can remove node_modules and install again.

npm ci

const { useState } = React;

function Footer() {
  const [selectedTab,setFullScreen] = useState(false);
  
  return (
    <div>
      <button onClick={() => setSelectedTab('blueTab')}>{selectedTab}</button>
      <button onClick={() => setHidden(isHidden => !isHidden)}>
        {hidden ? 'hidden' : 'visible'}
      </button>
      <button onClick={() => setFullScreen(isFullScreen => !isFullScreen)}>
        {fullScreen ? 'fullscreen' : 'windowed'}
      </button>
    </div>
  );
}

ReactDOM.render(
  <Footer />,document.getElementById('root')
);
<script src="https://unpkg.com/react@16.7.0-alpha.0/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@16.7.0-alpha.0/umd/react-dom.development.js"></script>

<div id="root"></div>
原文链接:https://www.f2er.com/react/300668.html

猜你在找的React相关文章