UseState Hooks in react gatsby - reactjs

import React, {useState} from 'react'
import Layout from "../components/layout"
const blog = () => {
const [state, setState] = useState({
name: 'Kerry',
age: 20
});
return (
<Layout>
<div>
<h1>blog</h1>
<p>Post will show up here later on</p>
{state.name + state.age}
</div>
</Layout>
)
}
export default blog
Hi I am trying to print out name from state. I am using react hooks. This project is built in gatsby command.
From my understanding the line {state.name + state.age} should return my name and age.
but the programs terminal is returning rebuilding failed
Also The program is giving me 2 error.
here theses two
5:19 warning 'setState' is assigned a value but never used no-unused-vars
5:31 error React Hook "useState" is called in function "blog" which is neither a React function component or a custom React Hook function react-hooks/rules-of-hooks
So a, i using useState wrong

Components must start with capital letters. You have to change blog to Blog. The rest of the code seems correct.
This is because if they start with a lowercase letter, it is interpreted as a built-in component such as <p> or <img> by React. Since they are passed to React.createElement it breaks your code.
You can check for further information in React's official documentation.

Related

React Hook "useState" is called in function "nav" that is neither a React function component nor a custom React Hook function

import {useState} from 'react'
const nav = () => {
const [activeNav, setActiveNav] = useState('#')
}
I was trying to build a nav bar.
and I am getting this error I don't know why
React Hook "useState" is called in function "nav" that is neither a React function component nor a custom React Hook function. React component names must start with an uppercase letter. React Hook names must start with the word "use" react-hooks/rules-of-hooks
Basically it seems like one or both of the following is the cause of your issue:
nav is not a valid React component. React components are capitalized.
nav might not be rendered as a React component.
Rename to Nav so it's at least named correctly.
import {useState} from 'react'
const Nav = () => {
const [activeNav, setActiveNav] = useState('#');
...
return (
// return valid JSX
);
}
Render Nav as a React component:
Valid
<Nav />
Invalid
{Nav()}

MOBX and react integration

getting in valid hook error in a re write of a clients app... (upgrading code)
mobx 6.3.8 mobx react 7.2.1 and mobx-state-tree 5.0.5
React 17.0.1 RN 0.64.3
i feel like the error is here. i googled the code line for use stores and it led me to the deprecated site... i dont know where to find new handling in the https://mobx.js.org/react-integration.html site... what would this be called?
import { createContext, useContext } from "react"
import { RootStore } from "./root-store"
const RootStoreContext = createContext<RootStore>({} as RootStore)
const RootStoreProvider = RootStoreContext.Provider
// hook error here? // export const useStores = () => useContext(RootStoreContext);
error:
Error: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
You might have mismatching versions of React and the renderer (such as React DOM)
You might be breaking the Rules of Hooks
You might have more than one copy of React in the same app
See https://reactjs.org/link/invalid-hook-call for tips about how to debug and fix this problem.
adding more context... rootstore file
import { Instance, SnapshotOut, types } from "mobx-state-tree"
import { creatMediaPlayerModel } from "../../models/media-player"
import { createUserModel } from "../../models/user"
import { createContentModel } from "../../models/content"
export const RootStoreModel = types.model("RootStore", {
mediaPlayerStore: creatMediaPlayerModel(),
userStore: createUserModel(),
contentStore: createContentModel(),
})
export type RootStore = Instance<typeof RootStoreModel>
export type RootStoreSnapshot = SnapshotOut<typeof RootStoreModel>
From that error message:
Hooks can only be called inside of the body of a function component.
You are not calling a hook from inside the body of a function component. So you are breaking the rules of hooks.
According the rules of hooks you can only call a hook from the top level of a react function component. If you are not inside a functional component, then you cannot use a react hook*.
From the docs:
Don’t call Hooks inside loops, conditions, or nested functions. Instead, always use Hooks at the top level of your React function, before any early returns. By following this rule, you ensure that Hooks are called in the same order each time a component renders. That’s what allows React to correctly preserve the state of Hooks between multiple useState and useEffect calls.
That means you need to call useStores from within a functional component.
Something like:
function MyComponent() {
const myStores = useStores()
// render component with data from stores here
return <>{myStore.myData}</>
}
* The exception, sort of, is a custom hook which can call other hooks. Your useStores here is a custom hook. So it's fine to call useContext from that custom hook.
But that custom hook must obey the same usage rules as the built in hooks, so all hooks are called from the body of a function component, there is just function in between.

Why am I getting error “hook cannot be called inside a callback” when setting state in React?

I am new to React, and I am trying to integrate React hooks in some of the projects that the React course I follow uses. This example is supposed to add an input field which sets the background color of a page. When I load the page, after 1 second, I get the following error
src\App.js
Line 16:43: React Hook "useBackgroundColor" cannot be called inside a callback. React Hooks must be called in a React function component or a custom React Hook function react-hooks/rules-of-hooks
Search for the keywords to learn more about each error.
What might be the issue here? I haven't managed to figure it out.
Thank you!
import logo from "./logo.svg";
import "./App.css";
import UserItem from "./UserItem";
import React, { useState } from "react";
function App() {
const [backgroundColor, useBackgroundColor] = useState("purple");
return (
<div className="App" style={{ background: backgroundColor }}>
<h1>Lista utilziatori:</h1>
<input type="color" onChange={event => useBackgroundColor(event.target.value)} />
</div>
);
}
export default App;
Functions prefixed with use are interpreted by React to be hooks. But here, your useBackgroundColor isn't actually a hook, it's just a state setter function. Rename it to something more appropriate - the usual convention is to prefix the state variable with set. Change
const [backgroundColor, useBackgroundColor] = useState("purple");
to
const [backgroundColor, setBackgroundColor] = useState("purple");
and then reference setBackgroundColor in your code.

NextJS (React) - Error: Invalid hook call (useState)

I am using NextJS for server side rendering & I am trying to use useState hook inside a component which is throwing :
Error:
Error: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
1. You might have mismatching versions of React and the renderer (such as React DOM)
2. You might be breaking the Rules of Hooks
3. You might have more than one copy of React in the same app
My code:
import { useState } from 'react';
const Banner = ({ movies }) => {
const [movie, setMovie] = useState(movies);
return (
<header>
<h1> Banner </h1>
</header>
)
}
export default Banner;
Directory Tree
/components/Banner.js
/pages
Getting same error when i use useEffect() like :
import React, {useEffect} from 'react'

ReactJS useCallback not a function of react?

I have a functional Component which is using the hook useCallback. For the last few days everything was just fine. All worked as it should. Today I start up the app and I have this error:
React Hook "useCallback" is called in function "loginPage" which is neither a React function component or a custom React Hook function
This makes no sense because it has been fine. For debugging I simply erased all code on the page except that one coed and even just put a useCallback template in its place, but still the same. It is as if it has been removed from react entirely.
I checked my react version and find 16.8.6 also in react-dom.
Does anyone have any ideas?
import React, {Fragment,useCallback } from 'react';
import { Redirect} from 'react-router-dom';
import {useDropzone} from 'react-dropzone';
const loginPage = (props) => {
const callbackFunction = useCallback(() => {
console.log("callback called");
// Do something with callbackCount ...
return true;
}, []);
}
I see three problems why the error occurred.
which is neither a React function component or a custom React Hook function
loginPage is not a component. useCallback is a hook and it needs to be called inside a function component (not a class component) or in another hook (a function whose name starts with "use"). Check out the rules of hooks.
And also the component name should start with a capital letter, so it should be LoginPage for it to be a valid React component.
You are not returning anything from your component.

Resources