Another beginner React question - QRCode Generation - javascript

Essentially, I want to generate a QR Code in a ReactJS application. I found a generator on npm for React & React Native. Here is how they set it up on the npm site:
import React from "react";
import ReactDOM from "react-dom";
import QRCode from "react-qr-code";
ReactDOM.render(<QRCode value="hey" />, document.getElementById("Container"));
Here is how I set up the page that will hold the QR Code:
import React from "react";
import ReactDOM from "react-dom";
import QRCode from "react-qr-code";
// empty profile page
class Profile extends React.Component
{
render()
{
return (
<div>
<h1>QR Code Page</h1>
</div>
);
}
}
//ReactDOM.render(<QRCode value="hey" />, document.getElementById("Container"));
export default Profile;
I have the actual QRCode code commented out, as I'm not sure where to put it in the code so that it works & shows up on the page. When I put it anywhere inside the Profile class the line has red underlines stating that a ";" or ")" is expected (depending on where it's put I get ";" or ")"). I know I'm not missing either, so I'm pretty sure it comes down to where I'm putting the line of code to generate the QRCode.
I'm sorry if this is is an obvious question, I'm still pretty new to React.
Please let me know if you need any more information!

You can just use the <QRCode value="hey"></QRCode>. You also don't need dom library for this. So I've removed it.
import React from "react";
import QRCode from "react-qr-code";
// empty profile page
class Profile extends React.Component
{
render()
{
return (
<div>
<h1>QR Code Page</h1>
<QRCode value="hey"></QRCode>
</div>
);
}
}
export default Profile;
Check out this live version to see it in action
https://codesandbox.io/s/billowing-butterfly-0dr39?file=/src/App.js

you can use it return itself it will work
return (
<div>
<h1>QR Code Page</h1>
<QRCode value="hey" />
</div>)

Try this.
import React from "react";
import QRCode from "react-qr-code";
// empty profile page
class Profile extends React.Component {
render() {
return (
<div>
<h1>QR Code Page</h1>
<QRCode value="hey" />
</div>
);
}
}
export default Profile;

Related

Capitalized Component Not Rendering in React

I am importing a component, but it does not render. The component is capitalized. I think there is something wrong with the import statement because when I remove it and replace with "Navbar", it renders, but if i keep the import then it does not render.
import styles from "./style";
import { Billing, Business, CardDeal, Clients, CTA, Footer, Navbar, Stats, Testimonials, Hero }
from "./components";
const App = () => { return (
<div className="bg-primary w-full overflow-hidden">
<div className={`${styles.paddingX} ${styles.flexCenter}`}>
<div className={`${styles.boxWidth}`}>
<Navbar />
</div>
</div>
</div>
);
};
export default App;
import React from 'react';
const Navbar = () => {
return (
<div>Navbar</div>
);
};
export default Navbar;
import Navbar from "./Navbar";
import Billing from "./Billing";
import CardDeal from "./CardDeal";
import Business from "./Business";
import Clients from "./Clients";
import CTA from "./CTA";
import Stats from "./Stats";
import Footer from "./Footer";
import Testimonials from "./Testimonials";
import Hero from "./Hero";
export {
Navbar,
Billing,
CardDeal,
Business,
Clients,
CTA,
Stats,
Footer,
Testimonials,
Hero,
};
If i exchange the component
for basic text like "Navbar" or "Navbar" it does not render either. These 2 display when I remove the import components statement.
When i control click , it brings me to the correct Navbar component.
I am following a yt tutorial
https://github.com/adrianhajdin/project_hoobank
my repo is here: https://github.com/lukasmckaine22/userbet_landing_page
I made sure the component was capitalized. I confirmed the component actually leads to the correct component by control clicking it. I confirmed the code renders plain text only when the import is removed. I confirmed the import leads to the correct file by control clicking. I tried only importing the Navbar with and without brackets. I changed the import directory to pull the navbar directory.

One of my component in React is not loading while the rest of the components load fine

