Problem with React-Bootstrap/InputGroup caused by <InputGroup.Prepend> - javascript

I have been encountering a problem that I didn't have before.
Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.
I have a Searchbar.jsx component in my React that I export and then I import it in my Navigation.jsx component. The Searchbar component looks like this:
import { useHistory } from 'react-router-dom';
import { useState } from 'react';
import { FontAwesomeIcon } from '#fortawesome/react-fontawesome';
import { faSearch } from '#fortawesome/free-solid-svg-icons';
import Form from 'react-bootstrap/Form';
import { Collapse, InputGroup } from 'react-bootstrap';
import Button from 'react-bootstrap/Button';
import './Searchbar.scss';
export default function Searchbar() {
const history = useHistory();
const [searchQuery, setSearchQuery] = useState('');
const [open, setOpen] = useState(false);
const onSubmit = (e) => {
history.push(`/dashboard/?s=${searchQuery}`);
e.preventDefault();
};
return (
<Form className="d-inline-block" bsPrefix="form" onSubmit={onSubmit}>
<InputGroup>
<InputGroup.Prepend>
<Button
variant=""
id="button-addon1"
onClick={searchQuery.length < 1 ? () => setOpen(!open) : onSubmit}
>
<FontAwesomeIcon icon={faSearch} className="searchIcon" />
</Button>
</InputGroup.Prepend>
<Collapse in={open} className="collapse d-sm-block">
<Form.Control
value={searchQuery}
onChange={(e) => setSearchQuery(e.target.value)}
type="text"
id="header-search"
placeholder="Search..."
name="s"
className="border border-secondary"
/>
</Collapse>
</InputGroup>
</Form>
);
}
This is how I import it later in my Navigation.jsx:
import Searchbar from './Searchbar/Searchbar';
/*...
...
a bunch of code
...
... */
return (
<>
<Navbar bg="white" className="mb-3 px-3" bsPrefix="navbar">
<Container className="justify-content-start d-flex-inline col-9 col-sm-6">
<Navbar.Brand href="/dashboard">
<img style={{ width: '100px' }} src={logo} alt="Logo" />
</Navbar.Brand>
</Container>
<Searchbar />
<>
I have made sure that export and import stuff the correct way.
As you can see I use React-Bootstrap/InputGroup and the problem seems to be caused by <InputGroup.Prepend> tag because as soon as I remove it, the error disappears.
What is more weird is that I don't get this error when I run it on my laptop despite it being the same exact code. So naturally I thought that is might be caused by different versions of react-bootstrap or, react-router-dom or even google chrome but I checked all of those and they are the same across my stationary computer and laptop. I get the same error across different web browsers on my stationary as well. I am really lost and I don't know how to solve this issue.
Any help or guidance will be appreciated.
// I just updated node version on my laptop and then yarn install in my web-app folder and now I'm getting the same error on my laptop as well. Could this be an issue with node or react-bootstap or react-router-dom? In that case what can I do?
//edit I have include my package.json
{
"name": "profet-web-app",
"version": "0.1.0",
"private": true,
"proxy": "https://localhost:5001",
"dependencies": {
"#emotion/react": "^11.9.3",
"#emotion/styled": "^11.9.3",
"#fortawesome/fontawesome-svg-core": "^1.2.36",
"#fortawesome/free-solid-svg-icons": "^5.15.4",
"#fortawesome/react-fontawesome": "^0.1.17",
"#mui/material": "^5.8.7",
"#testing-library/jest-dom": "^5.11.9",
"#testing-library/react": "^11.2.5",
"#testing-library/user-event": "^12.6.3",
"#types/jest": "^27.4.1",
"#types/node": "^17.0.23",
"#types/react": "^18.0.2",
"#types/react-dom": "^18.0.0",
"axios": "^0.21.1",
"bootstrap": "^5.2.0",
"chart.js": "^3.7.1",
"jwt-decode": "^3.1.2",
"moment": "^2.29.1",
"nodemon": "^2.0.7",
"react": "^17.0.1",
"react-bootstrap": "^2.5.0",
"react-chartjs-2": "^4.0.1",
"react-dom": "^17.0.1",
"react-ga": "^3.3.0",
"react-icons": "^4.3.1",
"react-number-format": "^4.4.4",
"react-responsive": "^9.0.0-beta.6",
"react-router-bootstrap": "^0.26.2",
"react-router-dom": "^5.2.0",
"react-router-hash-link": "^2.4.3",
"react-scripts": "^5.0.0",
"swiper": "^8.0.7",
"typescript": "^4.6.3",
"validator": "^13.7.0",
"web-vitals": "^1.1.0"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"lint": "eslint .",
"lint:fix": "eslint --fix ."
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"#types/react-router-dom": "^5.2.0",
"#typescript-eslint/eslint-plugin": "^5.19.0",
"#typescript-eslint/parser": "^5.19.0",
"eslint": "^8.13.0",
"eslint-config-airbnb": "^19.0.4",
"eslint-config-airbnb-typescript": "^17.0.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-jsx-a11y": "^6.5.1",
"eslint-plugin-prettier": "^4.0.0",
"eslint-plugin-react": "^7.29.4",
"eslint-plugin-react-hooks": "^4.4.0",
"prettier": "^2.6.2",
"sass": "^1.39.0"
}
}

I think I found why this happens.
I went through react bootstrap documentation and found this:
dropped InputGroupPrepend and InputGroupAppend. Buttons and InputGroupText can now be added as direct children.
Link:https://react-bootstrap.netlify.app/migrating/#inputgroup

Related

TypeError: URL is not a constructor while using react-router-dom

I have been working on one of my personal projects which includes use of react-router-dom.I switched from react-router-dom v5 to v6 and errors started popping out .I have used react-router before but the error shown this time is kind of different and not able to debug the thing since a while. Why it is showing this kind of error?
I
here is my App.js
import React, { useState } from "react";
import { BrowserRouter as Router, Routes, Route } from "react-router-dom";
import Navbar from "./components/Navbar";
import StreetNode from "./components/StreetNode";
import Nodes from "./components/Nodes";
import { Scheduler } from "./components/Scheduler";
function App() {
return (
<Router>
<Navbar />
<Routes>
{/* <Route path="/" exact component={Nodes} /> */}
<Route path="/" element={<Nodes />} />
<Route path="/node/:id" element={<StreetNode />} />
<Route path="/scheduler" element={<Scheduler />} />
</Routes>
</Router>
);
}
export default App;
index.js
index.js
import React from "react";
import ReactDOM from "react-dom";
import "./index.css";
import App from "./App";
import reportWebVitals from "./reportWebVitals";
import { NodeProvider } from "./NodeContext";
ReactDOM.render(
<NodeProvider>
<App />
</NodeProvider>,
document.getElementById("root")
);
// If you want to start measuring performance in your app, pass a function
// to log results (for example: reportWebVitals(console.log))
reportWebVitals();
package.json
{
"name": "lightitup",
"version": "0.1.0",
"private": true,
"dependencies": {
"#craco/craco": "^6.4.2",
"#date-io/date-fns": "^2.11.0",
"#emotion/react": "^11.4.1",
"#emotion/styled": "^11.3.0",
"#mui/icons-material": "^5.2.0",
"#mui/lab": "^5.0.0-alpha.61",
"#mui/material": "^5.0.2",
"#mui/styles": "^5.0.1",
"#testing-library/jest-dom": "^5.14.1",
"#testing-library/react": "^11.2.7",
"#testing-library/user-event": "^12.8.3",
"axios": "^0.22.0",
"bootstrap": "^5.1.3",
"chart.js": "^3.6.2",
"chartjs-plugin-zoom": "^1.2.0",
"classnames": "^2.3.2",
"date-fns": "^2.27.0",
"react": "^17.0.2",
"react-chartjs-2": "^4.0.0",
"react-dom": "^17.0.2",
"react-google-charts": "^3.0.15",
"react-notifications-menu": "^1.0.6",
"react-router-dom": "^6.4.5",
"react-scripts": "4.0.3",
"web-vitals": "^1.1.2"
},
"scripts": {
"start": "craco start",
"build": "craco build",
"test": "craco 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"
]
},
"devDependencies": {
"autoprefixer": "^9.8.8",
"postcss": "^7.0.39",
"react-circular-slider-bar": "^1.3.1",
"tailwindcss": "npm:#tailwindcss/postcss7-compat#^2.2.17"
}
}
You must wrap Router with BrowserRouter. I'm not sure if this is because of the error but you must wrap BrowserRouter anyway.

