How to import components in React Js? - reactjs

I have a error of Module not found. I think that such error is because of lack of a file or directory .However I have checked more than 30 times at that particular location and even for typos ,I don't think i have any mistake here.
Here is my code for DishdetailComponent.js
import React,{Component} from 'react';
import { Card, CardImg, CardImgOverlay, CardTitle, CardBody, CardText} from 'reactstrap';
class Dishdetail extends Component{
constructor(props){
super(props);
}
render(){
if(this.props.dish!=null){
return(
<div className="container">
<div className="row">
<div className="col-12 col-md-5 m-1">
<Card>
<CardImg width="100%" object src={dish.image} alt={dish.name} />
<CardBody>
<CardTitle>{dish.name}</CardTitle>
<CardText>{dish.description}</CardText>
</CardBody>
</Card>
</div>
</div>
</div>
)
}
else{
return <div></div>
}
}
}
export default Dishdetail;
I am calling this component from Menucomponent.js file.It has following code:
import React,{Component} from 'react';
import { Card, CardImg, CardImgOverlay, CardTitle, CardBody, CardText} from 'reactstrap';
import Dishdetail from './Components/DishdetailComponent';
class Menu extends Component{
constructor(props){
super(props);
this.state={
selectedDish:null
};
}
render() {
const menu = this.props.dishes.map((dish) => {
return (
<div key={dish.id} className="col-12 col-md-5 m-1">
<Card onClick={()=>this.onDishSelect(dish)}>
<CardImg width="100%" object src={dish.image} alt={dish.name} />
<CardImgOverlay>
<CardTitle>{dish.name}</CardTitle>
</CardImgOverlay>
</Card>
</div>
);
});
return (
<div className="container">
<div className="row">
{menu}
</div>
{/* {this.renderDish(this.state.selectedDish)} */}
<Dishdetail dish={this.state.selectedDish}/>
</div>
);
}
}
export default Menu;
The hierarchy of my code follows,
-src(folder)
-Components(sub-folder)
-menucomponent.js
-DishdetailComponent.js
The file I want to import to and the file which will recieve are in same folder Components.

Given your folder structure that you have provided, aren't menuComponent and dishDetailComponent in the same folder? perhaps you need to change your import line to
import Dishdetail from './DishdetailComponent';
as that is the relative path to the file from which you are calling the import.

By looking at your folder structure,
Both of the components are in same directory.
Then you will have to import it as
import Dishdetail from "./DishdetailComponent";
To avoid this kind of overhead of importing, you can simplify it by adding a jsconfig.json file at your root directory and configure as below.
{
"compilerOptions": {
"baseUrl": "src"
},
"include": ["src"]
}
This is to specify an absolute import path and now you can import it like
import Dishdetail from "Components/DishdetailComponent";

You have put the MenuComponent and the DishDetail Component in the same folder, so you can do
import Dishdetail from "./DishdetailComponent"
You should use a relative path as both you components are in the same folder and the
./ tells the program to look in the same folder as the current file is in. As you're using ./ in the MenuComponent, which is in the Components folder, ./ looks inside the components folder.
./Components/DishdetailComponent searches Components directory in the folder that MenuComponent is in, and since there is no such directory in that folder, it throws an error.

It seems both files are in the same level
Try using
import Dishdetail from './DishdetailComponent';

Related

Image not getting displayed in react project

This is my App.js. Here, I call the "Profile" Component.
import './App.css';
import Profile from "./Profile"
function App() {
return (
<div className="App">
<Profile />
</div>
);
}
export default App;
Then, inside Profile.js, I call Card component and inside the Card component, I've enclosed an image.
import React from 'react'
import Card from './Card'
import styles from "./Profile.module.css"
import image1 from "./assets/profile1.png"
const Profile = () => {
return (
<Card>
<div>
<img src={image1} alt="" />
</div>
</Card>
)
}
export default Profile
Inside of Card component, I've just applied some CSS to make it look like a Card.
import React from 'react'
import styles from "./Card.module.css"
const Card = () => {
return (
<div className={styles.card}>
</div>
)
}export default Card
This is my folder structure.
I'm really confused why the image isn't getting showed up. Currently this is the output I'm getting.
I've restarted the server as well. But it's not getting fixed.
your card doesn't have a child component return maybe that could be the problem
import React from 'react'
import styles from "./Card.module.css"
const Card = ({children}) => {
return (
<div className={styles.card}>
{children}
</div>
)
}
export default Card
try this

Calling card component

