I am trying functional components with hooks of react 16.7,there is an error:
src/components/Footer/index.js
function Footer() {
const [selectedTab, setSelectedTab] = useState('redTab');
const [hidden, setHidden] = useState(false);
const [fullScreen, setFullScreen] = useState(false);
//...
}
package.json
What should I do?
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.
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.
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.
Yes! Starting with 16.8. 0, React includes a stable implementation of React Hooks for: React DOM.
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>
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With