I'm having issues with react-bootstrap, specifically whenever I try and use a react-bootstrap tag, like <Row></Row>, I'm getting the "Invalid hook call" error on my app.
I've run npm ls and these are the packages/versions I currently have installed:
boostrap#4.6.0
react-bootstrap#1.6.1
react-dom#17.0.2
react#17.0.2
I've also followed the advice in the React documentation on this error and added logs to see if I've got duplicate copies of React, but the test is returning 'true' in the console.
Here's the package.json file in my project:
{
"name": "kanvaswebappr",
"version": "0.1.0",
"private": true,
"dependencies": {
"bootstrap": "^4.1.3",
"jquery": "^3.5.1",
"merge": "^1.2.1",
"oidc-client": "^1.9.0",
"react-router-bootstrap": "^0.25.0",
"react-router-dom": "^5.1.2",
"react-scripts": "^3.4.4",
"reactstrap": "^8.4.1",
"rimraf": "^2.6.2"
},
"devDependencies": {
"ajv": "^6.9.1",
"cross-env": "^5.2.0",
"typescript": "^3.7.5",
"eslint": "^6.8.0",
"eslint-config-react-app": "^5.2.0",
"eslint-plugin-flowtype": "^4.6.0",
"eslint-plugin-import": "^2.20.1",
"eslint-plugin-jsx-a11y": "^6.2.3",
"eslint-plugin-react": "^7.18.3",
"nan": "^2.14.1"
},
"peerDependencies": {
"react": "^16.0.0",
"react-dom": "^16.0.0"
},
"eslintConfig": {
"extends": "react-app"
},
"scripts": {
"start": "rimraf ./build && react-scripts start",
"build": "react-scripts build",
"test": "cross-env CI=true react-scripts test --env=jsdom",
"eject": "react-scripts eject",
"lint": "eslint ./src/"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
As you can see, I've moved react and reactdom into peeDependencies and then run npm install, but that hasn't helped either.
My app is currently super basic because I'm just starting out/learning.
My index.js file:
import React from 'react';
import ReactDOM from 'react-dom';
import { BrowserRouter } from 'react-router-dom';
import App from './containers/App';
import registerServiceWorker from './registerServiceWorker';
import 'bootstrap/dist/css/bootstrap.css';
const baseUrl = document.getElementsByTagName('base')[0].getAttribute('href');
const rootElement = document.getElementById('root');
ReactDOM.render(
<BrowserRouter basename={baseUrl}>
<App />
</BrowserRouter>,
rootElement);
My App.js file:
import React, { Component } from 'react';
import './App.css';
import Layout from '../components/Layout/Layout';
class App extends Component {
render() {
return (
<Layout>
<strong>This content is going to be rendered as the props.children inside the Layout component.</strong>
</Layout>
);
}
}
export default App;
My Layout.js file:
import React from 'react';
import Alert from 'react-bootstrap/Alert';
const layout = (props) => {
return (
<div>
<div>
This is the place for the navigation component.
</div>
<Alert>
This is an alert—check it out!
</Alert>
<main>
{props.children}
</main>
</div>
)
}
export default layout;
If anyone is able to help I would be incredibly grateful!
I fixed it myself after a bit more messing around this morning. Posting this here in case it's of any use to anyone else.
I'm building my React app as part of an asp.net solution and I had a node_modules folder at both solution level and in my actual React app/project. I'm very new to all this so I think I'd managed to install packages in both places and they were out of sync in terms of what was installed and version numbers. I ran npm update at both levels to bring everything up to date and then I ran npm link MyApp/node-modules/react from the solution folder just to make sure that React wasn't being duplicated (although I don't think it was).
All seems to be working now.
solved it by going into node modules folder to check the versions react and react-dom installations. then I changed those that were behind. actually i deleted the node modules folder.
Related
I'm building a react app and when i started with my first hook function I got this error
react-dom.development.js:16227
Uncaught 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.
at Object.throwInvalidHookError (react-dom.development.js:16227:1)
at useState (react.development.js:1622:1)
at MenuWrapper (menuWrapper.jsx:16:1)
at MenuList.jsx:12:1
at Array.map (<anonymous>)
at MenuList (MenuList.jsx:8:1)
at Menu.render (menu.jsx:108:1)
at finishClassComponent (react-dom.development.js:19752:1)
at updateClassComponent (react-dom.development.js:19698:1)
at beginWork (react-dom.development.js:21611:1)
react-dom.development.js:18687
The above error occurred in the <Menu> component:
at Menu (http://localhost:3000/static/js/bundle.js:385:5)
at div
at div
at App (http://localhost:3000/static/js/bundle.js:62087:78)
Consider adding an error boundary to your tree to customize error handling behavior.
Visit https://reactjs.org/link/error-boundaries to learn more about error boundaries.
logCapturedError # react-dom.development.js:18687
react-dom.development.js:26923
Uncaught 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.
at Object.throwInvalidHookError (react-dom.development.js:16227:1)
at useState (react.development.js:1622:1)
at MenuWrapper (menuWrapper.jsx:16:1)
at MenuList.jsx:12:1
at Array.map (<anonymous>)
at MenuList (MenuList.jsx:8:1)
at Menu.render (menu.jsx:108:1)
at finishClassComponent (react-dom.development.js:19752:1)
at updateClassComponent (react-dom.development.js:19698:1)
at beginWork (react-dom.development.js:21611:1)
this is my package.json
{
"name": "jonathan",
"version": "0.1.0",
"private": true,
"type": "module",
"dependencies": {
"#babel/runtime": "^7.5.0",
"#testing-library/jest-dom": "^5.16.5",
"#testing-library/react": "^13.4.0",
"#testing-library/user-event": "^14.4.3",
"airbnb-prop-types": "^2.16.0",
"axios": "^1.1.3",
"bootstrap": "^5.2.2",
"create-webpack-config": "^0.2.1",
"html-webpack-plugin": "^5.5.0",
"jquery": "^3.6.1",
"mdb-react-ui-kit": "^4.2.0",
"react": "^18.2.0",
"react-bootstrap": "^2.5.0",
"react-bootstrap-accordion": "^1.0.0",
"react-dom": "^18.2.0",
"react-helmet": "^6.1.0",
"react-onclickout": "^2.0.8",
"react-outside-click-handler": "^1.3.0",
"react-router": "^6.4.2",
"react-router-dom": "^6.4.2",
"react-script-tag": "^1.1.2",
"react-scripts": "^5.0.1",
"type-fest": "^3.1.0",
"webpack-dev-server": "^4.11.1"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"description": "This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).",
"main": "index.js",
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"#babel/core": "^7.19.6",
"#babel/node": "^7.20.0",
"#babel/preset-env": "^7.19.4",
"#babel/preset-react": "^7.18.6",
"web-vitals": "^3.0.4",
"webpack": "^5.74.0",
"webpack-cli": "^4.10.0"
}
}
Finally, the .jsx page that I used the usestate.
import React, { useState }from "react";
import Area from "../ReadFolder/geographicMainComponents/areaMainComponent.jsx";
import City from "../ReadFolder/geographicMainComponents/cityMainComponent.jsx";
import Country from "../ReadFolder/geographicMainComponents/countryMainComponent.jsx";
import Neighborhood from "../ReadFolder/geographicMainComponents/neighborhoodMainComponent.jsx";
import Region from "../ReadFolder/geographicMainComponents/regionMainComponent.jsx";
import { default as AreaW} from "../WriteFolder/geographicMainComponents/areaMainComponent.jsx";
import { default as CityW} from "../WriteFolder/geographicMainComponents/cityMainComponent.jsx";
import { default as CountryW} from "../WriteFolder/geographicMainComponents/countryMainComponent.jsx";
import { default as NeighborhoodW} from "../WriteFolder/geographicMainComponents/neighborhoodMainComponent.jsx";
import { default as RegionW} from "../WriteFolder/geographicMainComponents/regionMainComponent.jsx";
export function MenuWrapper(type,id,isEdit){
let [edit,setEdit] = useState(isEdit);
if(isEdit){
if(type==='country'){
return(
<div>
<div className={edit ? 'd-block' :'d-none'}><CountryW id={id}/><button onclick={() => setEdit=false}>read</button></div>
<div className={edit ? 'd-none' : 'd-block'}><Country id={id} /><button onclick={() => setEdit=true}>edit</button></div>
</div>
);
}
else{return(<div></div>)}
}
else{return(<div></div>)}
}
sorry, this is so long. please note I tried deleting the package_lock,json and node modules. then I tried npm start and even added a --force.that didn't resolve the issue.
For where I called it
import React,{Component} from "react";
import { MenuWrapper } from "./menuWrapper.jsx";
export function MenuList(Items,kind){
if(Items===null){
return(<div></div>)
}
return(
<div>
{
Items.map((item)=>{
return(
MenuWrapper(kind,item.id,false)
)
})
}
</div>
);
}
hooks can be called only within react components. MenuWrapper is not a react component (you are not invoking it as <MenuWrapper />).
You should use it as a component:
return (
<MenuWrapper kind={kind} id={item.id} ...otherProps />
);
and then:
export function MenuWrapper({ kind, id, isEdit }) {
// do stuff
}
I'm learning React at the moment, and I am currently trying out the Slate Markdown Editor in CodeSandbox. I am trying to initialize a Slate Editor instance like this:
import React from "react";
import ReactDOM from "react-dom";
import "./styles.css";
import Editor from "slate-md-editor";
const MdEditor = Editor();
<MdEditor />;
However, CodeSandbox displays the following error when I attempt to compile my code:
at evaluate (https://oxxbg.csb.app/node_modules/canner/slate-icon-codeblock/lib/index.js:80:45z
https://codesandbox.io/static/js/sandbox.cddc3c052.js:1:98419
X.evaluate
https://codesandbox.io/static/js/sandbox.cddc3c052.js:1:110543
ye.evaluateTranspiledModule
https://codesandbox.io/static/js/sandbox.cddc3c052.js:1:120123
c
https://codesandbox.io/static/js/sandbox.cddc3c052.js:1:110289
evaluate
https://oxxbg.csb.app/node_modules/slate-md-editor/lib/index.js:34:27
z
https://codesandbox.io/static/js/sandbox.cddc3c052.js:1:98419
X.evaluate
https://codesandbox.io/static/js/sandbox.cddc3c052.js:1:110543
ye.evaluateTranspiledModule
https://codesandbox.io/static/js/sandbox.cddc3c052.js:1:120123
c
https://codesandbox.io/static/js/sandbox.cddc3c052.js:1:110289
evaluate
/src/index.js:5
2 | import ReactDOM from "react-dom";
3 | import "./styles.css";
4 |
> 5 | import Editor from "slate-md-editor";
6 | const MdEditor = Editor();
7 |
8 | <MdEditor />;
I have not experienced this previously, and I am at a loss of knowing what to do.
I would greatly appreciate it if someone more experienced has some idea of this issue. Thank you in advance.
Link to the Sandbox
This is my package.json:
{
"name": "slate-editor",
"version": "1.0.0",
"description": "React testing project",
"keywords": [
"react",
"starter"
],
"main": "src/index.js",
"dependencies": {
"antd": "4.10.2",
"immutable": "4.0.0-rc.12",
"react": "17.0.0",
"react-dom": "17.0.0",
"react-scripts": "3.4.3",
"slate": "0.59.0",
"slate-md-editor": "1.5.4",
"slate-react": "0.59.0",
"slate-schema-violations": "0.1.39"
},
"devDependencies": {
"typescript": "3.8.3"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
},
"browserslist": [
">0.2%",
"not dead",
"not ie <= 11",
"not op_mini all"
]
}
Here is what I found out later:
Step 1
Setup a basic React project like so:
npx create-react-app slate-editor-demo
cd slate-editor-demo
Step 2
Delete the App.js, App.testing.js, and App.css in the src/ folder. You should now only have five files in that folder:
index.css
index.js
logo.svg
reportWebVitals.js
setupTests.js
Step 3
Install dependencies for slate-md-editor. Here they are:
"#testing-library/jest-dom": "^5.11.4",
"#testing-library/react": "^11.1.0",
"#testing-library/user-event": "^12.1.10",
"antd": "^3.8.4",
"immutable": "^3.8.2",
"react": "^16.4.2",
"react-dom": "^16.4.2",
"react-scripts": "1.1.5",
"slate": "^0.40.2",
"slate-react": "^0.12.0",
"slate-schema-violations": "^0.1.39",
"slate-md-editor": "1.5.4",
"web-vitals": "^1.0.1"
You can simply copy these dependencies into your package.json, and then run npm install to update the deps.
Step 4
Edit your src/index.js to look something like this:
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import Editor from "./slateMDEditor";
import reportWebVitals from './reportWebVitals';
const options = {
// default settings
prismOption: {
// https://github.com/GitbookIO/slate-prism
onlyIn: node => node.type === 'code_block',
getSyntax: node => node.data.get('syntax')
},
codeOption: {
// https://github.com/GitbookIO/slate-edit-code
onlyIn: node => node.type === 'code_block'
},
blockquoteOption: {
// https://github.com/GitbookIO/slate-edit-blockquote
},
listOption: {
// https://github.com/GitbookIO/slate-edit-list
types: ['ordered_list', 'unordered_list'],
typeItem: 'list_item',
typeDefault: 'paragraph'
}
}
const MdEditor = Editor(options);
ReactDOM.render(
<MdEditor onChange={this.onChange} />,
document.getElementById('root')
);
// Start measuring performance in app
reportWebVitals();
Step 5
Last step: run npm run start (if you use NPM), or yarn start (if you use yarn). You should see slate markdown editor running smoothly in your browser on localhost.
Other hooks in my components work normally, but useLocation throws an invalid hook call. To make sure this wasn't due to some obscure interference from weird logic in the rest of the app, I reduced the problem further and further untill I basically ended up with the example from the useLocation docs on https://reactrouter.com/web/api/Hooks and it still doesn't work. Here's my code:
import React from 'react'
import ReactDOM from 'react-dom'
// import App from "./App";
import { BrowserRouter as Router, Switch, useLocation, Route } from 'react-router-dom'
// import './index.scss'
// import * as serviceWorker from './serviceWorker'
// const dotenv = require('dotenv')
// ReactDOM.render(<App />, document.getElementById("root"));
function usePageViews() {
let location = useLocation()
console.log('===location===', location)
}
function App() {
usePageViews()
return (
<Switch>
...
{/* <Route path="*" component={() => <div>TEST</div>}></Route> */}
</Switch>
)
}
ReactDOM.render(
<Router>
<App />
</Router>,
document.getElementById('root')
)
I tryed all sorts of things but I don't know how to reduce the problem down from here. I used this hook before in a different project and it worked fine. I tried deleting my node_modules and package-lock.json and re-installing the packages and the same error persists. Here's my package.json:
{
"name": "personal_website",
"version": "0.1.0",
"private": true,
"dependencies": {
"#types/jest": "24.0.23",
"#types/node": "12.12.11",
"#types/react": "16.9.11",
"#types/react-dom": "16.9.4",
"react": "^16.12.0",
"react-dom": "^16.12.0",
"react-scripts": "3.2.0",
"typescript": "3.7.2"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"dotenv": "^8.2.0"
}
}
EDIT: It's clearly something to do with project setup. Obviously this code works for other people otherwise it wouldn't be there in the docs, and I set up a fresh project with the same code and it works. It's just that I have no clue what part of the project setup could cause this kind of issue.
I copied your code and it works without problems. Check here.
Maybe your usePageViews function isn't a valid React Component on your call context, which means that hooks won't work inside of it. You should do:
<usePageViews />
instead of:
usePageViews()
of course, this within the App return.
Ok, so I tracked it down. Turns out, I had react-router-dom installed further up in the project hierarchy (this app is the client app and it's inside of a larger project containing several apps and with its own package.json) but not in the local package.json. As you can see from the project.json i posted, react-router-dom isn't actually in there. The Router still worked, apparently succesfully working with the parent's node_modules but the hook threw this error which is very weird and unexpected. Hopefully this helps someone else having a simmilar problem.
I built something from react for the first time, and I'm about to deploy it on my github.
But when I go to my repository from github, it displays a blank page(But I'm not sure if the part of css is working as I can see black background color on it)
Since it doesn't show any error on the console, it was hard for me to figure out what the problem is.
I'd be great if someone can help me out on this.
https://stone-skipper.github.io/svs/#/
This is the link I'm trying to deploy on.
- Some people mentioned the problems from BrowserRouter, so I tried HashRouter, but it also didn't work.
// App.js
import React from "react"
import { Sev, Contents } from "./screens"
import { BrowserRouter as Router, Route } from "react-router-dom"
import "./App.css"
function App() {
return (
<Router>
<Route exact path="/" component={Sev} />
<Route path="/contents" component={Contents} />
</Router>
)
}
export default App
//package.json
{
"homepage": "http://stone-skipper.github.io/svs/",
"name": "sev-exhib",
"version": "0.1.0",
"private": true,
"dependencies": {
"modali": "^1.2.0",
"react": "^16.8.6",
"react-cursor-position": "^3.0.3",
"react-dom": "^16.8.6",
"react-router-dom": "^5.0.1",
"react-scripts": "3.0.1",
"styled-components": "^4.3.2"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"predeploy": "npm run build",
"deploy": "gh-pages -d build"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"gh-pages": "^2.1.1"
}
}
//index.js
import React from "react"
import ReactDOM from "react-dom"
import "./index.css"
import App from "./App"
import * as serviceWorker from "./serviceWorker"
ReactDOM.render(<App />, document.getElementById("root"))
serviceWorker.unregister()
I am trying to get data from firebase collection but getting error, can anybody help?
I am getting this error
Error: Context from react-redux not found. If you are using react-redux v6 a v3.. version of react-redux-firebase is required. Please checkout the v3 migration guide:
here is my code.
import React, { Component } from 'react'
import Notifications from './Notifications';
import ProjectList from '../projects/ProjectList';
import {connect} from 'react-redux';
import {firestoreConnect} from 'react-redux-firebase';
import {compose} from 'redux';
import { firebaseConnect, isLoaded, isEmpty } from 'react-redux-firebase'
class Dashboard extends Component {
render() {
return (
<div className="dashboard container">
<div className="row">
<div className="col s12 m6"><ProjectList projects={this.props.projects}></ProjectList></div>
<div className="col s12 m5 offset-m1"><Notifications></Notifications></div>
</div>
</div>
)
}
}
const mapStateToProps = (state) =>
{
console.log(state);
return ({projects:state.projectReducer.projects})
}
export default compose(firebaseConnect([
'projects'
]), connect(mapStateToProps)) (Dashboard)
I have tried this export but same error
export default compose(connect(mapStateToProps), firestoreConnect([
{collection:'project'}
])) (Dashboard)
here is my package.json
{
"name": "myproject",
"version": "0.1.0",
"private": true,
"dependencies": {
"firebase": "^5.7.0",
"firedux": "^1.1.0",
"react": "^16.6.3",
"react-dom": "^16.6.3",
"react-redux": "^6.0.0",
"react-redux-firebase": "^2.2.5",
"react-router-dom": "^4.3.1",
"react-scripts": "2.1.1",
"redux": "^4.0.1",
"redux-firestore": "^0.6.0",
"redux-thunk": "^2.3.0"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": [
">0.2%",
"not dead",
"not ie <= 11",
"not op_mini all"
]
}
The error is quite clear, you are using react-redux-firebase:^2.2.5 while you need to use v3, because you are using react-redux: ^6.0.0.
You have to update react-redux-firebase
If you're following along with someone's example and not building a production app, you can just switch to react-redux 5.1.1.
Just change "react-redux": "^6.0.0", to "react-redux": "^5.1.1", in package.json and run npm install.
#kabanny above is quite correct.
If you really want to use the next version of react-redux-firebase that supports the context api, then do the following at your own risk.
npm install react-redux-firebase#next
Also check out the migration guide
If you did install the packages using npm. downgrading to react-redux-firebase#2.2.5 and react-redux#5.1.1 did solve the problem in my case. Something to take note of: In case you face such errors when following someone's tutorials, I recommend you first confirm the version of dependencies they are using. npm can land you in trouble.