I'm trying to use React hooks to fetch some data and display it, but am getting an error:
function App() {
const [user, setUser] = React.useState(null);
React.useEffect(fetch('https://randomuser.me/api/')
.then(results => results.json())
.then(data => {
setUser(data.results[0]);
}), []);
return <div>
{user ? user.name.first : 'Loading...'}
</div>;
}
ReactDOM.render(<App/>, document.getElementById('app'));
<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="app"></div>
Uncaught TypeError: create is not a function
at commitHookEffectList (react-dom.development.js:15901)
at commitPassiveHookEffects (react-dom.development.js:15911)
at HTMLUnknownElement.callCallback (react-dom.development.js:149)
at Object.invokeGuardedCallbackDev (react-dom.development.js:199)
at invokeGuardedCallback (react-dom.development.js:256)
at commitPassiveEffects (react-dom.development.js:17299)
at wrapped (scheduler-tracing.development.js:204)
at flushPassiveEffects (react-dom.development.js:17338)
at dispatchAction (react-dom.development.js:12035)
at eval (index.jsx? [sm]:9)
The useEffect Hook allows you to perform side effects in your components. Some examples of side effects are: fetching data, directly updating the DOM, and timers. useEffect accepts two arguments. The second argument is optional.
To avoid confusion, it's not supported to call Hooks in other cases: 🔴 Do not call Hooks in class components. 🔴 Do not call in event handlers. 🔴 Do not call Hooks inside functions passed to useMemo , useReducer , or useEffect .
useEffect enables you to run something whenever the the component is rendered or when a state changes. Having that inside of a function that is called on click makes it useless. useEffect should call other functions, but should never be called from within a function.
If we just want to run the useEffect function after the initial render, as a second argument, we can give it an empty array. If we pass a second argument (array), React will run the callback after the first render and every time one of the elements in the array is changed.
It's because no callback function is being passed into useEffect
. In the example above, it is actually executing the fetch
request which doesn't return anything. Wrap the fetch
call in an arrow/anonymous function and it will work.
function App() {
const [user, setUser] = React.useState(null);
React.useEffect(() => { // Pass in a callback function!
fetch('https://randomuser.me/api/')
.then(results => results.json())
.then(data => {
setUser(data.results[0]);
});
}, []);
return <div>
{user ? user.name.first : 'Loading...'}
</div>;
}
ReactDOM.render(<App/>, document.getElementById('app'));
<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="app"></div>
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