I have the following code that produces a Card UX Component.:
import React from 'react';
import image1 from '../images/swimming_sign.jpg';
const Card = props =>{
return(
<div className="card text-center">
<div className="overflow">
<img src="image1" alt='Image 1'/>
</div>
</div>
);
}
export default Card;
I would like to know how I can call it in one of my pages that simply produces an image at the top half of the page, and also what is the command for importing the "bootstrap.min.css" from "node_modules" in this page. The code for the page currently looks like this:
import React from 'react'
import Hero from '../Components/Hero';
import Card from '../Components/ServicesUi';
export default function Services() {
return (
<Hero hero="servicesHero"/>
);
}
Card Import
import React from 'react';
let image1 = '../images/swimming_sign.jpg';
const Card = props =>{
return(
<div className="card text-center">
<div className="overflow">
<img src={image1} alt='Image 1'/>
</div>
</div>
);
}
export default Card;
import React from 'react'
import Hero from '../Components/Hero';
import Card from '../Components/ServicesUi';
export default function Services() {
return (
<Card/>
<Hero hero="servicesHero"/>
);
}
To import css file from node modules, please place the link statement in your index.html file
<link rel="stylesheet" href="./node_modules/bootstrap/bootstrap.min.css">

Have a common header layout in nextjs

I have 2 pages user.js and nonuser.js and one component header. user.js and nonuser.js have same functionality with slight changes in UI. Now I want to integrate all this. Like when I visit the page by default table of user.js must be viewed. One click of nonuser.js it should change to the nonuser.js table. And I want header to be same for both, content in textbox should not change when I switch between pages.
I'm new to next.js and react
header.js
import React, { Component } from 'react';
import '../Header/header.css';
import { Menu, Input, Icon } from 'antd';
import Link from 'next/link';
class HeaderComponent extends Component {
render() {
return (
<div className="navbar">
<div className="header">
<div className="col-1">
<div className="menu">
<div>
<Link href="/User"><a>Users</a></Link>
</div>
<div>
<Link href="/nonUser"><a>Non Users</a></Link>
</div>
<Input className="text-box" placeholder="Enter name" prefix={<Icon type="search" ></Icon>}></Input>
</div>
</div>
</div>
</div>
)
}
}
export default HeaderComponent
user.js
class User extends Component {
render() {
return (
<React.Fragment>
<div className="ant-table-row">
<div className="table-head-text">
<span className="text">Users({data.length})</span>
<Pagination defaultCurrent={1} total={100} />
</div>
<Table
rowKey={data._id}
columns={this.columns1}
rowSelection={this.rowSelection}
onExpand={this.onExpand}
dataSource={data} />
</div>
</React.Fragment>
)
}
I didn't add nonuser component, its same as user component
index.js
import Header from '../components/Header/header';
import Layout from '../components/Layout';
function App() {
return (
<Header/>
<div>
</div>
)
}
export default App;
I've done this, On first landing the only header is there and on clicking user link in header, header disappears and only table of user is shown.
EDIT:
I tried this header appears in both and I placed a textbox in header .textbox value clears when I switch between pages.
user.js and nonuser.js
render(){
return(
<Layout>
<div>.....</div>
</Layout>
)
}
Also tried
index.js
render() {
return (
<Layout>
<div>
</div>
</Layout>
)
}
layout.js
const Layout = ({children}) => (
<div>
<Header></Header>
{children}
</div>
);
From what I make of your question, you want to use HeaderComponent as a common header for both pages? Then I'd suggest placing it in your components/Layout file. Next will wrap all pages in the layout component, thus adding your header to all pages.
I'm also wondering why you have an index.js file? Unless it's placed in pages/ folder, it isn't something you normally do in Next. The pages user.js and nonuser.js should also be placed in the pages/ folder. Next will then automatically load the to files and provide them under the routes /user and /nonuser (based on the name of the file). This will also make Next wrap each page in the layout component mentioned above.
I'd suggest looking into NextJS learning guide. It provides a very good introduction to NextJS and will make it a lot easier to use NextJS if you. They have a lesson explaining how to use Shared Components which explains exactly what you seem to be looking for.
Hope this helps a bit.
Edit:
Example using _app.js
The following is an example of how to use a custom layout component in next using _app.js. It's based on Nexts own example.
// components/Layout.js
import React, { Component } from 'react';
import Header from './Header';
class Layout extends Component {
render () {
const { children } = this.props
return (
<div className='layout'>
<Header />
{children}
</div>
);
}
}
// pages/_app.js
import React from 'react';
import App from 'next/app';
import Layout from '../components/Layout';
export default class MyApp extends App {
render () {
const { Component, pageProps } = this.props
return (
<Layout>
<Component {...pageProps} />
</Layout>
)
}
}
To get more information on how to make use of _app.js properly, check out their documentation on custom app.

Creating a new component and adding it to the webpage in ReactJS

In reactjs, I have already created one component and added it to my Main.js file but now, I need to add one more component to it and due to JSX restriction of having only one root element per JSX, I cannot add one more component to it. I don't know where I'm going wrong in this.
`~~~~index.js file`
import React from "react";
import ReactDOM from "react-dom";
import Main from "./Main";
ReactDOM.render(
<Main/>,
document.getElementById("root")
);
-------------------------------------------------------
Main.js file
import React, { Component } from "react";
import Header from './Header';
// import Body from './Body';
import './index.css';
class Main extends Component {
render() {
return (
<div className="Main">
<Header image={require('./demo.jpg')}/>
{/* <Body/> */}
</div>
);
}
}
export default Main;
-------------------------------------------------------
Header.js file
import React from 'react'
import './Header.css';
// import Body from './Body';
export default (props) => {
const style= {
backgroundImage : "url(" + props.image + ")"
}
return (
<div className="body" style={style}>
<button className="btn" > Drawer </button>
<h1 className="heading1"> Questions </h1>
<button className="heading2"> Eye Button </button>
<h2 className="heading3"> Hi.. Sandeep Gautam </h2>
<h3 className="heading4"> Mind answering a few questions </h3>
</div>
)
}`

FontAwesomeIcon "defined but never used" even though it is required

I recently added FontAwesomeReact to my React site. I'm using the icons in a Sidebar component that displays on every page.
Page:
import React from 'react'
import Header from '../components/header'
import Sidebar from '../components/sidebar'
import Layout from '../components/layout'
import Footer from '../components/footer'
class IndexPage extends React.Component {
render() {
return (
<Header />
<Sidebar />
<Layout>
<p>Hello there is some content here </p>
</Layout>
<Footer />
)
}
}
Sidebar component:
import React from 'react'
import Menucard from '../components/menucard'
import { library } from '#fortawesome/fontawesome-svg-core'
import { FontAwesomeIcon } from '#fortawesome/react-fontawesome'
import { faInfoCircle, /*...*/, faCheck} from '#fortawesome/free-solid-svg-icons'
library.add(faInfoCircle,/*...*/,faCheck)
const clubAdminMenu = (
<div>
<h2>Club Admin</h2>
<ul>
<li className="pod">
<FontAwesomeIcon icon="user" pull="right" /> Manage Registrations
</li>
<li className="pod">
<a href="..."><FontAwesomeIcon ... /> ...<a>
</li>
...
</ul>
</div>
)
class Sidebar extends React.Component {
render() {
return (
<div className="Sidebar">
<Menucard content={clubAdminMenu} />
...
</div>
)
}
}
export default Sidebar
At first I assumed that <FontAwesomeIcon /> would be defined everywhere, since it's imported in <Sidebar /> and <Sidebar /> is on every page. Clearly I was wrong, the icons did not show up on any page unless I explicitly included import { FontAwesomeIcon } from '#fortawesome/react-fontawesome' on every single page.
But when I include that import on every page, the compiler warns me that 'FontAwesomeIcon' is defined but never used about a zillion times (once on every page that doesn't include <FontAwesomeIcon /> in its body, even if it is included in <Sidebar />) I get why it's saying this, but if I remove the import, the icons do not render in the sidebar on that page.
These two things seem to contradict each other. Am I missing something? Is there a better way to do this?
This is the official documentation for Font Awesome v5.15 https://fontawesome.com/v5.15/how-to-use/on-the-web/using-with/react
It says that you need to include the below code in App.js (Not necessarily, but if you want to use font awesome icons in a lot of files, it is a good idea to make it Global)
import { library } from "#fortawesome/fontawesome-svg-core";
import { fab } from "#fortawesome/free-brands-svg-icons"; // To use branded icons such as Google, Github, etc.
import {
faCheckSquare,
faCoffee,
faInfoCircle,
// All other icons (except fab) that you want to use MUST be declared here
} from "#fortawesome/free-solid-svg-icons";
library.add(fab, faCheckSquare, faCoffee, faInfoCircle); // All icons imported must be added to the library
And then in every file where you want to use Font Awesome icons, you must include
import { FontAwesomeIcon } from "#fortawesome/react-fontawesome";
And then to use the Font Awesome icons, you need to do the following:
<FontAwesomeIcon icon={[iconType, icon]} />
where iconType = 'fab', 'fas', etc.
and icon = 'github', 'coffee', etc.
<FontAwesomeIcon icon={['fab', 'google']} />
<FontAwesomeIcon icon={['fas', 'coffee']} />
When you import
import { FontAwesomeIcon } from '#fortawesome/react-fontawesome'
You need to call
<FontAwesomeIcon />
The reason you get that message because you are just importing it but you never called it. So do as I suggested above

Resources