Yesterday I developed a react custom hook for making our react application's title as dynamic and published in npm. It is my first attempt in npm contribution. So I faced so many errors. And one error not fixed after my well effort. The problem is as below:
The hook using useRef, and useEffect.
The version of react used by the hook is 18.2.0
dependencies in this format:
"dependencies": {
"react": "^18.2.0"
}
I used the hook like this:
import React from "react";
import useTitle from "./hook/react-title-hook";
// import useTitle from "./title";
// import useTitle from "react-dynamic-title";
import { useNavigate } from "react-router-dom";
function App() {
const navigate = useNavigate();
useTitle("Home");
return (
<div className="App">
<header className="App-header">
app
</header>
<h1
onClick={() => {
navigate("/somting");
}}
>
somithing
</h1>
</div>
);
}
export default App;
There are 4 scenarios:
The hook has not a special package.json nor node_modules and it is using what the whole project uses in common. Then, there is no problem. it works smoothly. (It is like second useTitle of the above code)
The hook installed from npm and the version of react in main project is same as the version of hook 18.2.0 , so it would not install the dependencies specially. Then, there is no problem. It works smoothly. (It is like third useTitle of the above code)
The hook has special package.json and node_modules (because, we want to initialise and mention the dependencies when we publish our package). (It is like first useTitle of the above code) Then, the app not render and throw these two errors:
Invalid hook call. Hooks can only be called inside of the body of a function component. (actually the hook is inside of the component)
Uncaught TypeError: Cannot read properties of null (reading 'useRef'). (the first line of hook is useRef, so the error is like this. When I changed and make the useEffect the error is like cannot read properties of null (reading 'useEffect'))
The hook installed into a project that the version of react used by that project is not matching of the version of the hook. Then, the app throw the error as above.
So, this is the problem. The link of the hook is this: react-dynamic-title
How to fix it, Thank you in advance.
Related
Developing teams application using react - I am trying to use Ag grid react to design sample table. I have added code and while running it it throws errors as follows.
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 See https://reactjs.org/link/invalid-hook-call for tips about how to debug and fix this problem.
Below the code snippet
import React from 'react';
import {AgGridReact} from 'ag-grid-react';
import 'ag-grid-community/dist/styles/ag-grid.css';
import 'ag-grid-community/dist/styles/ag-theme-alpine.css';
export default function Project() {
return (
<div className="ag-theme-alpine" style={{height: 400, width: 600}}>
<AgGridReact>
</AgGridReact>
</div>
)
};
Error is due to the duplicate version of react in the application folder - deleting the node_module folder and ran npm install solved my issue.
I am quite new to Stripe and fairly new to React, so I need help troubleshooting - hopefully I am just forgetting something simple. I am building an app on create-react-app with modular Firebase, cloud functions, react-router-dom v6 and hopefully Stripe Elements. The error gives 3 reasons I could be getting it (React's page on that: https://reactjs.org/warnings/invalid-hook-call-warning.html) - I don't think I'm breaking the rules of hooks, since everything works fine as long as I don't try to add Elements to any component, and I only see one version of React and react-dom in my app when I run npm ls react and npm ls react-dom. Maybe I have more than one copy of React - can Stripe Elements be adding another copy? I've tried adding react and react-dom as peer dependencies, but I can't complete the final step of editing webpack because I don't have access to it with create-react-app. On the Stripe discord page, they told me they didn't know enough about React to troubleshoot, so I'm hoping that someone here will recognize what I'm doing wrong. Here is an example of my PaymentForm component:
import { getApp } from 'firebase/app'
import { Link, useNavigate } from "react-router-dom";
import { getFirestore, doc, setDoc } from 'firebase/firestore'
import { getAuth, EmailAuthProvider, linkWithCredential} from 'firebase/auth'
import {loadStripe} from '#stripe/stripe-js';
import { Elements } from '#stripe/react-stripe-js';
import CardPaymentForm from "../CardPaymentForm"
const stripePromise = loadStripe(my public key here);
export const PaymentFormPage = () => {
return (
<div className="paymentFormPage flex-col">
<Elements stripe={stripePromise}>
{/* CardPaymentForm would go here */}
</Elements>
</div>
)
}
Has anyone had any experience with troubleshooting this? Any obvious things I might have done wrong, or places to start first?
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.
I'm trying to use Atomize (https://atomizecode.com/) with Gatsby JS (https://www.gatsbyjs.org/) and while it is successfully installed, the atomize components are not being rendered to the page.
I am trying to import a button to start "Hey", and while it renders something it is not the standard button component. Also, even though the button component is imported, I get the warning that reads it is not being used.
I am wrapping atomize around gatsby app by the following way:
import React from "react"
import { StyleReset } from "atomize"
export const wrapRootElement = ({ element }) => (
<>
<StyleReset />
{element}
</>
)
Did you install styletron toolkit?
According to their docs it's a dependency and looking at their package.json it's defined under peerDependencies which means it won't get installed along with atomize.
Styletron's docs sugggets using gatsby-plugin-styletron for Gatsby.
Hope that helps.
I'm trying to use lazy loading for my create-react-app with react#16.5.2 and I did this :
import React, { Component } from 'react';
import { BrowserRouter } from 'react-router-dom';
const Header = React.lazy(() => import('./_header'));
class SomeFile extends Component {
render() {
return (
<BrowserRouter>
<React.Fragment>
<Header />
</React.Fragment>
</BrowserRouter>
);
}
}
export default SomeFile;
Do you know why this error occurs ? is it because of my react version ? based on this everything seems fine!
Edit
What does this mean? based on reactjs.org :
Note:
React.lazy and Suspense is not yet available for server-side rendering. If you want to do code-splitting in a server rendered app, we recommend Loadable Components. It has a nice guide for bundle splitting with server-side rendering.
React.lazy is only available from v16.6.0 https://reactjs.org/blog/2018/10/23/react-v-16-6.html
In your comand line interface, try updating React using the following command:
npm i react#latest
Restart Development Server, if you still face issue try the above steps:
Search for REACT_EDITOR on the Readme.md file, and insert =atom in front of it, like as follow:
REACT_EDITOR=atom
Then, save, restart the development server. It should work now