24
loading...
This website collects cookies to deliver better user experience
npx create-react-app react-qr-generator
cd react-qr-generator
npm install react-bootstrap [email protected]
npm install react-qr-code
src/App.js
to the following:import { useState } from 'react'
import 'bootstrap/dist/css/bootstrap.min.css';
import Container from 'react-bootstrap/Container'
import Tab from 'react-bootstrap/Tab'
import Row from 'react-bootstrap/Row'
import Col from 'react-bootstrap/Col'
import Nav from 'react-bootstrap/Nav'
import QRCode from 'react-qr-code'
function App() {
const [qrText, setQrText] = useState("");
return (
<Container className='mx-auto'>
<h1 className='my-4'>Qr Generator</h1>
{qrText.length > 0 && <QRCode value={qrText} />}
<h4 className='my-3'>Choose the type of QRCode format</h4>
<Tab.Container defaultActiveKey="text">
<Row>
<Col sm={3}>
<Nav variant="pills" className="flex-column">
</Nav>
</Col>
<Col sm={9}>
<Tab.Content>
</Tab.Content>
</Col>
</Row>
<Tab.Container
</Container>
);
}
export default App;
qrText
which will hold the text to generate a QR Code. When the qrText
is not empty, the QRCode
component from the library react-qr-code
will show the QR Code for that text.<Nav>
, and the component will be added as a tab-pane nested inside <Tab.Content>
.components
nested inside src
, so make sure to create that directory.src/components/Text.js
with the following content:import { useState } from "react";
import { Button, Form } from "react-bootstrap";
function Text ({ setQrText }) {
const [text, setText] = useState('');
function handleSubmit (e) {
e.preventDefault();
setQrText(text);
return false;
}
return (
<Form onSubmit={handleSubmit}>
<Form.Group className="mb-3">
<Form.Label>Text or URL</Form.Label>
<Form.Control type="text" value={text} onChange={(e) => setText(e.target.value)} />
</Form.Group>
<Button variant="primary" type="submit">
Generate
</Button>
</Form>
)
}
export default Text
Text
component receives as a prop the function setQrText
, which will be used to set the text to be encoded as QR Code. Then, a form with one input "Text or URL" will be shown to the user.setQrText
. This will change the value of qrText
in the App
component, which will show a QR Code for that text.src/App.js
nested in <Nav>
:<Nav>
<Nav.Item>
<Nav.Link eventKey="text">Text and URLs</Nav.Link>
</Nav.Item>
</Nav>
Tab.Content
:<Tab.Content>
<Tab.Pane eventKey="text">
<Text setQrText={setQrText} />
</Tab.Pane>
</Tab.Content>
Text
component at the top of the file:import Text from './components/Text'
npm start
localhost:3000
and the website will open in a browser. You should see one tab on the left and on the right a form that shows one text field with a button.http://maps.google.com/maps?q={lat},{long}
{lat}
is the latitude and {long}
is the longitude of the location.src/components/Geolocation.js
with the following content:import { useState } from "react";
import { Button, Form, Spinner } from "react-bootstrap";
function Geolocation ({ setQrText }) {
const [lat, setLat] = useState('');
const [long, setLong] = useState('');
const [locationLoading, setLocationLoading] = useState(false);
function getCurrentLocation () {
setLocationLoading(true);
navigator.geolocation.getCurrentPosition((pos) => {
setLat(pos.coords.latitude);
setLong(pos.coords.longitude);
setLocationLoading(false);
}, (err) => {
alert(err.message);
setLocationLoading(false);
}, {
enableHighAccuracy: true
});
}
function handleSubmit (e) {
e.preventDefault();
setQrText('http://maps.google.com/maps?q=' + lat + ','+ long);
return false;
}
return (
<Form onSubmit={handleSubmit}>
<Form.Group className="mb-3">
<Form.Label>Latitude</Form.Label>
<Form.Control type="text" value={lat} onChange={(e) => setLat(e.target.value)} />
</Form.Group>
<Form.Group className="mb-3">
<Form.Label>Longitude</Form.Label>
<Form.Control type="text" value={long} onChange={(e) => setLong(e.target.value)} />
</Form.Group>
<Button variant="secondary" type="button" className="me-2" disabled={locationLoading} onClick={getCurrentLocation}>
{locationLoading && <Spinner animation="border" className="me-2 align-middle" />}
Set Current Location
</Button>
<Button variant="primary" type="submit">
Generate
</Button>
</Form>
)
}
export default Geolocation
setQrText
function as a prop. 3 state variables are defined. lat
to hold the latitude value entered in the Latitude field, long
to hold the longitude value entered in the Longitude field, and locationLoading
to show a loading spinner when fetching the user's current location.getCurrentLocation
is executed when the user clicks on Set Current Location
. You first have to obtain the user's permission to get access to their location, then, when permitted, set the lat
and long
state variables based on the coordinates obtained.qrText
is set using setQrText
to the format shown before using lat
and lang
.App.js
just like you did in the previous section under <Nav>
:<Nav.Item>
<Nav.Link eventKey="geo">GeoLocation</Nav.Link>
</Nav.Item>
<Tab.Content>
:<Tab.Pane eventKey="geo">
<Geolocation setQrText={setQrText} />
</Tab.Pane>
import Geolocation from './components/Geolocation'
BEGIN:VCALENDAR\nBEGIN:VEVENT\nDTSTART:{start-date}\nDTEND:{end-date}\nSUMMARY:{title}\nEND:VEVENT\nEND:VCALENDAR
YYYYMMDD
with no separation between any of them./src/components/Calendar.js
with the following content:import { useState } from "react";
import { Button, Form } from "react-bootstrap";
function Calendar ({ setQrText }) {
const [title, setTitle] = useState('');
const [dateStart, setDateStart] = useState('');
const [dateEnd, setDateEnd] = useState('');
function handleSubmit (e) {
e.preventDefault();
const dateStartFormatted = dateStart.replace(/-/g, "");
const dateEndFormatted = dateEnd.replace(/-/g, "")
setQrText(`BEGIN:VCALENDAR\nBEGIN:VEVENT\nDTSTART:${dateStartFormatted}\nDTEND:${dateEndFormatted}\nSUMMARY:${title}\nEND:VEVENT\nEND:VCALENDAR`);
return false;
}
return (
<Form onSubmit={handleSubmit}>
<Form.Group className="mb-3">
<Form.Label>Title</Form.Label>
<Form.Control type="text" value={title} onChange={(e) => setTitle(e.target.value)} />
</Form.Group>
<Form.Group className="mb-3">
<Form.Label>Start Date</Form.Label>
<Form.Control type="date" value={dateStart} onChange={(e) => setDateStart(e.target.value)} />
</Form.Group>
<Form.Group className="mb-3">
<Form.Label>End Date</Form.Label>
<Form.Control type="date" value={dateEnd} onChange={(e) => setDateEnd(e.target.value)} />
</Form.Group>
<Button variant="primary" type="submit">
Generate
</Button>
</Form>
)
}
export default Calendar
title
, dateStart
, and dateEnd
. When the user enters the values into the fields and clicks "Generate", qrText
will be set to the format stated above, with the values used in their specific places.src/App.js
and add a new tab under <Nav>
:<Nav.Item>
<Nav.Link eventKey="calendar">Calendar</Nav.Link>
</Nav.Item>
Tab.Content
:<Tab.Pane eventKey="calendar">
<Calendar setQrText={setQrText} />
</Tab.Pane>
Calendar
component at the top of the file:import Calendar from './components/Calendar'
MATMSG:TO:{to};SUB:{subject};BODY:{message};;
{to}
is the email address this mail should be sent to, {subject}
is the subject of the email, and {message}
is the message to include in the body.src/components/Mail.js
with the following content:import { useState } from "react";
import { Button, Form } from "react-bootstrap";
function Mail ({ setQrText }) {
const [to, setTo] = useState('');
const [subject, setSubject] = useState('');
const [message, setMessage] = useState('');
function handleSubmit (e) {
e.preventDefault();
setQrText(`MATMSG:TO:${to};SUB:${subject};BODY:${message};;`);
return false;
}
return (
<Form onSubmit={handleSubmit}>
<Form.Group className="mb-3">
<Form.Label>To Email</Form.Label>
<Form.Control type="email" value={to} onChange={(e) => setTo(e.target.value)} />
</Form.Group>
<Form.Group className="mb-3">
<Form.Label>Subject</Form.Label>
<Form.Control type="text" value={subject} onChange={(e) => setSubject(e.target.value)} />
</Form.Group>
<Form.Group className="mb-3">
<Form.Label>Message</Form.Label>
<Form.Control as="textarea" value={message} onChange={(e) => setMessage(e.target.value)} />
</Form.Group>
<Button variant="primary" type="submit">
Generate
</Button>
</Form>
)
}
export default Mail
src/App.js
in <Nav>
:<Nav.Item>
<Nav.Link eventKey="mail">Mail</Nav.Link>
</Nav.Item>
<Tab.Control>
:<Tab.Pane eventKey="mail">
<Mail setQrText={setQrText} />
</Tab.Pane>
Mail
component at the top of the file:import Mail from './components/Mail'
TEL:{phone_number}
{phone_number}
is the phone number that the person will call when they scan the QR code.src/components/Call.js
with the following content:import { useState } from "react";
import { Button, Form } from "react-bootstrap";
function Call ({ setQrText }) {
const [phoneNumber, setPhoneNumber] = useState('');
function handleSubmit (e) {
e.preventDefault();
setQrText("TEL:" + phoneNumber);
return false;
}
return (
<Form onSubmit={handleSubmit}>
<Form.Group className="mb-3">
<Form.Label>Phone Number</Form.Label>
<Form.Control type="text" value={phoneNumber} onChange={(e) => setPhoneNumber(e.target.value)} />
</Form.Group>
<Button variant="primary" type="submit">
Generate
</Button>
</Form>
)
}
export default Call
src/App.js
the tab for "Call":<Nav.Item>
<Nav.Link eventKey="call">Call</Nav.Link>
</Nav.Item>
Call
component:<Tab.Pane eventKey="call">
<Call setQrText={setQrText} />
</Tab.Pane>
Call
at the beginning of the file:import Call from './components/Call'
smsto:{phoneNumber}:{message}
{phoneNumber}
is the phone number to send the SMS to, and {message}
is the prefilled message. The {message}
is optional. So, you can just have the phone number and the user will be able to send you a message without prefilled content.src/components/Sms.js
with the following content:import { useState } from "react";
import { Button, Form } from "react-bootstrap";
function Sms ({ setQrText }) {
const [phoneNumber, setPhoneNumber] = useState('');
const [message, setMessage] = useState('');
function handleSubmit (e) {
e.preventDefault();
setQrText(`smsto:${phoneNumber}:${message}`);
return false;
}
return (
<Form onSubmit={handleSubmit}>
<Form.Group className="mb-3">
<Form.Label>Phone Number</Form.Label>
<Form.Control type="text" value={phoneNumber} onChange={(e) => setPhoneNumber(e.target.value)} />
</Form.Group>
<Form.Group className="mb-3">
<Form.Label>Message (Optional)</Form.Label>
<Form.Control type="text" value={message} onChange={(e) => setMessage(e.target.value)} />
</Form.Group>
<Button variant="primary" type="submit">
Generate
</Button>
</Form>
)
}
export default Sms
src/App.js
under <Nav>
:<Nav.Item>
<Nav.Link eventKey="sms">SMS</Nav.Link>
</Nav.Item>
Sms
component under Tab.Content
:<Tab.Pane eventKey="sms">
<Sms setQrText={setQrText} />
</Tab.Pane>
import Sms from './components/Sms'
WIFI:T:{authentication};S:{name};P:{password};H:{hidden};
{authentication}
can either be nopass
, WPA
or WEP
. {name}
is the name or SSID of the network. {password}
is the password of the network and optional. {hidden}
is a boolean value (true or false) that indicates whether this network is hidden or not.src/components/Wifi.js
with the following content:import { useState } from "react";
import { Button, Form } from "react-bootstrap";
function Wifi ({ setQrText }) {
const [authentication, setAuthentication] = useState('nopass');
const [name, setName] = useState('');
const [password, setPassword] = useState('');
const [hidden, setHidden] = useState(false);
function handleSubmit (e) {
e.preventDefault();
setQrText(`WIFI:T:${authentication};S:${name};${authentication !== 'nopass' ? `P:${password};` : ''}H:${hidden};`);
return false;
}
return (
<Form onSubmit={handleSubmit}>
<Form.Group className="mb-3">
<Form.Label>Authentication type</Form.Label>
<Form.Select value={authentication} aria-label="Authentication" onChange={(e) => setAuthentication(e.target.value)}>
<option value="nopass">No Password</option>
<option value="WEP">WEP</option>
<option value="WPA">WPA</option>
</Form.Select>
</Form.Group>
<Form.Group className="mb-3">
<Form.Label>Network Name (SSID)</Form.Label>
<Form.Control type="text" value={name} onChange={(e) => setName(e.target.value)} />
</Form.Group>
<Form.Group className="mb-3">
<Form.Label>Password (Optional)</Form.Label>
<Form.Control type="password" value={password} onChange={(e) => setPassword(e.target.value)} />
</Form.Group>
<Form.Group className="mb-3">
<Form.Label>Hidden?</Form.Label>
<Form.Check
type={'checkbox'}
checked={hidden}
onChange={(e) => setHidden(e.target.checked)}
/>
</Form.Group>
<Button variant="primary" type="submit">
Generate
</Button>
</Form>
)
}
export default Wifi
src/App.js
the tab in <Nav>
:<Nav.Item>
<Nav.Link eventKey="wifi">Wifi</Nav.Link>
</Nav.Item>
Wifi
as a tab-pane in <Tab.Content>
:<Tab.Pane eventKey="wifi">
<Wifi setQrText={setQrText} />
</Tab.Pane>
Wifi
at the beginning of the file:import Wifi from './components/Wifi'
youtube://{videoId}
{videoId}
is the ID of a video. You can obtain the ID of a video from the v
parameter in the URL of the video:https://www.youtube.com/watch?v={videoId}
js/components/Youtube.js
with the following content:import { useState } from "react";
import { Button, Form } from "react-bootstrap";
function Youtube ({ setQrText }) {
const [videoId, setVideoId] = useState('');
function handleSubmit (e) {
e.preventDefault();
setQrText('youtube://' + videoId);
return false;
}
return (
<Form onSubmit={handleSubmit}>
<Form.Group className="mb-3">
<Form.Label>Video ID</Form.Label>
<Form.Control type="text" value={videoId} onChange={(e) => setVideoId(e.target.value)} />
</Form.Group>
<Button variant="primary" type="submit">
Generate
</Button>
</Form>
)
}
export default Youtube
src/App.js
inside <Nav>
:<Nav.Item>
<Nav.Link eventKey="youtube">Youtube</Nav.Link>
</Nav.Item>
<Tab.Pane eventKey="youtube">
<Youtube setQrText={setQrText} />
</Tab.Pane>
Youtube
component at the top of the file:import Youtube from './components/Youtube'
https://instagram.com/{username}
{username}
is the username of the profile to open in the Instagram app.src/components/Instagram.js
with the following content:import { useState } from "react";
import { Button, Form } from "react-bootstrap";
function Instagram ({ setQrText }) {
const [username, setUsername] = useState('');
function handleSubmit (e) {
e.preventDefault();
setQrText('https://instagram.com/' + username);
return false;
}
return (
<Form onSubmit={handleSubmit}>
<Form.Group className="mb-3">
<Form.Label>Username</Form.Label>
<Form.Control type="text" value={username} onChange={(e) => setUsername(e.target.value)} />
</Form.Group>
<Button variant="primary" type="submit">
Generate
</Button>
</Form>
)
}
export default Instagram
src/App.js
add the new tab under <Nav>
:<Nav.Item>
<Nav.Link eventKey="instagram">Instagram</Nav.Link>
</Nav.Item>
Instagram
under <Tab.Content>
:<Tab.Pane eventKey="instagram">
<Instagram setQrText={setQrText} />
</Tab.Pane>
Instagram
at the top of the file:import Instagram from './components/Instagram'
https://twitter.com/{username}
{username}
will be opened in the Twitter app.https://twitter.com/intent/tweet?text={content}
{content}
is the content of the tweet.src/components/Twitter.js
with the following text:import { useState } from "react";
import { Button, Form } from "react-bootstrap";
function Twitter ({ setQrText }) {
const [type, setType] = useState('profile')
const [text, setText] = useState('');
function handleSubmit (e) {
e.preventDefault();
setQrText('https://twitter.com/' + (type === 'profile' ? text : 'intent/tweet?text=' + text));
return false;
}
return (
<Form onSubmit={handleSubmit}>
<Form.Group className="mb-3">
<Form.Label>Type</Form.Label>
<Form.Select value={type} aria-label="Type" onChange={(e) => setType(e.target.value)}>
<option value="profile">Profile</option>
<option value="tweet">Tweet</option>
</Form.Select>
</Form.Group>
<Form.Group className="mb-3">
<Form.Label>Username or Tweet Text</Form.Label>
<Form.Control type="text" value={text} onChange={(e) => setText(e.target.value)} />
</Form.Group>
<Button variant="primary" type="submit">
Generate
</Button>
</Form>
)
}
export default Twitter
src/App.js
add the new tab in <Nav>
:<Nav.Item>
<Nav.Link eventKey="twitter">Twitter</Nav.Link>
</Nav.Item>
Twitter
component in <Tab.Content>
:<Tab.Pane eventKey="twitter">
<Twitter setQrText={setQrText} />
</Tab.Pane>
Twitter
component at the top of the file:import Twitter from './components/Twitter'