Uncaught ReferenceError: Searchbar is not defined - reactjs

Sorry, i am new in React.
I have 2 components in my react application. Here is the parent:
import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
import './Searchbar';
const App =() => {
return (
<div className="App">
<div className="App-header">
<Searchbar></Searchbar>
<h2>Welcome to React</h2>
</div>
</div>
);
}
export default App;
and here is the searchbar component:
import React,{Component} from 'react';
export default class Searchbar extends Component{
render(){
return <div>Here is search bar</div>;
}
}
Unfortunately, when the page loads it complains with the error:
Uncaught ReferenceError: Searchbar is not defined
It seems that, it does not recognize the Searchbar component.
What is the problem?

The solution is really simple. You are importing the searchbar wrongly in your file. You need to import it like import Searchbar from './Searchbar'; , since you have exported it as default you also need to import it in default manner.
import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
import Searchbar from './Searchbar';
const App =() => {
return (
<div className="App">
<div className="App-header">
<Searchbar></Searchbar>
<h2>Welcome to React</h2>
</div>
</div>
);
}
export default App;

Related

Why does my React Portal shows a Blank Page?

this is my index.html
<div id="root"></div>
<div id="portaltest"></div>
this my App.js
import React, { Component } from "react";
import PortalTest from "./PortalTest";
export class App extends Component {
render() {
return (
<div>
<PortalTest />
</div>
);
}
}
export default App;
this is what's in PortalTest.js
import React from "react";
import { ReactDOM } from "react";
function PortalTest() {
return ReactDOM.createPortal(
<div>This is the PORTAL DIV</div>,
document.getElementById("portaltest")
);
}
export default PortalTest;
This suppose to show the contents of PortalTest.js instead it only returns a blank page.
What did I do wrong?
Use import ReactDOM from 'react-dom' instead of import { ReactDOM } from 'react' and it should work.
See this sandbox.

My Custom Component is not working in react App.js

In my components/ folder I have created two components:
using function-based components:
import React from 'react';
const greet = () => <div className="App"><h3>Hello Sukarna Jana</h3></div>;
export default greet;
using class-based-components:
import React,{Component} from 'react';
class welcome extends Component{
render(){
return <div className="App"><h3>Hi, its Class Components</h3></div>;
}
}
export default welcome;
but when I am trying to import that in App.js it's not running:
import './App.css';
import React from 'react';
import greet from './components/greet';
import welcome from './components/welcome';
/*
* dont know why greet and welcome tags are not working
*/
function App() {
return (
<div className="App">
<greet/>
<welcome/>
<h1>Hello Sukarna Jana</h1><hr/>
</div>
);
}
export default App;
I just reviewed the code and could find the issues easily,
You didn't keep the naming rules in your code.
<greet/> replace to <Greet/>
<welcome/> replace to <Welcome>
Also, you should change the name of the component.

I have a simple code for reactjs but is showing is declared but its value is never read

welcome.js
import React, { Component } from 'react'
class welcome extends Component
{
render()
{
return <h1>Class Component</h1>
}
}
export default welcome
App.js
import React from 'react';
import logo from './logo.svg';
import './App.css';
import Greet from './components/Greet'
import welcome from './components/welcome'
function App() {
return (
<div className="App">
<Greet />
<welcome />
</div>
);
}
export default App;
So basically in welcome.js i have a code for rendering hello rahul and i have exported that and i have imported that in App.js but it showing welcome is declared but never used please help
You have a problem with naming. React assumes every tag beginning with a small letter is native (f ex a <div> or <table>). You have to name your own components with capital letters.
App.js
import React from 'react';
import logo from './logo.svg';
import './App.css';
import Greet from './components/Greet'
import Welcome from './components/Welcome'
function App() {
return (
<div className="App">
<Greet />
<Welcome />
</div>
);
}
export default App;
It's also a good idea to name your files with components with capital letters, to make them easy to identify.
You are importing a component in lower case letters. React doesn't recognize that as a component. So just restructure it as :
import Welcome from './components/Welcome'
then do
<Welcome/>

React create app - How do you import a nested component?

I am using React create app and I understand how to import a component into my main app which is called app.js.
What I do not understand is how I import a component in a component which is then imported in app.js (nested component).
In my app.js file, I have a component called Portfolio. In my portfolio.js file, I have a component called Language. Is it even possible to nest components with React create app? Because it does not seem to work.
This is what I tried to do:
In my app.js file :
import React from "react";
import logo from "./logo.svg";
import "./App.css";
import Portfolio from './components/portfolio';
import "./css/styles.css"
function App() {
return (
<div className="App">
<header>
<h1>My beautiful title</h1>
</header>
<Portfolio />
</div>
);
}
export default App;
In my portfolio.js file :
import React, {Component} from 'react';
import Language from './components/language';
class Portfolio extends Component {
render() {
return (
<div className="portfolio">
<Language/>
<h1>My protfolio</h1>
</div>
);
}
}
export default Portfolio;
And in language.js I have:
import React, {Component} from 'react';
class Language extends Component {
render() {
return(
<div>Switch language</div>
);
}
}
export default Language;
In my browser, I keep getting the following error:
"./src/components/portfolio.js Module not found: Can't resolve
'./components/language' in
'C:\Users\mluce\exercice-react\src\components'"
My language.js file is definitely sitting in my components folder. I do not understand why I get this error?
In portfolio.js:
import Language from './language';

SCRIPT1028: SCRIPT1028: Expected identifier, string or number in reactJS

When I import:
import {ReactComponent as Name} from '../assets/name.svg';
I get the error, but when I do:
import test from '../assets/name.svg';
The error goes away.
I've installed the react-polyfills and have imported them at the top of index.js:
import 'react-app-polyfill/ie11';
import 'react-app-polyfill/stable';
import React, {Component} from 'react';
import Particle from '../Particle/particle';
import {ReactComponent as Name} from '../assets/name.svg';
import './App.css';
class App extends Component {
constructor(props) {
super(props);
}
// componentDidMount() {
// console.log(this.state.points);
// }
render() {
return (
<React.Fragment>
<Particle />
<div className="name">
{/* <Name/> */}
</div>
</React.Fragment>
);
}
}
export default App;
It should be showing the svg Edge, but I get the error. When i uncomment the component, the page doesn't even load, but works in other browsers

Resources