React blank page or 404 page after npm run build, Where did the problem come from?

My index.html shows an empty page, after npm run build(react).
First of all, I tried this.
adding in package.json
homepage : "./" or "." or "absolute file PATH"
And my package.json is...
{
"name": "client",
"homepage": "./",
"version": "0.1.0",
"private": true,
"dependencies": {
"#testing-library/jest-dom": "^5.14.1",
"#testing-library/react": "^11.2.7",
"#testing-library/user-event": "^12.8.3",
"axios": "^0.21.4",
"bootstrap": "^5.1.0",
"dotenv": "^10.0.0",
"html-react-parser": "^1.3.0",
"http-proxy-middleware": "^2.0.1",
"quill": "^1.3.7",
"react": "^17.0.2",
"react-bootstrap": "^2.0.0-beta.6",
"react-dom": "^17.0.2",
"react-ga": "^3.3.0",
"react-html-parser": "^2.0.2",
"react-icons": "^4.2.0",
"react-kakao-maps-sdk": "^1.0.3",
"react-loading-skeleton": "^3.0.1",
"react-quill": "^1.3.5",
"react-redux": "^7.2.5",
"react-router-dom": "^5.3.0",
"react-scripts": "4.0.3",
"react-use-clipboard": "^1.0.7",
"redux": "^4.1.1",
"redux-devtools-extension": "^2.13.9",
"redux-persist": "^6.0.0",
"redux-promise": "^0.6.0",
"redux-thunk": "^2.3.0",
"use-react-router": "^1.0.7",
"web-vitals": "^1.1.2"
},
"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"
]
}
}
and my app.js file is...
import React, { useEffect } from "react";
import { Switch, Route, BrowserRouter } from "react-router-dom";
//import some components omitted
const App = () => {
// some logical code
return (
<BrowserRouter history={history}>
<Switch>
<Route path="/contact" component={Contact} exact />
<Route path="/withdrawal" component={Withdrawal} exact />
{* and some Route code omitted *}
<Route path="*" component={ErrPage} />
</Switch>
</BrowserRouter>
);
};
export default App;
What's the problem with my code?
Or what setting should I do more?
After you build your app, you need to open your "build" folder... and then open your index.html with an editor...
After that you need search for sources that have "static", like this one:
So you need to put a point before the bar, like this example:
You need to put in all of static sources in this archieve.
An another if you're using react-router and trying to open index.html directly in the browser (or using electron, which essentially does that), in addition to setting homepage as others have suggested, replace your BrowserRouter with a HashRouter.
one solution similar this one you can see in this post: Blank page after running build on create-react-app

