Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React 16.7 Hooks : `react.useState` is not a function

Tags:

I am trying functional components with hooks of react 16.7,there is an error:

enter image description here

src/components/Footer/index.js

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

package.json

enter image description here

What should I do?

like image 729
zwl1619 Avatar asked Nov 13 '18 14:11

zwl1619


People also ask

Is useState function in React?

The React useState Hook allows us to track state in a function component. State generally refers to data or properties that need to be tracking in an application.

Is useState a React hook?

A Hook is a special function that lets you “hook into” React features. For example, useState is a Hook that lets you add React state to function components.

Does useState accept a function?

useState is a Hook (function) that allows you to have state variables in functional components. You pass the initial state to this function and it returns a variable with the current state value (not necessarily the initial state) and another function to update this value.

Can I use Hooks in React 16?

Yes! Starting with 16.8. 0, React includes a stable implementation of React Hooks for: React DOM.


2 Answers

Make sure that you upgrade react-dom to 16.7.0-alpha.0 as well.

package.json

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

It might also be that you only bumped the version in package.json without installing the new version. You can remove node_modules and install again.

npm ci

Example

const { useState } = React;

function Footer() {
  const [selectedTab, setSelectedTab] = useState('redTab');
  const [hidden, setHidden] = useState(false);
  const [fullScreen, 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/[email protected]/umd/react.development.js"></script>
<script src="https://unpkg.com/[email protected]/umd/react-dom.development.js"></script>

<div id="root"></div>
like image 74
Tholle Avatar answered Oct 02 '22 22:10

Tholle


I have installed both react and react-dom alpha, as you can see in this package.json.

Using, in the same project, the following code, would work just fine:

import React, { useRef, useState } from 'react';

function Counter() {
  const [count, setCount] = useState(0);
  const [icount, setICount] = useState(0);
  const {current: increment} = useRef(1 + Math.floor(Math.random() * 5));
  return (
    <div>
      Count {count}<br />
      Increment {increment}<br />
      <button onClick={() => {
        setCount(count + 1);
        setICount(icount + increment);
      }} clicks={count}>
        Current {icount}
      </button>
    </div>
  );
}

export default Counter;

That export can be tested/used via a basic app like:

import React from 'react';
import ReactDOM from 'react-dom';
import Counter from './Counter';

ReactDOM.render(<Counter />, document.body);

I hope this example clarifies/solves your issues.

Best Regards

like image 34
Andrea Giammarchi Avatar answered Oct 02 '22 23:10

Andrea Giammarchi