I'm a beginner who's trying to make a simple website using React JS.
Here's my App.js file and the other component which is not rendering.
All the other components are rendering fine,Except for the one which I've provided code for(homePageProducts.js).
App.js
import './App.css';
import Header from './Components/Header';
import Footer from './Components/Footer';
import CarouselContainer from './Components/Carousel/CarouselContainer';
import homePageProducts from './Components/homePageProducts/homePageProducts';
import 'bootstrap/dist/css/bootstrap.min.css';
function App() {
return (
<div>
<Header></Header>
<CarouselContainer></CarouselContainer>
<homePageProducts></homePageProducts>
<Footer></Footer>
</div>
);
}
export default App;
homePageProducts.js
import React from 'react';
function homePageProducts()
{
return(
<div>
<p>Good morning</p>
</div>
);
}
export default homePageProducts;
PS I was just testing around with this component, Hence the simple code.
I've checked twice and I'm sure that I've imported homePageProducts.js correctly
If you meant to render a React component, start its name with an uppercase letter.
import HomePageProducts from './Components/homePageProducts/homePageProducts';
and render it
<HomePageProducts/>
make sure you have imported it correctly import homePageProducts from './Components/homePageProducts/homePageProducts'

How to dynamically import images in create-react-app

I am trying to pass a locally stored image name to a component via props. The problem is
<img src={require(`../assets/img/${iconName}.svg`)} />
is not working. I have bootstrapped the project using create-react-app. The component looks like this:
import React from 'react';
import best from '../../assets/img/best.svg';
import cashback from '../../assets/img/cashback.svg';
import shopping from '../../assets/img/shopping.svg';
const Card = ({iconName}) => {
return (
<>
<img alt="icon" src={require(`../assets/img/${iconName}.svg`)} />
</>
);
};
export default Card;
SVGs are failing to load. Even if I write something like:
<img src={require('../assets/img/best.svg')} />
<img src={`${iconName}`} />
it doesn't work. Can this be an issue with webpack loader in CRA?
Because those are SVGs, I imagine, there's a static number of images you might want to display.
At this point you might need to import them all in the beginning. Depending on the tools that you're using around react the dynamic loading of data may or may not work. Your approach definitely would not work for instance with Next.JS (a framework around react).
so I would suggest just loading them all, putting into a map and using that instead of the dynamic require.
import React from 'react';
import best from '../../assets/img/best.svg';
import cashback from '../../assets/img/cashback.svg';
import shopping from '../../assets/img/shopping.svg';
const icons = Object.freeze({best, cashback, shopping});
const Card = ({iconName}) => (<img alt="icon" src={icons[iconName]} />);
export default Card;

"Value is declared but never read" but i am declaring the value

I'm trying to build a portfolio website in React ,i'm very new to React (about 3 days), and i have imported some code for the website nav bar and declared it in the code but it doesn't show up on the website and vscode says that the value was not declared. How do i fix this?
I've tried turning the code into a component and rearranging the code but i still get the same result.
App.js - main code
import React from 'react';
import './App.css';
import navBar from './navBar/navBar';
function App() {
return (
<div className="app">
//navBar that won't show up
<navBar />
<div className="landingPage"></div>
<div className="projectPage"></div>
<div className="aboutPage"></div>
<div className="footer"></div>
</div>
)
}
export default App;
Code for the navBar
import React from "react";
import "./navBar.css";
function navBar() {
return (
<div>
<h1>NAVBAR PLACEHOLDER</h1>
</div>
)
}
export default navBar;
Your question title is little bit wierd. You have fixed the value error in your code. But you still have error with the component:
//navBar that won't show up
<navBar />
In react custom built components name should always start with capital letter. So do this:
import NavBar from './navBar/navBar';
// -- ^^ you can name it anything as it's default import
<NavBar />

How can I run correctly a css-load in react?

I'm learning react and I'm trying to put in my personal project some CSS. I was searching for it and I found several ways to do so:
javascript
npm run eject
// and modifi the path [name]__[local]__[]
import Classes from '.../css/style.css'
//using this method
<link rel="stylesheet" src="./css/styles.css">
// in HTML file
//adding react glamor
Which is the correct way to do it?
Hi only import in the component for example
import './my-style.css'
and in the component jsx call clasname normally
<div className="my-class-css">
...
</div>
Like this:
import React, {Component} from 'react';
import './my-style.css'
class MyComponent extends Component{
render(){
return(
<div className="my-class-css">
....
</div>
)
}
}
export default MyComponent;

Categories

Resources