Page style takes time to load with Next Js

The style of the page takes time to load when the user open the page or make reload in the page. I don't have much time working with next js, still learning the hard skills of the framework.
This is an gif showing when i open the page. This also happens when I reload or enter a page and come back right away: https://imgur.com/J24YAPl
This is my _document.jsx:
import React from 'react';
import Document, { Head, Main, NextScript } from 'next/document';
import { ServerStyleSheet } from 'styled-components';
export default class MyDocument extends Document {
static getInitialProps({ renderPage }) {
const sheet = new ServerStyleSheet();
const page = renderPage(App => props => sheet.collectStyles(<App {...props} />));
const styleTags = sheet.getStyleElement();
return { ...page, styleTags };
}
render() {
return (
<html lang="en">
<Head>
{this.props.styleTags}
<link rel="manifest" href="/manifest.webmanifest" />
<meta name="them-color" content="#189AB4" />
<link rel="apple-touch-icon" sizes="72x72" href="/next/icons/icon-72x72.png" />
</Head>
<body>
<Main />
<div id="portal" />
<NextScript />
</body>
</html>
);
}
}
and this is my package.json:
{
"name": "novo-site",
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start"
},
"dependencies": {
"#zeit/next-css": "^1.0.1",
"axios": "^0.21.1",
"classnames": "2.2.6",
"dotenv": "^10.0.0",
"file-loader": "^6.2.0",
"formik": "2.1.5",
"jquery": "3.5.1",
"lodash": "4.17.20",
"next": "11.0.0",
"next-absolute-url": "^1.2.2",
"next-compose-plugins": "^2.2.1",
"next-images": "^1.8.1",
"next-transpile-modules": "^8.0.0",
"normalize.css": "8.0.1",
"polished": "3.6.5",
"prop-types": "15.7.2",
"react": "17.0.2",
"react-animated-burgers": "1.2.8",
"react-collapse": "5.0.1",
"react-dom": "17.0.2",
"react-icons": "^4.2.0",
"react-is": "16.13.1",
"react-on-screen": "2.1.1",
"react-reveal": "1.2.2",
"react-scroll": "1.8.1",
"react-slick": "0.27.9",
"react-sticky-el": "2.0.5",
"react-string-replace": "0.4.4",
"react-use": "15.3.3",
"shortid": "2.2.15",
"slick-carousel": "1.8.1",
"styled-components": "^5.3.0",
"styled-media-query": "2.1.2",
"styled-system": "5.1.5",
"typewriter-effect": "^2.18.0",
"url-loader": "^4.1.1",
"webpack": "^5.39.1",
"yup": "0.29.3"
},
"devDependencies": {
"#types/react": "17.0.11",
"typescript": "4.3.2",
"webpack": "^5.23.0"
}
}
I found the solution after try fix other bug on the project. Basically, my project has two custom pages, _app.jsx and _document.jsx, but outside the /pages directory.
After read the documentation, could see this difference with the docs recomendation. Then i moved _app.jsx and _document.jsx to /pages folder and got the right solution.

How to use material-ui theme along with redux in gatsby

I am using material-ui theme along with redux-toolit in gatsby.
My theme.ts file:
import { createMuiTheme } from "#material-ui/core";
import { useSelector } from "react-redux";
import { State } from "./Types/SliceTypes";
export const Theme = () => {
const islit = useSelector((state: State) => state.themes.value);
const theme = createMuiTheme({
palette: {
type: islit ? "light" : "dark",
},
});
return theme;
}
Now When I import this Theme in gatsby-browser and gatsby-ssr file it give an error. Invalid hook call. Hooks can only be called inside of the body of a function component
Even if I don't use islit and use theme only that is inside the Theme function the code still doesn't work.
My gatsby-browser.js and gatsby-ssr.js file:
import React from 'react';
import { Provider } from 'react-redux';
import store from './src/Global/store';
import { ThemeProvider } from "#material-ui/core/styles";
import { Theme } from "./src/Global/theme";
export const wrapRootElement = ({ element }) => {
return <Provider store={store}><ThemeProvider theme={Theme()}>{element}</ThemeProvider></Provider>;
};
My gatsby-config.js file:
plugins: [
`gatsby-plugin-react-helmet`,
`gatsby-plugin-material-ui`,
`gatsby-plugin-image`,
My package.json file:
{
"name": "gatsby-starter-default",
"private": true,
"description": "A simple starter to get up and developing quickly with Gatsby",
"version": "0.1.0",
"author": "Kyle Mathews <mathews.kyle#gmail.com>",
"dependencies": {
"#contentful/rich-text-react-renderer": "^14.1.3",
"#material-ui/core": "^4.11.3",
"#material-ui/icons": "^4.11.2",
"#reduxjs/toolkit": "^1.5.1",
"#types/react": "^17.0.3",
"#types/react-dom": "^17.0.3",
"#types/react-redux": "^7.1.16",
"#typescript-eslint/eslint-plugin": "^4.22.0",
"#typescript-eslint/parser": "^4.22.0",
"dotenv": "^8.2.0",
"firebase": "^8.4.1",
"firebaseui": "^4.8.0",
"gatsby": "^3.3.0",
"gatsby-plugin-firebase": "^0.2.0-beta.4",
"gatsby-plugin-gatsby-cloud": "^2.2.0",
"gatsby-plugin-image": "^1.2.1",
"gatsby-plugin-manifest": "^3.2.0",
"gatsby-plugin-material-ui": "^3.0.1",
"gatsby-plugin-offline": "^4.2.0",
"gatsby-plugin-react-helmet": "^4.2.0",
"gatsby-plugin-sharp": "^3.2.1",
"gatsby-source-contentful": "^5.3.0",
"gatsby-source-filesystem": "^3.2.0",
"gatsby-transformer-remark": "^4.0.0",
"gatsby-transformer-sharp": "^3.2.0",
"prop-types": "^15.7.2",
"react": "^17.0.1",
"react-dom": "^17.0.1",
"react-helmet": "^6.1.0",
"react-redux": "^7.2.3",
"typescript": "^4.2.4"
},
"devDependencies": {
"#types/react-helmet": "^6.1.1",
"prettier": "2.2.1"
},
"keywords": [
"gatsby"
],
"license": "0BSD",
"scripts": {
"build": "gatsby build",
"develop": "gatsby develop",
"format": "prettier --write \"**/*.{js,jsx,ts,tsx,json,md}\"",
"start": "npm run develop",
"serve": "gatsby serve",
"clean": "gatsby clean",
"test": "echo \"Write tests! -> https://gatsby.dev/unit-testing\" && exit 1"
},
"repository": {
"type": "git",
"url": "https://github.com/gatsbyjs/gatsby-starter-default"
},
"bugs": {
"url": "https://github.com/gatsbyjs/gatsby/issues"
}
}
I have a similar set up and was able to solve this by following this setup recommended by MUI; https://github.com/mui-org/material-ui/tree/next/examples/gatsby
In the file gatsby/plugins/gatsby-plugin-top-layout/TopLayout.js you can add your redux Provider;
import { Provider } from "react-redux";
import { ThemeProvider } from "#material-ui/core/styles";
import { Theme } from "../../src/Global/theme";
import store from '../../src/Global/store';
...
<Provider store={store}>
<ThemeProvider theme={theme}>
{/* CssBaseline kickstart an elegant, consistent, and simple baseline to build upon. */}
<CssBaseline />
{props.children}
</ThemeProvider>
</Provider>
...

I've looked at the single-spa site on how to create parcels, I get exceptions

I've been trying to implement single-spa parcels, but I've been getting the following exception
index.js:167 Uncaught (in promise) Error: During 'mount', parcel threw an error:
<Parcel /> was not passed a mountParcel prop, nor is it rendered where mountParcel is within the React context.
If you are using <Parcel /> within a module that is not a single-spa application, you will need to import mountRootParcel from single-spa and pass it into <Parcel /> as a mountParcel prop
at index.js:167
at index.js:114
The error is about a mountParcel attribute of which is passed down the parcel, I've pulled it from single-spa library but still I get the same issue, I've been battling with this problem for a while now, please assist
Please find my code below
parcel-config.js
// myParcel.js
import React from 'react'
import ReactDOM from 'react-dom';
import singleSpaReact from 'single-spa-react'
import MyParcelComponent from './my-parcel-component.component';
export const MyParcel = singleSpaReact({
React,
ReactDOM,
rootComponent: MyParcelComponent
})
export function bootstrap(props) {
return MyParcel.bootstrap(props).then(res => {
debugger;
});
}
export function mount(props) {
return MyParcel.mount(props);
}
export function unmount(props) {
return MyParcel.unmount(props);
}
// in this case singleSpaReact is taking our inputs and generating an object with the required lifecycles.
MyParcelComponent.js
import React from 'react';
export default class MyParcelComponent extends React.Component {
render() {
return (
<div >
<h1>This is a parcel</h1>
</div>
);
}
}
single-spa application. attempt to consume the parcel
import React from 'react';
import Parcel from 'single-spa-react/parcel'
import * as singleSpa from 'single-spa';
import { MyParcel } from './parcel1/parcel-config';
export default class Root extends React.Component {
componentWillMount(){
console.log(singleSpa);
}
render() {
return (
<div style={{ marginTop: '100px' }}>
This was rendered by app 1, which is written in React.
<Parcel
config={MyParcel}
mountParcel={singleSpa.mountRootParcel}
wrapWith="div" />
</div>
);
}
}
package.json
{
"name": "simple-single-spa-webpack-example",
"version": "1.0.0",
"description": "A simple example of how to use webpack with single-spa",
"scripts": {
"watch": "webpack-dev-server --open",
"build": "webpack --config webpack.config.js -p"
},
"repository": {
"type": "git",
"url": "git+https://github.com/joeldenning/simple-single-spa-webpack-example.git"
},
"author": "Joel Denning",
"license": "MIT",
"bugs": {
"url": "https://github.com/joeldenning/simple-single-spa-webpack-example/issues"
},
"homepage": "https://github.com/joeldenning/simple-single-spa-webpack-example#readme",
"devDependencies": {
"babel-core": "^6.25.0",
"babel-loader": "^7.1.1",
"babel-plugin-syntax-dynamic-import": "^6.18.0",
"babel-preset-env": "^1.6.0",
"babel-preset-react": "^6.24.1",
"clean-webpack-plugin": "^0.1.16",
"css-loader": "^2.1.1",
"http-server": "^0.10.0",
"style-loader": "^0.23.1",
"ts-loader": "^2.3.2",
"typescript": "^2.4.2",
"webpack": "^3.4.1",
"webpack-dev-server": "^2.8.2"
},
"dependencies": {
"#angular/common": "^4.3.3",
"#angular/compiler": "^4.3.3",
"#angular/core": "^4.3.3",
"#angular/platform-browser": "^4.3.3",
"#angular/platform-browser-dynamic": "^4.3.3",
"#angular/router": "^4.3.6",
"core-js": "^2.4.1",
"es6-promise": "^4.1.1",
"react": "^15.6.1",
"react-dom": "^15.6.1",
"reflect-metadata": "^0.1.10",
"rxjs": "^5.4.2",
"single-spa": "^3.9.1",
"single-spa-angular2": "^1.2.0",
"single-spa-react": "^2.9.0",
"single-spa-vue": "^1.3.0",
"vue": "^2.6.10",
"zone.js": "^0.8.16"
}
}

Categories